Conditional statements
Some programs are written in/usr/bin python, which indicates that the Python interpreter is inside the/usr/bin. However, if written as/usr/bin/env, it means looking for the Python interpreter through the system search path. Different systems, can be
The location of the interpreter can be different, so this way can make the code more portable.
#/usr/bin/env python#Coding=utf-8Number = Int (Raw_input ("Please enter any one of the integers:"))ifNumber = = 10: Print "the number you entered is:%d"% NumberelifNumber > 10: Print "This was more than."elifNumber < 10: Print "This was less than."Else: Print "Is you a human?"
Ternary operator
Ternary operation, is a relatively concise method of assignment in conditional statements, it looks like this:
" 5 " if Else 3>>> a'5'"5" if Else 3>>> a3
Looping statements
For
# /usr/bin/env pythonfruits=["Apple","Orange", " Banana " ] for in fruits: Print i
Multiple
a=[, (3,4), (5,6)]d=[] for in A: d.append (x+y) Print D
While
#!/usr/bin/env python
#coding: Utf-8
A = 9
While a:
If a%2 = = 0:
Break
Else
Print "%d is odd number"%a
A = 0
Print "%d is even number"%a
While...else
Once you encounter the else, it means that it is no longer within the while loop.
# !/usr/bin/env Pythoncount = 0 while count < 5:print' is Less than 5"= count + 1else:print" was not less than 5"
For...else
This loop is also commonly used when jumping out of a loop to do something.
# !/usr/bin/env python # Coding=utf-8 from math import sqrt for n in Range (1, -1 = sqrt (n) if root == int (root): print n break else : print " nothing.
Python Learning Note 8-statements