1.bool Variables and operations
Print 1 + 1 = = 2
Print 1 + 1! = 2
Print 1 + 1 = = 2 and 1 + 1 = = 3
Print 1 + 1 = = 2 or 1 + 1 = = 3
Print not 1 + 1 = = 2
print 1 in [1, 2, 3] Output:True
False
False
True
False
True 2.ifLight = "Red"
if light = = "Red":
print "stop!"
elif Light = = "Green":
print "go!"
elif Light = = "Yellow":
print "Slow down!" 3.whileA =While a<100:a = a +1Print a 4. for in
Contact = {"Li Lei": "1303030", "Hanmeimei": "1450303"}for i in Contact: print I, Contact[i]
Output:
"Li Lei": "1303030"
"Hanmeimei": "1450303"
5.break Continuethe meaning is the same as most other languages 6. Custom Functionsdef addnum (num):
A = 0
For i in Num:
A = a + I
return a
num = [1, 3, 4, 5, 9]
print addnum (num) 7. Function Scopevery similar to C + + x = 0
y = 0
def changex ():
Global x//global can use variables outside the function inside a function
x = Ten
y = ten
Print x
Print y
Changex ()
Print x
Print y 8.importImport Math//Importing package
Print Math.PI
Print dir (math)//dir can view all the methods of a package
Python Basic Concepts 2