Python3¶
General¶
PEP 8 – Style Guide for Python Code: https://www.python.org/dev/peps/pep-0008/
Examples¶
Send email via SMTP:
#!/usr/bin/env python3
import smtplib
from email.message import EmailMessage
s = smtplib.SMTP('localhost')
msg = EmailMessage()
msg.set_content('Test message content')
msg['Subject'] = 'The test message'
msg['From'] = 'admin@localhost'
msg['To'] = 'example@gmail.com'
s.send_message(msg)
s.quit()
Base syntax¶
str(number) # convert to string
test = 10 # init new var
test = True # case sensitive
test = False
test_one = None # analog 'null' (functions returns None if not return any)
print(10 == 10) # True
== != <= >= < >
'Test' > 'Tesa' # True (position in text, registr, position in alphabet)
del test # del var
print("text " + str(test) + " some text")
print('hello ' * 3) # hello hello hello (num only int)
print('hello', 3 + 4) # hello 7
str(test) # to string
int(test) # to int
float(test) # to float
list(test) # to list
type(object) # return class
input("input your name: ") # return string input by keyboard
len(<string | list>) # return length
max() # return max
min()
+ - * /
** # 2 ** 3 == 8
// # 20 // 6 == 3
% # 20 % 6 == 2
+= -= *= /= %= **= //=
print('hello \n world')
print("hello \n world")
print('''hello
world''')
print("""hello
world""")
if test == 1 and time >= 12:
print(test) # tab important
elif test == 2 or (time <= 12 and date == "summer"):
print('hah!')
else:
print('hm..')
if not 1 < 2: # invert
print("yeah") # yeah don't print
while i <= 5:
print('ok')
i += 1
if i == 3:
break
if i == 2:
continue # go to next iteration
for test in range(15):
print(test)
Lists¶
test = [1, 2, 3, [4, 5, 6]]
print(test[3][0]) # 4
print(test[0:2]) # 1, 2
print(test[:2]) # 1, 2
print(test[2:]) # 3, [4, 5, 6]
print(test[::2]) # all with step 2
test[start:to:step]
test = [1, 2, 3]
print(test * 2) # [1, 2, 3, 1, 2, 3]
test = [1, 2, 3, 4, 5, 6]
print(test[-2]) # 5
print(test[::-1]) # [6, 5, 4, 3, 2, 1]
test = 'HELLO'
print(test[3]) # L
test = [1, 2, 3]
if 1 in test: # find in list
print('yeah')
if 1 not in test:
print('yeah')
test.append(object) # add to list
test.remove(object) # find first input and remove
test.insert(pos, object)
test.count(object)
test.reverse() # reverse list (return none)
test = range(10)
print(list(test)) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
test = range(10, 15)
print(list(test)) # [10, 11, 12, 13, 14]
test = range(0, 10, 2)
print(list(test)) # [0, 2, 4, 6, 8]
for element in test:
print(element) # all elements
Tuples¶
Less resource pressure than LISTS
names = ("Make", "James") # elements can't be change
names = "Make", "James" # elements can't be change
print(names[0])
Dictionary¶
test = {
"key" : "value", # key == only simple types
"key2" : "value2"
}
print(test["key"]) # exception if not exist
print(test.get('key')) # return None if not exist
print(test.get('key', 'text')) # return 'text' if not exist
if 'key' in test:
print('OK')
if 'key' not in test:
print('hm..')
Functions¶
def sum(a, b): # DEFINE ONLY BEFORE USE
"""On line documentation"""
return a + b
print(sum(1, 5))
print(sum.__doc__)
my_sum = sum
print(my_sum(1, 5))
def complex(real=0.0, imag=0.0):
"""Form a complex number.
Keyword arguments:
real -- the real part (default 0.0)
imag -- the imaginary part (default 0.0)
"""
def incr(a):
return a + 1
print(sum(incr, 5)) # send fuction in function
def fun(text):
assert text != ''
print(text)
fun('') # AssertionError
def fun(text):
assert text != '', "text could be no blank!"
print(text)
fun('') # AssertionError: text could be no blank!
# any number of args
def func(*args):
return args # args - tuple
func(1, 2, 3, 'abc') # (1, 2, 3, 'abc')
func() # ()
func(1) # (1,)
# any number of kv args
def func(**kwargs):
return kwargs # kwargs - dictionary
func(a=1, b=2, c=3) # {'a': 1, 'c': 3, 'b': 2}
func() # {}
func(a='python') # {'a': 'python'}
Modules¶
https://pypi.python.org/pypi http://pypi-ranking.info/alltime
# import module
import random
a = random.randint(0, 10)
# import function from module
from random import randint
from math import sqrt, pi
a = randint(0, 10)
# import all function from module
from random import *
# rename import function
from math import sqrt as my_sqrt
my_sqrt(25)
Exceptions¶
Catch all exceptions:
try:
print(4 / 0)
except:
print("oh no =(")
raise # show exception, exit programm
ZeroDivisionError:
try:
print(4 / 0)
except ZeroDivisionError:
print("Error")
Different exceptions catching:
try:
print(4 / 0)
except ZeroDivisionError:
print('zero division err')
except NameError:
print('var not defined')
except SyntaxError:
print('syntax err') # no catch syntax err
except:
print('some err')
Syntax error:
try:
eval('<some code>')
except SyntaxError:
print('Syntax error')
Finnaly:
try:
<code>
except:
<code>
finally:
<code> # run always (if catch or no catch)
Override error:
try:
a = 1
if a == 1:
raise ZeroDivisionError
except ZeroDivisionError:
print("zero division err")
a = 1
if a == 1:
raise ZeroDivisionError('some text') # ZeroDivisionError: some text
Own exceptions:
class MyError(Exception):
print('hm')
raise MyError('TEST')
Files¶
r - read w - write (create if not exist, rewrite if exist) a - append
b - binary mode (rb wb ab)
file = open('file.txt', 'r')
print(file.read())
file.close()
# read 1 byte
file.read(1)
file = open('file.txt', 'w')
file.write('hello')
file.close()
# append
file = open('file.txt', 'a')
file.write('hello')
file.close()
# get list of lines
file.readlines()
# close file descriptor automatically
with open('file', 'r') as f:
print(f.read())
COMMENTS¶
# this is a comment
print('hello') # this is a comment
'''Multi
lines
comment'''
"""Multi
lines
comment"""
Reserved VARS¶
foo bar
Classes¶
All classes is an object of class metaclass - ‘type’.
class MyClass:
pass # pass do nothing
# new object of class
obj = MyClass()