Python 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

o

o 783 0 -192 0b010 0o642 0xF3


o

o
o 9.23 0.0 -1.7e-6

o
o True False

o ' " '''

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 round(15.564, 1) 15.564 15.6


o round(15.564, 0) 15.564 16.0

o bool(x) x False None False 0 x


True True

o str(x) str(x) x "x"


o chr(64) chr()

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 ['a', 'b', 'c']

o dict([(3, "three"), (1, "one")])


o

o {1: 'one', 3: 'three'}

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']

o str str → list str


 "1,4,8,2".split(",") # Output: ['1','4','8','2']
My_list = [10, 20, 30, 40, 50]

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(My_list[:-1]) # Output: [10, 20, 30, 40]


print(My_list[::-1]) # Output: [50, 40, 30, 20, 10]
print(My_list[1:3]) # Output: [20, 30]
print(My_list[1:-1]) # Output: [20, 30, 40]
print(My_list[::-2]) # Output: [50, 30, 10]
print(My_list[:3]) # Output: [10, 20, 30]
print(My_list[-3:-1]) # Output: [30, 40]
print(My_list[3:]) # Output: [40, 50]
print(My_list[::2]) # Output: [10, 30, 50]
print(My_list[:]) # Output: [10, 20, 30, 40, 50]
len()

print(len(my_list))

my_list = [10, 20, 30, 40, 50]


length = len(my_list)
print(length) # Output: 5

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

from monmod import nom1, nom2 as fct


nom1 nom2 monmod
nom2 fct as

import monmod monmod


monmod.nom1

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 **

print((1+5.3)*2) #Output: 12.6

You might also like