Python - Basics - Dr. CHADI
Python - Basics - Dr. CHADI
jezdbihiefv
In [2]: type(b)
Out[2]: float
In [3]: type(d)
Out[3]: str
<class 'list'>
<class 'set'>
<class 'dict'>
<class 'tuple'>
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
Cell In[5], line 1
----> 1 d[0] = 'amine' #error, because it's immutable
In [23]: e[0]
Out[23]: 'a'
-------------------------------------------------------------------------
--
TypeError Traceback (most recent call las
t)
Cell In[24], line 1
----> 1 e[0] = 'z' #error, because it's immutable
In [6]: a = "aabcd"
a.replace('a', 'h')
Out[6]: 'hhbcd'
Out[28]: 'silopaidnum'
In [ ]:
In [ ]:
In [ ]:
In [1]: k = [1, 2, 3, 4, 5]
v = ["a", "b", "c", "d", "e"]
d = dict(zip(k, v))
d
In [ ]:
4. built-in functions
In [4]: a = input("entrer la valeur de a:")
In [5]: a
Out[5]: '33'
In [31]: a = int(a)
type(a)
Out[31]: int
In [31]: print("mundiapolis")
mundiapolis
J'ai eu 10 sur 10
Out[1]: 5
In [12]: sorted(mylist)
In [13]: round(5.76543, 2)
Out[13]: 5.77
In [2]: a = [2, 1, 3, 8]
In [3]: max(a)
Out[3]: 8
In [4]: min(a)
Out[4]: 1
In [5]: x = sum(a)
x
Out[5]: 14
In [ ]:
5. built-in modules
see all here: https://docs.python.org/3/py-modindex.html (https://docs.python.org/3/py-
modindex.html)
In [3]: import os
cwd = os.getcwd()
print("Current working directory:", cwd)
In [18]: import os
path = 'my_dir'
os.makedirs(path, exist_ok=True)
Out[19]: 'my_dir/example_1.txt'
file.close()
In [21]: import os
files = os.listdir(path)
print(files)
['example_1.txt']
Out[22]: 'my_dir/example_1.txt'
In [ ]:
0.2653782615653967
9.967925911386839
4
70
draw
[40, 50, 30, 20]
b'This is a text file created with Python.\r\nYou can write multiple line
s of text here.\r\nThis is the last line of text.'
aaaa
bbbb
bbb
-------------------------------------------------------------------------
--
NameError Traceback (most recent call las
t)
Cell In[9], line 1
----> 1 print(x) # this will not be executed because x is not defined
2 # if there was no try and except it will give error
In [13]: # try and except, program continue even when there are errors
try:
print(x)
except:
print("x is not defined")
x is not defined
0
1
2
3
4
5
6
7
8
9
3
4
5
6
In [15]: jj
0 1
1 3
2 6
3 8
4 10
a 0
b 1
c 2
In [ ]:
Out[3]: 10
9. Classes
In [31]: a = [1, 2, 3, 5]
print(type(a))
<class 'list'>
In [ ]:
def sayhello(self):
print(self.color)
print("hello ", self.name)
print("your age is", self.age)
In [12]: Mohamed.sayhello()
rouge
hello Mohamed
your age is 20
In [ ]:
In [37]: # classes:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
In [18]: # héritage:
class Vehicle:
def __init__(self, a, b, c, d, e, f):
self.couleur = a
self.pos = b
self.vitesse = c
self.nbr_portes = d
self.nbr_roues = e
self.taille = f
def transporter(self):
return self.pos + self.vitesse
V = Vehicle("black", 4, 80, 4, 4, 3)
V.transporter()
Out[18]: 84
Out[19]: 74