This article follows on from the first three parts that can be viewed here:
I am discussing the basics of the Python language to learn it quickly. In France these bases are taught in high school at the second SNT, first and senior NSI classes. They are also part of the knowledge program for CAPES NSI.
When an error occurs, (an exception) it can be managed using the try statement:
try: print (r) Except NameError: print ("undefined r variable") Except: print ("Other exception")
#Ceci is a comment #sur #plusieurs line print ("Hello Python") """ This is a comment On several line """ print ("Hello Python")
Classes are like an object builder, it helps to model real concepts.
Person class: def __init__ (self, name, age): self.name self.age def ma_fonction (self): print ("Name of person: " 'self.name) p1 - Person ("Sebastian," 36) print (p1.name) print (p1.age) p1.ma_fonction() p1.age - 40 #Modification of the properties of the object del p1.age #supprime ownership of the object del p1 #supprime the object
self is a reference to the current instance of the class and is used to access variables that belong to the class. It may be named other than self, but it must be the first setting of any class function.
Inheritance defines a class that inherits all the methods and properties of another class.
Animal class: def walk(): print ('walk') Class Dog (Animal) Pass c1 - Dog () c1.walk()
The function __init () is automatically called every time a new object is created.
super () will make the child class inherit all the methods and properties of its parent.
Student class (Person): def __init__ (self, name, first name): super().__init__ (name, first name)