With the above basic basis, has been written on a few small exercises, we certainly have a lot of dissatisfaction, such as query why query once the exit? Let's learn the conditional statements below.
One, the evil of the plus
We used to use + to connect as a variable when we were in print, but it's not good to do it.
Because when the plus sign is used, new memory addresses are opened in memory to hold new content the disadvantage of doing this is to increase the memory consumption.
650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M00/7A/B8/wKiom1axuYPyHD0UAACkybVUrTM809.png" width= "547" height= "280"/>
So what do we do?
(1) string formatting
① built-in placeholders
Python supports the output of formatted strings. Although this may use a very complex expression, the most basic usage is to insert a value into a string that has the string format of%s. In Python, string formatting uses the same syntax as the sprintf function in C.
Class as in the above example we change the way:
>>> a = "abc"
>>> print ("sssss%s"% (a))
Sssssabc
We're mostly here.%s Here%s We call this a placeholder, we have a lot of placeholders for data types
650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M01/7A/B8/wKiom1axuYWDWGP0AACl3qvXTig705.png" width= "448" height= "402"/>
② Custom placeholder Format function
AA = "Dsadsa{ss}" Print (Aa.format (ss= ' CCCCC '))
Dsadsaccccc
The ss= ' CCCCC ' in print can be replaced by variables so that you don't have to think about the type of data.
substitution of values for two or two variables
In some algorithms we need to replace the value of the variable
Method 1:
>>> A = 1
>>> B = 2
>>> C = A
>>> A = b
>>> B = C
>>> print (A, B)
2 1
So we use another variable to temporarily relay the value of a so that we have a variable that does not look high-end then is there another way?
Method 2:
>>> A = 1
>>> B = 2
>>> print (A, B)
1 2
>>> B = b,a
>>> print (A, B)
2 1
Isn't it fun?
Three, Boolean expression
The Boolean expression is to judge the true and false, that is, the machine code of the computer is represented by 1 and the number of lines, so 1 indicates that there is also true, 0 means no or false.
Let's take a look at the following example:
From the following example can be seen 1 equals true when the return value is true true is right, when 0 equals True when the return value is False is wrong, when 0 is false, the return value is true is correct.
>>> 1 = = True
True
>>> 0 = = True
False
>>> 0 = = False
True
Example 2: The following example also knows that when the value is empty, the Boolean expression is recognized as a false
>>> bool (')
False
>>> bool (' a ')
True
Iv. If statement
(1)If statements in Python are used as logical judgments as in other languages, for example, we want to implement a
Demand (1):
If the user enters a value of a then output welcome, otherwise output tumbling bar xxx
Inpot = input ("Please enter name:") if inpot = = "a": Print (' Welcome:%s '% (inpot)) else:print (' Roll it:%s '% (Inpot))
Demand (2):
If the user enters a value of a then the output welcome, if the user entered a value of B is output welcome Director inspection, otherwise output tumbling bar xxx
Inpot = input ("Enter name:") if inpot = = "a": Print (' Welcome:%s '% (inpot)) elif inpot = "B": Print (' welcome Director [%s] inspection '% (Inpot)) Else: Print (' Roll it:%s '% (Inpot))
As you can see from the two examples above, Python does not have a case in the shell script and becomes a elif in the IF statement where we can use only if instead of else.
V. While loop
Why there is a while loop, because we need to do something repetitive, such as we want to print 1-100 installation commonsense we don't need print 1 Print 2 ... Print100 is a very wasteful code, so we can do this.
(1) While loop-counter
x = 1 #先设置一个变量的值为1while x <=: #当x的值小于等于100时运行下面的代码 print (x) #打印x的值 x = X+1 #每次循环x都加1, the first cycle is X (1) +1= 2 The second time is X (2) +1=3 and so on
Such statements are called counters
(2) While Dead loop
We know the truth in Boolean expressions, where we can combine with while to become a dead loop, so the dead loop is not always running,
What to do, Python provides break can exit the current loop, we combine the previous practice to try
Exercise 1:
Requirements: Write an input employee name can be queried to the employee's phone and number of the program, the request can be continuous query, unless the user entered the exit command to end
python3.5 Environment
#!/usr/bin/env python#-*-coding:utf-8-*-
address = { ' A ':{ ' number ': ' 01 ', ' phone ': ' + ' }, ' B ': { ' number ': ', ' phone ': ' 119 ' }, ' C ':{ ' number ': ' 03 ', ' phone ': ' }}while True: inpu = input ("Please enter the user name of the query:") if inpu in Address.keys (): #address. Keys () means to get all keys for the dictionary (key) print ("Current User:%s"% (INPU)) print (' User id:%s '% (address[inpu][' number ')) print (' User phone is: %s '% (address[inpu][' phone ')) elif inpu == "Exit" : Print (' Thank you for using goodbye ') break else: print ("The user you entered [%s] does not exist"% (INPU))
Six, for Loop
(1) First knowledge for loop
While statements are very flexible, but some specific conditions are very complex to use while loops, such as having a list that wants to print the contents of the list separately, and can be implemented with while, but without a for loop so simple,
Let's compare we have a list of a = [' A ', ' B ', ' C ', ' d '] we use while and for to print out each element of the list, respectively.
While
A = [' A ', ' B ', ' C ', ' d ']x = 0while x < Len (a): The value of #len (a) is 4, then the index of List A is from 0-3 so here is x less than Len (a) print (a[x]) x = x+1
For
A = [' A ', ' B ', ' C ', ' d ']for i in A:print (i)
After comparison, at least two lines of code are missing for the For loop.
For-I in A For loop code: represents I from the beginning to the a sequence of values, so the first loop i = a second time I = B and so on
(2) Range function
Remember the while loop printing 1-100, where we can use the For Plus range function to:
For I in Range (1,101): #为什么这里写1-101, because of the properties of the range function, if it is 1-100 he will generate 1-99 print (i) #所以这里是1-101
The range function works like a shard
(3) for Loop dictionary
Note: The order of the dictionary elements is undefined, that is, the key-value pairs of the dictionaries that you have for the loop are not sequential.
Method 1:
A = {"A": "AA", "B": "BB", "C": "CC"}for key in A:print (' Welcome "%s", your information is (%s) '% (Key,a[key))
Method 2:
A = {"A": "AA", "B": "BB", "C": "CC"}for Key,value in A.items (): Print (' Welcome '%s ', your information is (%s) '% (Key,value))
Results 650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M00/7A/B8/wKiom1axuYeBgKpDAAANz70OiTo482.png" width= "239" Height= "/>"
The second release uses the items function, which translates dictionary a into ([' B ', ' BB '), (' A ', ' AA '), (' C ', ' CC ')]) and this structure is assigned to key and value respectively.
Vii. jumping out of the loop
(1)break, previously introduced jump out of the current loop
(2) Contiune is generally used in the IF statement, that is, after entering the current judgment, there is no need to continue to judge to use it
Example:
For I in Range (1,6): if i = = 3:print (' OK ') continue #当i等于3时, output OK and end this judgment is not to do else else: Print (i)
Results: 650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M02/7A/B8/wKiom1axuYjwCXFKAAAWc5JDauI330.png" height= "196" />
(3) Pass
Just like the meaning of English, don't do anything.
For I in Range (1,6): if i = = 3:print (' OK ') continue elif i = = 4:pass Else:print ( I
Results 650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M01/7A/B8/wKiom1axuYjhxRIIAAAJdpIoR00948.png" height= "137" />
Viii. arithmetic
There are a lot of operations in Python recommended sites here: http://www.runoob.com/python/python-operators.html
Python
Arithmetic Operators
650) this.width=650; "title=" clip_image002 "style=" Border-top:0px;border-right:0px;border-bottom:0px;border-left : 0px; "border=" 0 "alt=" clip_image002 "src=" http://s3.51cto.com/wyfs02/M02/7A/B8/ Wkiom1axuymib2qtaacndu87sts483.jpg "height=" 290 "/>
Python
comparison Operators
650) this.width=650; "title=" clip_image004 "style=" Border-top:0px;border-right:0px;border-bottom:0px;border-left : 0px; "border=" 0 "alt=" clip_image004 "src=" Http://s3.51cto.com/wyfs02/M00/7A/B8/wKiom1axuYrh_ Rc0aaces1bfi2s082.jpg "height=" 252 "/>
Python
Assignment Operators
650) this.width=650; "title=" clip_image006 "style=" Border-top:0px;border-right:0px;border-bottom:0px;border-left : 0px; "border=" 0 "alt=" clip_image006 "src=" http://s3.51cto.com/wyfs02/M01/7A/B8/ Wkiom1axuyvrwehwaaceawibbaq251.jpg "height=" 299 "/>
Python
Bitwise Operators
650) this.width=650; "title=" clip_image008 "style=" Border-top:0px;border-right:0px;border-bottom:0px;border-left : 0px; "border=" 0 "alt=" clip_image008 "src=" http://s3.51cto.com/wyfs02/M01/7A/B8/ Wkiol1axudyb8lpjaacntjksjbg901.jpg "height=" 254 "/>
Python
logical Operators
650) this.width=650; "title=" clip_image002[6] "style=" border-top:0px;border-right:0px;border-bottom:0px; border-left:0px; "border=" 0 "alt=" clip_image002[6] "src=" http://s3.51cto.com/wyfs02/M02/7A/B8/ Wkiol1axud3dr5etaabmpqla5ze449.jpg "height=" 137 "/>
Python
member Operators
650) this.width=650; "title=" clip_image004[6] "style=" border-top:0px;border-right:0px;border-bottom:0px; border-left:0px; "border=" 0 "alt=" clip_image004[6] "src=" http://s3.51cto.com/wyfs02/M02/7A/B8/ Wkiol1axud6gr5kzaabgog8keze759.jpg "height="/>
Python
identity operator
650) this.width=650; "title=" clip_image006[5] "style=" border-top:0px;border-right:0px;border-bottom:0px; border-left:0px; "border=" 0 "alt=" clip_image006[5] "src=" http://s3.51cto.com/wyfs02/M02/7A/B9/wKiom1axuY6zp_ 7maabmudqqoky901.jpg "height=" 137 "/>
Python
Operator Precedence
650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "http://s3.51cto.com/wyfs02/M00/7A/B9/wKiom1axuZCjvrunAAGYTdxReIM069.png" height= "478" />
Python's Path to Growth first (4) _if,for,while conditional statements