This article follows on from the first three parts that can be viewed here:
- https://128mots.com/index.php/2019/12/04/python-les-bases-capes-nsi-snt-en-plus-de-128-mots/
- https://128mots.com/index.php/2019/12/06/python-les-bases-en-plus-de-128-mots-partie-2/
- https://128mots.com/index.php/2019/12/07/python-les-bases-en-plus-de-128-mots-partie-3/
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.
Exceptions:
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")
Comments:
#Ceci is a comment #sur #plusieurs line print ("Hello Python") """ This is a comment On several line """ print ("Hello Python")
Classes:
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.
Heritage:
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()
Manufacturer:
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)