The second lesson introduces the use of some variables in Python, lists, tuples, dictionaries, and more ... The space will be more ... So in the finishing ...
Skip the second lesson first ... Come straight to the third lesson. Object-oriented learning in Python and connection to MySQL database!!!
Python is also an object-oriented language, and before we start I have to boast that I really ... It's true.. Really good like Python ah ah ah!!!
Here we go into the object world of Python ...
How do I define, create, and use a class in Python???
Gossip doesn't say much. Look at the code is the hard truth ...
python--class Creation
‘‘‘@desc: Basic class definition @author: jimly @date: 20160325‘‘‘ClassStudent:stunum = 0#Exposing variables__num = 0#Private variables are defined with two underscores at the beginning of a variableDef__init__(Self, name, age): Self.name =Name Self.age =Age Student.stunum + = 1DefShowstudent (self):Print (Self.name,‘‘, Self.age)‘Create Student objects‘Stu = Student ("Zhangsan", 20) STU1 = Student ("LiSi", 21)‘Accessing properties in a class‘Stu.showstudent ()Print‘Stunum:%d‘ %Student.stunum)‘Program error because the NUM property is a private property‘#Print (' stunum:%d '% student.num)Print‘Stu Name:', GetAttr (Stu,‘Name‘))#Returns the value of the Name property if no run error occursPrint‘Stu has sex?', Hasattr (Stu,‘Sex‘))#Determines whether the Stu object has a sex attribute, there is a return of true, there is no return falsesetattr (Stu,‘Sex‘,‘Mans ") # Set the Sex property value and create a property if it does not exist print ( stu Sex: ', GetAttr (Stu, sex " sex< Span style= "color: #800000;" > ') # delete an attribute print ( Span style= "color: #800000;" > ' sex ")
Program output:
Zhangsan 20
Stunum:2
Stu Name:zhangsan
Stu has sex? False
Stu Sex:man
Stu Sex:false
Not like the Java language ... Python is supporting multiple inheritance drops ... such as Class A (P1, P2, P3 ... )。。。
Where class P1,p2,p3 is the parent class ... A is to inherit so many subclasses of the parent class ...
There is also the constructor method of the subclass that does not call the constructor of the parent class by default ... This is also a bit out of Java ...
python--Inheriting classes
‘‘‘@desc:P Ython inheritance @author: jimly @date: 20160325‘‘‘ClassGrand:Def__init__(self):Print‘Grand Constructor‘)DefSay (self):Print‘I am Grand‘)ClassParent:Def__init__(self):Print‘Parent Constructor‘)DefMethod (self):Print‘Parent method‘)ClassChild (Parent, Grand):#The child class inherits the parent class and the Grand class, and Python supports multiple inheritanceDef__init__(self):# grand.__init__ (self)# parent.__init__ (self) print (' childConstructor' ) def Method (self): # override methods in the parent class print (' child method') Child = Child ( ) Child.method () Child.say ( )
Program Run output:
Child Constructorchild Methodi am Grand
python--Database Connection
To connect to the MySQL database via Python code, there are two libraries of mysqldb and pymysql that can be used to connect to MySQL ...
But after Python3 no longer support mysqldb ... Because I'm pretending to be Python 3.4.3. So you can only use Pymysql library ...
Attached: See version of Python installation = "Typing command in DOS window: Python--version
First download the ez_setup.py file on the Python website ... If the link fails, please visit https://pypi.python.org/pypi/setuptools/
After downloading the ez_setup.py file, execute the file ...
Execute command: python ez_setup.py as shown
After execution, a Easy_install.exe file is generated under the Scripts folder in the Python installation directory ... As shown
Go to this Folder ... Execute command: easy_install.exe pymysql3 to install the PYMYSQL3 service as shown in
After the PYMYSQL3 service is installed, you can write Python's program to connect to the MySQL service ...
The example code is as follows:--Query column
ImportPymysqldb = Pymysql.connect (user=‘Root', passwd=‘Root', host=‘localhost', db=‘my_form ) sql = "select * from Form_frame" cursor = Db.cursor () cursor.execute (SQL) for r in cursor: print ( ' id: ", r[0], '
Program Run Result:
Id:5 rows:111 cols:111id:6 rows:222 cols:222id:7 rows:333 cols:333Id:11
rows:2015 cols:111id:12 rows:2015 cols:222id:13 rows:2015 cols:333ID : rows:2015 cols:111id:30 rows:2015 cols:222id:31 rows:2015cols:333
Note:import pymysql can substitute aliases for 、、、 such as:import pymysql as MySQLdb where mysqldb can be customized aliases ...
At this time db = pymysql.connect (User= ' " need to change to
db = MySQLdb.connect (user='root ', passwd='root ',host='localhost', db= 'my_form')
New Case Code:
ImportPymysqldb = Pymysql.connect (user=‘Root', passwd=‘Root ", Host= localhost< Span style= "color: #800000;" > ', Db= ' my_form ' ) sql = insert into Form_frame (cols, rows) VALUES (101, 201), (102, 202) "try: Cursor = Db.cursor () res = Cursor.execute (SQL) Db.commit () print (res) #except: Db.rollback () Cursor.close () Db.close ()
Program output: 2
All right... It's here today ...
With Python falling in Love_python steps (object-oriented)