, close the connection
Example:
(1). Open a database connection
The. Connect () function is used to create a connection, write the host, username, password, existing database, port number
(2). Create a table
(3), inserting data
. Cursor () is the method used to get Python to execute the MySQL command, that is, to obtain an operation cursor; The Execute () function is the command used to execute the SQL statement, the. Commit ()
into a packageThe package here is the aggregation of modules, wrapped in the same folder.Four, otherSome students notice two different kinds of grammar when quoting.__author__ ' Bruce ' from Import darkred Import Humans if __name__ ' __main__ ' : = darkred () dark_red.printinfo () PassIf you look at the subdivision point, you will see: From Color.Red, the general from behind is directly followed by the inclusion of the upper space, and import can only follow a level of space.Reprint
beginning of a row is matched, ^ this special meaning is only at the beginning.$ means only the end of a row is matched\a to match only the beginning of the first line of the string ^ matches the beginning of each line\z that matches only the end of a row line string matches the first line of the line end\b Only matches the boundary example of a word: \binfo\b matches only "info" does not match information\b to match non-word boundariesExamples are as follows:>>> Print Re.compile (r "\binfo\b")
You may have seen a similar Python line of code that can be implemented online, such as:99 Multiplication Table:Print (". Join" (['%s*%s=%-2s '% (y,x,x*y) for Y in range (1,x+1)]) "For X in Range (1,10)])or output the value of the first 100 Fibonacci sequences:Print ([x[0] for x in [(A[i][0], A.append ((A[i][1], a[i][0]+a[i][1]))))-A In ([[[[]],) ' For I in range (100)]])Well, these can be done, then the following cool code, more worthy of a look!Sing
example of aa = a ()End I run the above statement in the Python2.7 version of IdleWhat's going to happen? To Class A, an instance is generated, in memory we call itA1UnderA1, the instance a,a the object instance pointed to is generated we call itA1Now A.count is 1, i.e.A1The count in the instance is 1 at this point, if we exit the program, a will not be immediately recycled so A.count still 1,sys.getrefcount (a) is still 2 (and 1 is incremented for incoming parameters)now open test.py run once,
List: slices, indexes, references
Build list: a=[1,2,3]
references:a_ref=a, and a point to the same address
List common actions:copy operation b=a[:]
A.append(3): Add an element after list a
A.insert(1, 20): Inserts 20 into the 1 position of List A
A.pop(): pops up the last element of the list
A.sort (): Sort the list
A.reverse(): Anti-sorting the list
del a[1]: Remove the first element of the list
Nested, multiple types coexist into a list, such as c=[1,2, ' 123 ',
[Getting started with Python 013] yuanzu: A list of things on the shackles
Tuples: list with shackles
Because it is closely related to the list, the actual use of the ancestor and list is very similar.
This section mainly discusses the differences between elements and lists.
Tuples cannot be changed, but cannot be inserted, deleted, or sorted. A list can be changed, inserted, or deleted.
We will discus
beginning of a row is matched, ^ this special meaning is only at the beginning.$ means only the end of a row is matched\a to match only the beginning of the first line of the string ^ matches the beginning of each line\z that matches only the end of a row line string matches the first line of the line end\b Only matches the boundary example of a word: \binfo\b matches only "info" does not match information\b to match non-word boundariesExamples are as follows:>>> Print Re.compile (r "\binfo\b")
How to get a string or a number in []Http://stackoverflow.com/questions/4894069/python-regex-help-return-text-between-parenthesisRoutines:[[1.1916991]][[0.87677399]][[0.69110944]][[0.65589256]]Def readtxttolist (): Fp_read = open ("ReadTest.txt", "r") Readnum = []for line in fp_read:readnum = Line[line.find ("[") +1:li Ne.find ("]")]print Readnumreadnum = Readnum[readnum.find ("[") +1:]print Readnumfp_read.close () readTxtToList ()
This article mainly introduces 17 "pitfalls" that should be avoided by Python programmers during code writing, and 17 issues that should be avoided by Python programmers during code writing. For more information, see
1. do not use a mutable object as the default function value.
The code is as follows:
In [1]: def append_to_list (value, def_list = []):...: Def_list.append (value)...: Return def_list...:
In
This article mainly introduces Python programming in the use of closures need to pay attention to some of the places, the article from the well-known Python developers Felinx blog, need friends can refer to the
Write this blog, originating from the Tornado Mail Group of this question how to use outer variable in inner method, which the foreigner's answer is very reference value, the key points are basicall
Whether range () or xrange () is a built-in function in Python. The two built-in functions are most commonly used in the For loop. Range () and xrange () are two different implementations in Python 2.
But in Python 3, the implementation of range () is removed, the implementation of xrange () is preserved, and the Xrange () is renamed to Range ().
First, let's lo
The Industrial Light demon behind the python and Star Wars
Mentioning the Python language, many people will think of system operations, web development and other work. Few people will know that Python can also be used to make movie visual effects, including the creation of some of the movie effects of Star Wars.
The father of Star Wars, George Lucas, created the
To print the 9*9 multiplication table:>>> print (' \ n '. Join (['%s*%s=%-2s '% (y,x,x*y) for Y in range (1,x+1)]) (= x in range (1,10)]) 1*1=1 1*2 =2 2*2=4 1*3=3 2*3=6 3*3=9 1*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5 *8=40 6*8=48 7*8=56 8*8=641*9=9 A line of code calculates the prime number between 1-1000:Python
() switch.interfaceinfo (' showipint Brief ') nbsP;switch.show (' show Version ') switch.close () except (eoferror,netmikotimeoutexception): print (' Cannot connecttodevice! ')How to increase the number of subnets requires more than one layer of loops, the code changes as follows:if__name__== ' __main__ ':p rint "[+]thisprogramisbegingdone ... " #username =raw_input (' username: ') #password =getpass.getpass () ipadd=ipset ([IP (' 10.0.0.0/30 '), IP (' 10.0.1.0/30 '), IP (' 10.0.2.0/30 ')
To print the 9*9 multiplication table:Print ' \ n '. Join ([') '. Join (['%s*%s=%-2s' for in for in range (1,10)]) 1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4 *7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=4 5 6*9=54 7*9=63 8*9=72 9*9=81>>>A line of code calculates the prime number between 1-1000:>>>Print(* (i forIi
This chapter describes the
python stringTo write and deal with, before we figure out the vexing
character encodingAfter the problem, we'll study
a string of Python.
In the latest version of Python 3, strings are encoded in Unicode, meaning that Python strings support multiple languages, such as:
>>> print (' str with
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.