This is an old revision of the document!


Python3 Crash Course

Basics

Command Line (CLI)

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

Input/Output

Data Structures

Lists:

l1 = [1, 2, 3]
l2 = [10, 'mere', [25, 20], {}, ]

Object Oriented Programming



String formatting

Old way: % - format OR str.format() New way: F-strings

string = f"Ana are {nr_mere} mere."

Conditions & Loops

i = 0
cnt1 = 0
while i < 10:
    if i%2 == 0:
        cnt += 1
 
 range(), len(), type()

Built-in Functions

range(start, stop, step)
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

References

ic/resurse/python.1601119365.txt.gz · Last modified: 2020/09/26 14:22 by acosmin.maria
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0