Difference between revisions of "Python quick reference"

From thelinuxwiki
Jump to: navigation, search
(assigning a variable to another variable)
 
(11 intermediate revisions by one user not shown)
Line 68: Line 68:
 
</source>
 
</source>
  
print multiple variables with text
+
print variables with text
 +
<source lang="python">
 +
print 'my variable is %s' % FOO
 +
</source>
 +
or
 
<source lang="python">
 
<source lang="python">
 
print 'my variable are %s %s' % (FOO, BAR)
 
print 'my variable are %s %s' % (FOO, BAR)
 +
</source>
 +
 +
print without a newline, use comma at the end of the print statement
 +
<source lang="python">
 +
for i in range(10):
 +
...    print i,
 +
... else:
 +
...    print
 +
...
 +
0 1 2 3 4 5 6 7 8 9
 
</source>
 
</source>
  
Line 78: Line 92:
 
<source lang="python">
 
<source lang="python">
 
  print s.split()[n]
 
  print s.split()[n]
 +
</source>
 +
 +
<source lang="python">
 +
str = "this is string example....wow!!!"
 +
print (str.split( ))
 +
print (str.split('i',1))
 +
print (str.split('w'))
 +
</source>
 +
 +
 +
>>> print (str.split( ))[0]
 +
this
 +
===string replace===
 +
 +
string = "geeks for geeks geeks geeks geeks"
 +
 
 +
Prints the string by replacing geeks by Geeks 
 +
print(string.replace("geeks", "Geeks")) 
 +
 
 +
Prints the string by replacing only 3 occurrence of Geeks 
 +
print(string.replace("geeks", "GeeksforGeeks", 3))
 +
 +
 +
===grep equivalent===
 +
use splitlines and test for string using regex via re.search
 +
 +
<source lang="python">
 +
import re
 +
txt = "foo\nbar\n foo\n bar"
 +
for line in txt.splitlines():
 +
  if re.search("\sfoo", line):
 +
    print line
 +
</source>
 +
 +
==System-specific parameters and functions==
 +
===Command line arguments - sys.argv===
 +
required...
 +
<br>import sys
 +
<br>usage....
 +
<br>sys.argv[0] = name of script
 +
<br>sys.argv[1] = first arg
 +
<br>sys.argv[x] = xth arg, where x=some integer
 +
<br>len(sys.argv) = number of args
 +
<br> str(sys.argv) = argument list
 +
 +
[ https://medium.com/swlh/python-argparse-by-example-a530eb55ced9 Python Argparse by Example - Rupert Thomas]
 +
 +
[https://docs.python.org/3/library/argparse.html https://docs.python.org/3/library/argparse.html]
 +
 +
===accessing input from a pipe===
 +
script that takes input from pipe and outputs it again to stdout
 +
 +
<source lang="python">
 +
import sys
 +
 +
for line in iter(sys.stdin.readline, ''):
 +
    sys.stdout.write(line)
 
</source>
 
</source>

Latest revision as of 13:34, 14 July 2022

Contents

Introduction

Lexical analysis

Data model

Execution model

Expressions

Simple statements

variable operations

assigenment examples

simple string assignment

 >>> var1 = 'foo'

this automatically creates a variable of type string

>>> type(var1)
<type 'str'>

printing your variable...

 >>> print var1
 foo

string assignment like above must be incapsulated by quotes or the right side is interpretted as a variable name.

example:

>>> var1 = foo
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'foo' is not defined

integer variable

>>> my_int_var1 = 1
>>> print my_int_var1
1

assigning a variable to another variable

>>> var2 = 'bar'
>>> var1 = var2
>>> print var1
bar

muli-variable example:

>>> var1 = 'foo'
>>> var2 = 'bar'
>>> new_var = "%s %s" % (var1,var2)
>>> print new_var
foo bar

print

print sing variable named foo

>>> print foo

print variables with text

print 'my variable is %s' % FOO

or

print 'my variable are %s %s' % (FOO, BAR)

print without a newline, use comma at the end of the print statement

for i in range(10):
...     print i,
... else:
...     print
...
0 1 2 3 4 5 6 7 8 9

Common string operations

print nth word of string

 print s.split()[n]
str = "this is string example....wow!!!"
print (str.split( ))
print (str.split('i',1))
print (str.split('w'))


>>> print (str.split( ))[0]
this

string replace

string = "geeks for geeks geeks geeks geeks"

Prints the string by replacing geeks by Geeks

print(string.replace("geeks", "Geeks"))  
 

Prints the string by replacing only 3 occurrence of Geeks

print(string.replace("geeks", "GeeksforGeeks", 3))


grep equivalent

use splitlines and test for string using regex via re.search

import re
txt = "foo\nbar\n foo\n bar"
for line in txt.splitlines():
   if re.search("\sfoo", line):
     print line

System-specific parameters and functions

Command line arguments - sys.argv

required...
import sys
usage....
sys.argv[0] = name of script
sys.argv[1] = first arg
sys.argv[x] = xth arg, where x=some integer
len(sys.argv) = number of args
str(sys.argv) = argument list

[ https://medium.com/swlh/python-argparse-by-example-a530eb55ced9 Python Argparse by Example - Rupert Thomas]

https://docs.python.org/3/library/argparse.html

accessing input from a pipe

script that takes input from pipe and outputs it again to stdout

import sys
 
for line in iter(sys.stdin.readline, ''):
    sys.stdout.write(line)