Python 3
Python 3
Python 3
o
o 9.23 0.0 -1.7e-6
o
o True False
o "One Two"
o
multiline_string = '''This is a
multiline
string.'''
o
▪ \n
▪ \t
▪ \'
▪ \xhh
o
o b"toto"
o
o
o
o
• [1, 5, 9]
• ["x", 11, 8.9]
• ["mot"]
o
o
o
o
o
o
• (1, 5, 9)
• 11, "y", 7.4
• ("mot",)
• True,
o
o
o
o
o
• "Hello, world!" # as str
• b"Binary data" # as bytes
o
o
o
• {"key": "value"}
• {1:"one",3:"three",2:"two",3.14:"π"}
• dict(a=3,b=4,k="v") # another syntax
o
o
o
fs = frozenset({1, 2, 3})
o
• {"key1", "key2"}
• {1,9,3,0}
o
o
o
o a, toto, x7, y_max, BigOne
o 8y, and, for
o
▪ x = 1.2 + 8 + sin(y) sin(y)
▪ 1.2 + 8 + sin(y) x
o
▪ a = b = c = 0 a b c 0
o
▪ y, z, r = 9.2, -7.6, 0 y = 9.2 z = -7.6 r = 0
o
▪ a, b = b, a
▪ a b
o
▪ a, *b = seq
▪ a b
▪ a, b, *c = seq a b seq
c seq
▪
seq = ["A546", 45 , True]
a, *b = seq
print(a) # Output: A546
print(b) # Output: [45, True]
o += *=
o -= /= %=
▪ x += 3 x = x + 3
▪ x -= 2 x = x - 2 x *= 2 x /= 2 x %= 5
x = None None x
del x x
type()
x = 10
print(type(x)) # Output: <class 'int'>
o int("15") "15" 15
o int("3F", 16) "3F" 63
o int(15.56) 15.56 15
o → list
[int(x) for x in ('1','29','-3')] # Output: [1,29,-3]
o float("11.24E8") "11.24E8"
1124000000.0
o ord('@') ord()
o repr(x) repr()
repr(42) "42"
o bytes([72, 9, 64])
o [72, 9, 64]
o b'H\t@'
▪ 72 H
▪ 9 \t
▪ 64 @
o list("abc")
o "abc"
o set(["one", "two"])
o ["one", "two"]
o {'one', 'two'}
o join()
o str str → str
':'.join(['toto','12','pswd']) # Output: 'toto:12:pswd'
o split()
o str → list str
"words with spaces".split() # Output: ['words','with','spaces']
My_list[index]
print(My_list[0]) # Output: 10
print(My_list[1]) # Output: 20
print(My_list[2]) # Output: 30
My_list[index]
print(My_list[-1]) # Output: 50
print(My_list[-2]) # Output: 40
print(My_list[-3]) # Output: 30
My_list[start:stop:step]
print(len(my_list))
del My_list[index]
My_list[index]=25
del My_list[3:5]
My_list[1:4]=[15,25]
o True False
o
▪ <
▪ >
▪ <=
▪ >=
▪ ==
▪ !=
o
o
▪ a and b True a b True
▪ a or b True a b True
o and or
▪ a False a and b False b
▪ a True a or b True b
▪ a b
o not a a
o True False
sys.path
if elif else
if
if
x = 10
if x > 5:
print("x is greater than 5")
x
elif
if
if elif
x = 5
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else
if-elif
x = 5
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
x
if age<=18:
state="Kid"
elif age>65:
state="Retired"
else:
state="Active"
x if x:
# with a var x:
if bool(x)==True: ⇔ if x:
if bool(x)==False: ⇔ if not x:
o +
o -
o *
o /
o //
o %
o **