Python - Start
programming
What you can do
Interpret variables and solve expressions by applying mathematical
operations.
Describe how to manipulate strings by using a variety of methods and
operations.
Build a program in JupyterLab to demonstrate your knowledge of types,
expressions, and variables.
Work with, manipulate, and perform operations on strings in Python.
Pyhton basics Module 1
Expressions and variables : Module1
25+30+601+45
2*30+60=120 > int
(28+2)*5=150 int
my_variable= 54
x= 25+60-78+45
y=x/60 > y(type): float
x=15 , x=x/3 > x=5
total_minute= 43+25+455 > total_hour= total_minute/60
26//5 >5 (rounded)
Pyhthon Data Structure: Module2
Learning Objectives
In this module, you will:
Describe and manipulate tuple combinations and list data structures.
Execute basic tuple operations in Python.
Perform list operations in Python.
Write structures with correct keys and values to demonstrate understanding of dictionaries.
Work with and perform operations on dictionaries in Python.
Create sets to demonstrate understanding of the differences between sets, tuples, and lists.
Work with sets in Python, including operations and logic operations.
I-Tuple + Nest
Tuple cant be opearted once define not change
tuple_1=(“djaja”, “mounada”,15,17.1,”Fine”)
index = 0.1.2.3.4
tuple_1[4]=Fine
tuple_1[0,3]= djaja, mounada (not 15, last index not include)
tuple_2= tuple1+(“yes”+100) > tuple_2= (“djaja”, “mounada”,15,17.1,”Fine,”yes”,100)
NT= ((“djaja”, “mounada”),15,17.1,”Fine”, (14,16)) ( Nesting , many tuples)
NT[0]=djaja”, “mounada” , NT[4][1]=16
II-List
List can be oparetaed
a-
l=[14,19.1,”words”,”why”, (15,0.15),[15,17.8,20],20]
index= 0/1/2/3/4/5/6
l[0]=14 , L[6]=20
l[5:7]= [15,17.8,20] , 20
l1=l+[10,”coca”] or l.extend([10,”coca”])
l=[14,19.1,”words”,”why”, (15,0.15),[15,17.8,20],20,10,”coca”]
Change entry
l[0]= “nice bro”
l=[”nice bro”,19.1,”words”,”why”, (15,0.15),[15,17.8,20],20,10,”coca”]
index=0/1/2/3/4/5/6/7/8
del(L[8]) will remove index 8
a= [”A,B,c,D”] ( 1 elem)
a.split(“,”) > [”A”,”B”,”c”,”D”] (4 elem)
a=[”apple”,15,17.8]
b=a
a[0]= “Love”
b=[’Love”,15,17.8]
III-Dictionary
b-
NB : List =[”index”:”element”] / Dictionary=[”Key”:”Value”]
diction_1={”my love”: 1980, “my heart”:1987 , “good bro”:(1991,1992,1995),
“wonderful day”:[2018,2022]}
diction_1[”my heart”]> 1987
diction_1[”50 cent”]= 2024
dction_1= {”my love”: 1980, “my heart”:1987 , “good bro”:(1991,1992,1995),
“wonderful day”:[2018,2022], ”50 cent”: 2024 }
del(diction_1[”my love”]) ( will remove key “my love” from diction_1)
“good bro” in dioction_1 > True
diction_1.keys() > will show all keys of dioction_1
diction_1.values() > will show all value of diction_1
values , keys ( techincal name in programming called Methode)
Conditions and branching : Module3
I- Boolean True /False
a- True/false
a=6 / a==7 False
a=31/ a>10 > True
b=2/ b!=6 > True (diffrent)
2- If statement / else statemnt
b- If
marks= 40
if(marks>60)
print(“accepted in work”)
print(“No you cant”) > No you cant
c- else like if
marks=45
if(marks>60)
print (“you cant work”)
else
print(“try remote job”)
print(“thank you for understanding”) > try remo....+thank you for...
d- elif +else
marks=60
if(marks>65)
print(“you are eligible for work at engineering”)
elif (marks==65)
print(“you can apply to see your result”)
else
print(“try remote work”)
print(“thank you for your effort of applying to job”)
> “try remote work” + “thank you for your effort of applying to job”
e- Or & And
year album= 1995
if (year album<1980 Or year album>1998)
print(“ the album was made in the 70s or 90s”)
else
print(“ the album was made in 80s”) > he album was made in the 70s or 90s
if we change or with and > the album was made in 80s
II- Loops
range(3)> [0,1,2]
range(10,14) =[10,11,12,13]
I-for loops
squars :
“red” “yellow” “green” “purple” “blue”
0 1 2 3 4
squares=[”red”,”yellow”,”green”...]
change all squres to whithe ?
for i in range(0,5):
square[i]=”withe”
Can you get element and index ?
yes ennumerate methode
for i, square in enumerate(squares ) ( ! squares are argument in computing)
square
“red”:0
“yellow”:1
“green”:2
etc...
II- while loop
“orange” “orange” “green” “orange” “orange”
0 1 2 3 4
squares=[”orange”,”orange”....]
Can you copy squares to new_squares using while loop methode?
squares=[”orange”, etc......]
new_squares=[]
i=0
while(squares[i]==”ornage” )
new_squares. append (squares[i]) ( . like = , append in each times add element to
create a list)
i=i+1
For remembre
a=[”route”, “train”, “voiture”]
for x in a
print(x+”s’)
a=[”routes”,”trains”,”voitures”]
c-
for i, x in enumerate(['A','B','C']):
print(i,x) > 0A, 1B, 2C
III- Functions
A Basic function
def function(a)
b=a+1
print(a,”+1=”,b) // ( function name b and input a and do a+1)
return b // (return function or return output of function)
example a-2:
def f1(input)
output= input +1
return output
c=f1(5) > c=6
II - Intern function
“red” “yellow” “green” “purple” “blue”
0 1 2 3 4
squares=[”red”.”yellow”,”green”,”purple”,”blue”]
x=len(squares) > x=4
example2:
10 15.1 100 32 75
0 1 2 3 4
a=[10....]
y=sum(a) > 10+15.1+100+32+75 > y:++++
B- function sorted
make order
C=[10.7.9.8.100]
new_c= sorted(c) > new_c=[7,8,9,10,100]
or c.sort() >c=[7,8,9,10,100]
c.reverse() > [100,10,9,8,7]
C - function Multiply
def mult(x,y)
c=x*y
return c
v=mult(2,15) > v=30
E= mult(2, “Maroc”)
E= “Maroc”, “Maroc”
D-Function enumerate
stuff 9.8 7 6 2
i 0 1 2 3
def printrating(stuff)
for i, s in enumerate(stuff)
print(“Album”, i , “Rating is”, s) > Album 0 Rating is 9.8
Album 1 ...
for a in a
a=a+1
print([”a”,”b”,”c”]> a1 b1 c1
2- Exception handling
try:
getfile=open(“myfile”,”r”)
getfile.write(“My file for exception handling”)
exceptIO:
print(“Unable to read and write data in the file”
else:
print(“Some other errors occure”)
3- Object & Classes
A-Type()
type([10,”amine”,15.7] > list
type(1): int
a=[15,10,abc] ( or instance of type is
list=objet=instance
type(a)> list
type(“bro”)> str ( object, instance)
B- Classe
Class have multiple objects(instances) And Methode
-class circle object //( attribue: radius, color)
-class rectangle object // ( attribute: height, width,
color)
-class box object // ( attribue: heigh, weight, etc, color)
how to create instance or object ?
-class circle(object)
def -- int – (self, radius, color) // circle
self_radius= 5
self_color=pink
-class rectangle(object
def --int--(self, height, width, color) // rectangle
self_height= 55
self_width=64
self_color=blue
C1=Circle(14,”red”)
c1.radius > 14
c1.color > red
If you want change color just type
c1.color=”pink”
What is the methode ?
You have attribute and you define object but you
want make change in object
lets see
-class=Circle(object):
def -- int – (self, radius=3, color=”red”) // circle
self_radius= radius
self_color=color
def add_radius(self,r)
self_radius=self_radius+r
def drawCircle(self):
return(self_redius)
c1=Circle(2,”red)
c1=add_radius(8)
self_radius =self_radius+8
c1> 10, red
Help 1 ! Create object circle
C1=Circle (10,”red”)
Help 2 ! Which of the following options would create an appropriate object that
points to a Black, 5-seater vehicle with a maximum speed of 260kmph and a mileage
of 80kmpl?
car= Vehicle(260, 80)
car.color=”black”
car.assing.seating.capacity(5)