You are on page 1of 16
Output © HHH print( functior print("Data Club") print(2) Data Club 2 © #444 string formating name="sabir" age=6 fusing % print("Hello %s,you are %d years old"X(name,age)) fusing . format() print("Hello (),you are {} years old”. format(name,age)) fusing f-string print(f"Hello {name},you are {age} years old") Hello sabir,you are 6 years old Hello sabir,you are 6 years old Hello sabir,you are 6 years old bonus 1 number1=157.3345667 print("numbert = %2.4F"%number1) #2 is the minimum number of digits #.4 is the number of digits to be displayed after the decimal point 4f type of the injected value nunber1 = 157.3346 bonus 2 name = "saber" string] = f"hello {name)" string? = "hello {} again" format nane) string3 = “hello % again again" Xname print (stringl) print (string2) print (string3) hello saber hello saber again hello saber again again Input © dH input0 functior s1=input() print(s1) s2zinput("Enter a string : ") print(s2) inti=int(input()) print (int1) int2=int(input("Enter an integer = print (int2) hello hello Enter a string : hello again 3 3 Enter an integer : 25 25 hello agair bonus 1 x,y,z =input("Enter three values : print(f"x={x} , y={y} , z={z}") 234 Enter three values : X=2, y=3 , 2-4 x,y,z =input("Enter three values separated with print(#*x=(x} , y=(y} 5 z={2}") Enter three values separated with ',' : X25, yo6 5 267 » ").split() 2") .split 5,6,7 Leinput("Enter multiple values : ").split() print(L) Enter multiple values : 245690 ['2", "4", "5", "6", "9", *8") Conditionals print(F"{(x} is greater than 2") elif x print(f"{x} is between 1 and 2") print("{(x} is negative") <3 is negative Loops © #848 for loop between @ and 1") for i in range(S): print (i,end=" 01234 Le["said","sabir", 2] for i in L: print (i, end: said sabir 2 © #### while loop n=int(input("input a positive number : “)) while n<@ : int (input ("input 2 positive nunber print (n) input a positive number input a positive number 3 Data types © di integer xel print(x) 1 © sit float y-5.0 print(y) 5.8 © #### boolean T=True FoFalse print(T) print(F) True False © HHH string phrase = ‘data club" phrase? = "Data club" print(phrase[@]) # returns the first character print(phrase[-1]) # returns the first character from the end print (phrase[-6]) # returns the sixth character from the end print (phrase.upper()) # returns an uppercase version of phrase print(phrase.lower()) # returns an Lowercase version of phrase print(phrase.title()) # returns a new version of phrase with first Letter of every print(phrase.replace(‘a’, 'b')) # returns a new version of phrase where all ‘a's ai print("~ ") print (phrase) #to check if the original string has been changed print ("- print(phrase.find("a")) # returns the index of the first occurrence of “a” DATA CLUB data club Data Club dbtb club data club © HHH Lists Lst = [5@, 7@, 38, 20, 9@, 18, 40] print(Lst) Lst2 = [58, "sabir", 3, 5.8, 98, 18, 40] print(Lst2) [50, 70, 38, 20, 90, 10, 40) [50, ‘sabir’, 38, 5.8, 98, 10, 40) print(Lst[3]) #access Lst[3]=5 #modify print(Lst) #check 28 [50, 70, 38, 5, 90, 10, 40: bonus 1: check if an element is in the list x=66 if x in Lst = print(f"{(x} is in the list ") else: print(f"{(x} is not in the list ") 56 is not in the list bonus 2: slicing form : List_name[start : end : step] print(Lst[::])#taking copy of the List print (Lst[::2]) print (Lst[4::]) print("~ ") print(Lst[::-1])#inverse a List print (st [4:2:-1]) print("~ *) print(Lst)#to check if the original string has been changed [58, 70, 38, 5, 90, 10, 40: [58, 30, 98, 40] [9e, 10, 40" [40, 10, 90, 5, 30, 70, 50) [90,5 [s0, 70, 30, 5, 90, 10, 40: note : the slicing concept works also with strings © #84 tuples Atuple is a collection which is ordered and unchangeable. Tpl = (50, 7, 30, 20, 98, 10, 40) print (Tp1) Tpl2 = (50, "sabir", 3, 5.8, 98, 18, 40) print(Tpl2) (58, 78, 38, 20, 98, 10, 40) (5, ‘sabir', 30, 5.8, 98, 10, 40) print (Tp1[3]) #access Tpl[3]=5 #testing modifying print(Lst) #check 2e ‘Typetrror Traceback (most recent call last) ~\appData\Local\Tenp\ipykernel_17776\375632327.py in 1 print(Tpl[3]) #access > 2 Tpl[3]=5 #testing modifying 3 print(Lst) #check TypeError: ‘tuple’ object does not support item assignment print(Tpl[::2]) (50, 30, 90, 40) note : the slicing concept works also with tuples bonus joining tuples print(Tpli+Tpl2) (4, 2, 1, 2) © dts Dictionaries form : Dict_name{key1 value ,key2 ‘value2} Det={"name":"sabir","age":6} print (Dct) Det2={"first name":"sabir","second name":"nan3rf"} print(Dct2) Det3=("name":"sabir", “age":6) print(Dct3) {'name': ‘sabir', ‘age’: 6} {'first name’: ‘Sabir’, ‘second name’: ‘man3rf*} {'name': 'sabir', ‘age’: 6} print (Oct. keys()) print(Dct.values()) dict_keys(['name’, ‘age']) dict_values(['sabir’, 6]) © #### Mutable vs Immutable Mutable : can be changed (Lists ,dictionares ..)\ Immutable: can not be changed (Numbers ,Strings,Tuples ...) atest 1[5,6] print(1,": 1 append(7) print(1,":",4d(1)) [s, 6] : 2101625460032 [5, 6, 7] : 2101625460032 »44(1)) wtest2 s="hello" print(s,":",id(s)) ses+" Club Data” print(s,":",id(s)) hello : 2444317527600 hello Club Data : 2444354078832 wtest3 in print(i,":",id(i)) is5 print (i,":",4d(4)) 4 : 2101507942800 5 : 2101507942832 Functions def welcoming(): print("hello guys") welcoming() hello guys def show_info(nane, age, state="student"):#default value of state print(#"hello {name) , you are {age) years old , you are {state)") show_info("sabir",6) show_info("sabir",6, "software engineer") hello sabir , you are 6 years old , you are student hello sabir , you are 6 years old , you are software engineer © #### local vs global fsevil is global mark is Local seuil=12 def accepted(mark): Af mark >seuil: print ("accepted") else: print("not accepted") accepted(10) print (mark) not accepted NameError Traceback (most recent call last) ‘~\AppData\Local\Temp\ipykernel_2864\439800553.py in 18 11 accepted(10) ---> 42 print(nark) NameError: name ‘nark' is not defined © #444 print vs returr def fact_returned(num): po for i in range(1,num+t): return p x=fact_returned(3) print (x) 6 def fact_printed (num; pal for i in range(1,nums2) : ppt print(p) fact_printed(3) 6 OOP © #### Concept Attributes: Name City Capacity Methods Engineering() Attributes Attributes Name : ENSAH Name : ENSAT City : HOCEIMA. City : TANGER Capacity : 280 Capacity : 340 Capacity : 300 Methods Methods Methods Engineering() Engineering() Engineering() © #### Class class Class_nane: def _init_(self,attribute1, attribute2): Self.attributel-attributel self.attribute2=attribute2 def show_info(self, param): print(f'value of attributel is {self.attribute1} and value of attribute? i print(F*value of paraneter is {param)") © #888 object c=Class_name("string",6) print(c.attribute1) print(c.attribute2) €-Show_info( argument") string 6 value of attributel is string and value of attribute2 is € value of parameter is argument © #### constructor class Class_nane: def _ init_(self,attribut1,attribut2): self. attributi-attributt self.attribut2=attribut2 print("object created") C=Class_name("string”,6) object createc © st static attributes Humans count example class Human: count=0 #class/static attribute def _init_(self,name): Self.name=nane #object attribute Hunan .count+=1. human1=Human("sabir") hunan2=Hunan("anine” print (Human.count) print (humani.count) 2 2 human. count=5 print (Human..count) print (humant. count) 2 5 © dist static methodes Student and admir istration example class Student: count=@ #tclass/static attribute def _init_(self,name): Self.namesnane #object attribute student .count+=1 @staticmethod def show_info():# static methode print(f"number of students is {Student.count}") Student. show_info() student=Student("sabir") Student. show_info() number of students is @ number of students is 1 number of students is 1 # iH operator overloading class Assir: def _ init__(self, tofah, banane): Self. tofah=tofah self. banane=banane def __add__(self,another_one): Feturn Assir(self.tofah+another_one. tofah, self.banane+another_one.banane) assiri=Assir(2,9) assir2=Assir(4,3) assir3-assiri+assir2 print(f"number of tofah in assir3 = {assir3.tofah}") print(#"number of banane in assir3 = {assir3.banane)") number of tofah in assir3 = 6 number of banane in assir3 = 12 for more you can check : https://www.geeksforgeeks.org/operator-overloading-in-python/ # si## Printing objects class Assir: def _init_(self,tofah,banane): Self. tofah=tofah self. banane-banane def _add__ (self, another_one): return Assir(self.tofah+another_one. tofah, self.banane+another_one.banane) def _str_(self): return #"there is {self.tofah} tofahs and {self.banane} bananes” assin-Assir(8,3) print (assir) there is 8 tofahs and 3 bananes Practice Engineering() Attributes Attributes Num_ENSAs ‘Num_ENSAs List_ENSAS List_ENSAS Name : ENSAH Name : ENSAT Gity : HOCEIMA, City : TANGER ‘Capacity : 280 Capacity : 340 Methods Methods ‘Show infol ‘Show info Engineering() Engines Engineering() class ENSA: Num_ENSAS=2 List_eNsa: def _init_(self,nane, city, capacity): Self.nane-nane self. city-city self.capacity=capacity ENSA.Num_ENSAs#=1 ENSA. List_ENSAs.append(self.name) @staticmethod def show_info(): print(f"There are {ENSA.Num_ENSAs} ENSAs in Morocco : {ENSA.List_ENSAs}") def Engineering(self): print(#"Every year ,{self.capacity} future engineer is joining {self.name} def _str_(self): return #"{self.nane} is located in {self.city} and receives {self.capacity ENSAH-ENSA("ENSAH","Al hoceina",282) ENSAT=ENSA(" Tangier", 340) ENSAK=ENSA(” Kenitra”, 300) print (ENSA.Num_ENSAs) ENSA. show_info() print ("- print (ENSAH) ENSAK. Engineering() There are 3 ENSAs in Morocco : ["ENSAH', "ENSAT', 'ENSAK'] ENSAH is located in Al hoceima and receives 28@ every year Every year ,300 future engineer is joining ENSAK Inheritence class Parent: def _init_(self,attribute1, attribute2,name): Self. attributel-attributel self.attribute2=attribute2 self.name=nane def parent_func(self): print(f°I an {self.name} and this function is from the parent class") class Child(Parent) : def _init_(self,attribute1, attribute2,name, child attribute): Super().__init__(attributel, attribute2,name) self.child_attribute=child_attribute def child_func(self): print(f°I am {self.nane} and this function is from the child class" Pi-Parent(2,3,"father") CA=Child(4,5,"child”, "extra atribute") Pi. parent_func()i#executing a parent function from a parent object C1. parent_func()#executing a parent function from a child object Ci.child_func()#executing a child fuction from a child object I am father and this function is from the parent class I am child and this function is from the parent class I am child and this function is from the child class exemple class Hayawan def __init_(self,name,weight): self.nane-nane self.weight-weight def eat(self): print(f"{self.name} is eating") class Cat (Hayawan): def _init_(self,name,weight, owner) : Super().__init__(name,weight) self.owner-ouner def mian(self): print("niaw miaw miaw" class Shark(Hayawan): def _init_(self,name,weight): Super()._init__(name,weight) def swim(self): print("swiiiiming") def speak(self): print(*waa smitini 19iiirch") hayawani=Hayawan("far",5) hayawani.eat() far is eating cati=Cat("Imech",10, "Anas" ) cati.eat() cati.miaw() Amech is eating niaw miaw miaw shark1=Shark(52) sharki.eat() shark1.swim() shark1.speak() None is eating swiiiiming waa smitini 19iiirch Polymorphisme class Person: def _init_(self,nom): Self.nom=nom def affiche(self): print("I am a person”) class Student(Person) : def _init_(self,nom,cne): Super().__init__(nom) self.cne=cne def affiche(self): print("T am a student") class Teacher(Person): def _init_(self,nom, ppr super().__init__(nom) self.ppr=ppr def affiche(self): print("I am a teacher") Pi-Person("sabin") 1p119144139") Let's notice the behavior of the affiche function P1.affiche() I am a person Si.affiche() I am a student T1.affiche() I am a teacher Encapsulation This is encapsulation def _init_(self,attribute1,attribute2) : self.attribute1-attributel def funct(self): print ("Doing sonething") def func2(self): print("Doing something else") Access modifiers * public: accessible from everywhere * protected : accessible from the class itself and it's children * private : accessible just from the class class Parent: def _init_(self): self.public_attr= self._protected_att self. _private_attr: his is public" his is protected” ‘this is private" def show_public(self): print(f"printing the public attribute : (self. public_attr}") def show_protected(self): print(f"printing the protected attribute : {self._protected_attr}") def show_private(self): print(f"printing the private attribute : (self._private_attr}") class Child(Parent): def _init_(self): super().__init_() def print_public(self): print(#"this a private attribute called from child : {self.public_attr}") def print_private(self): print(f"this a private attribute called from child : {self.__private_attr) def print_protected(self): print(f"this a protected attribute called from child : {self._protected_at c Accessing the public attributes #print(P1.public_attr) 4#P1. show_public() +#C1. print_public() this a private attribute called from child : this is public ‘Accessing the private attributes +#P1. show_private() #print(P1.__private_attr) 4C1.print_private() printing the private attribute : this is private Accessing the protected attributes Guess the output #$P1. show protected() print(P1._protected_attr) #C1.print_protected() this is protected Let's do another test print (Pi._Parent_private_attr) this is private Getters && Setters Getter : to control how the user gets the value\ Setter : to control how the user sets the value class Parent: def _init_(self): Self. full_name-None def set_full_name(self,name) : self. full_name-name.replace(" ","_") def get_full_nane(self): if self.__full_name==None: return “no name set return self.__full_name.replace def print_real_full_name(sel): return self.__full_name Pi=Parent() P1.set_full_name("yassine boono00e") print (P1.get_full_name()) print(P1.print_real_full_name()) yassine boonooooc yassine_boonooaoc

You might also like