Introduction Python LSE3
Ex1#print in python
print("Hello Python")
Ex2#Learn Variables in python
character_name ="John"
age ="30"
is_male = "False"
print("there was a " + is_male+ "called " + character_name + " Mohamed")
print("he was " + age +" years old ")
Ex3# Learn Strings in python
text_1="MasterBio"
text_2="Master Bio"
print(len(text_1))
print(len(text_2))
first_name=" Master "
laste_name="Bio"
print(first_name[0],laste_name[0])
Ex4# Learn Numbers in python
print(2) #int
print(2.385) #float
print(-2.385) #float
print(3+5)
print(3-5)
print(10/5)
print(3*5+6)
print(3*(5+6))
print(10 % 3)
my_num = -5
print(abs(my_num))
print(str(my_num)+" my favorite number")
print(pow(10,4))
Hammadi Oumaima
Introduction Python LSE3
print(max(4,10))
print(min(4,10))
print(round(3.7))
print(round(3.2))
from math import*
print(sqrt(16))
print(sqrt(4))
Ex5# Learn Boolean in python
#Boolean
print(10 >9 )
print(10 == 9)
print(0 < 9)
print(bool("ok"))
print(bool(15))
print(bool(""))
print(bool(0))
Ex6# Learn inputs in python
name= input("enter your name :")
age= input("enter your age:")
print("hello "+name+" and your age is "+age)
Ex7# Learn lists in python
lessons=["French","English","Arabic","Programming"]
print(lessons[0:2])
print(lessons[1:3])
#print(lessons[:2])
print(lessons[2:])
print(lessons[1:])
print(lessons[0])
Hammadi Oumaima
Introduction Python LSE3
Ex8# Learn functions in python
def say_hello(): #Ouverture de la fonction
print("hello")
say_hello() #Fermeture de la fonction
####
def say_hello(name,age):
print("hello "+name+" your age is "+str(age))
say_hello("Ahmed",15)
def cube(number):
number*number*number
cube(3)
Rien ne s’affiche, donc la solution est au-dessous :
def cube(number):
return number*number*number
print(cube(3))
Autrement :
def cube(number):
return number*number*number
result=cube(3)
print(result)
Ex9# Learn if statement in python
is_hungry =True
wants_to_eat= True
if is_hungry and wants_to_eat :
print("go to eat you are hungry or you just want to eat")
elif is_hungry and not wants_to_eat :
print("eat so you can survive")
Hammadi Oumaima
Introduction Python LSE3
elif not is_hungry and wants_to_eat :
print("do not eat you are not hungry")
else :
print("do not eat")
Ex10# Learn comparison operators in python
def max_num(num1,num2,num3):
if num1 >= num2 and num1>=num3 :
return num1
elif num2 >=num1 and num2 >= num3:
return num2
else:
return num3
print(max_num(300,260,10))
def match_string(str1,str2):
if str1 != str2 :
print("the string don't match")
else :
print("the string do match")
(match_string("Master_Bio1","Master_Bio1"))
Ex11# Learn while loops in python
i=1
while i<= 5 :
print(i)
i+=1
print("the loop has ended")
i=1
Hammadi Oumaima
Introduction Python LSE3
while i<= 5 :
i+=1
print(i)
print("the loop has ended")
i=1
while i<= 10 :
i+=1
if i == 5:
break
print(i)
print("the loop has ended")
i=1
while i<= 10 :
i+=1
if i == 5:
continue
print(i)
print("the loop has ended")
Ex12# Learn for loops in python
for char in "Master":
print(char)
Students=["amal","mohamed","mariem","zeinbe","omar"]
for char in range(len(Students)):
if Students[char] =="mariem":
continue
print(Students[char]," is your friend")
for x in range(5):
print(x)
Hammadi Oumaima
Introduction Python LSE3
for x in range(3,9):
print(x)
for x in range(10):
if x % 2 ==0:
print(x,"est un nombre pair")
else :
print(x,"est un nombre impair")
Text="MasterBio1"
print(len(Text))
print(range(len(Text)))
for x in range(len(Text)):
print("La lettre d'index ",x,"est",Text[x])
Ex13# Learn the power function in python
def power(base_num,power_num):
result =1
for index in range(power_num) :
result=result*base_num
print(index,result)
return result
power(2,3)
#print(power(2,3))
Ex14# Learn the nested loops & 2D array in python
no_list=[
[1,2,3],
[4,5,6],
[7,8,9],
[0]
Hammadi Oumaima
Introduction Python LSE3
]
print(no_list)
for row in no_list :
print(row)
for col in row :
print('col:',col ,"col*2 est : ",col*2)
Ex15# Writing comments in python
#This is a Python Tutorial
print('Python')
Ex16# Catching errors in python
try:
value =int(input("Enter a number :"))
print(value)
print("success")
result = 10/0
#print(value/10)
except ZeroDivisionError as err:
print(err)
except ValueError as err1:
print(err1)
Ex17# Class & Objects in python
#Student.py
class Student:
def __init__(self, name, age, departement):
self.name = name
self.age = age
self.departement = departement
#app.py
from Student import Student
Hammadi Oumaima
Introduction Python LSE3
Student_1=Student("Ala",10,"Infomatique")
Student_2=Student("Salma",20,"Physique")
print(Student_1.age,Student_1.name)
print(Student_2.age,Student_2.departement)
Ex18# Class functions in python
#Student.py
class Student:
def __init__(self, name, age, departement):
self.name = name
self.age = age
self.departement = departement
def is_young(self):
if self.age >= 20:
return True
else:
return False
#app.py
from Student import Student
Student_1=Student("Ala",10,"Infomatique")
Student_2=Student("Salma",20,"Physique")
print(Student_1.age,Student_1.name)
print(Student_2.age,Student_2.departement)
print(Student_2.age,Student_2.is_young())
print(Student_1.age,Student_1.is_young())
Ex19# Class inheritance in python
#FamilyTeacher.py
Hammadi Oumaima
Introduction Python LSE3
from Teacher import Teacher
class FamilyTeacher(Teacher):
def what_specialization(self):
print("i work with families")
def paid_by_who(self):
print("i get paid by the people")
#app.py
from Teacher import Teacher
from FamilyTeacher import FamilyTeacher
teacher_1=Teacher() #Constructor
teacher_2=FamilyTeacher() #Constructor
teacher_1.studied_years()
teacher_1.works_where()
teacher_1.paid_by_who()
teacher_2.what_specialization()
teacher_2.paid_by_who()
teacher_2.studied_years()
teacher_2.works_where()
teacher_2.paid_by_who()
Hammadi Oumaima
Introduction Python LSE3
Hammadi Oumaima