This is an old revision of the document!
Start REPL (Read Execute Print Repeat) interactive mode and exit:
shell$ python # OR python3 >>> >>> quit() shell$
Execute files:
shell$ python filename.py shell$ python filename.py arg1 arg2 arg3
Variables:
a = 10 b = "Ana are mere" c = [1, 2, 3]
Functions:
def my_function(a
Lists:
l1 = [1, 2, 3] l2 = [10, 'mere', [25, 20], {}, ]
Old way: % - format OR str.format() New way: F-strings
string = f"Ana are {nr_mere} mere."
i = 0 cnt1 = 0 while i < 10: if i%2 == 0: cnt += 1 range(), len(), type()
for i in range(5): print(i) # 0 1 2 3 4 for i in range(3, 10, 2): print(i) # 3 5 7 9
In Python 2.x range() generates first the entire list of numbers and then iterates through it.
for i in range(5): # equivalent to: for i in [0, 1, 2, 3, 4]
The alternative is the xrange() function which uses a generator to generate numbers as you need them. Python 3.X range() function is the xrange() function from Python 2.x.
def xrange(start, stop, step): i = start while i < stop: yield i i += step iter = xrange(0, 10, 1) type(iter) # <class 'generator'>
Other