One,print and import information
Print ' Age : ', # # Print multiple expressions separated by commas age:22
Import somemodule # importing functions from module imports math as Foobar>>> FOOBAR.SQRT (4)2.0 from import * # Import all features from a given module
Second, assign the value
1. Sequence unpacking: The sequence of multiple values is untied
>>> values = 1,2,3>>> values (1, 2, 3)>>> x,y,z=values>> > x1>>> x,y=y,x # swapping two variables print x, y, z2 1 3
2, chain-assigned value
>>> x = y = somefunction
3. Increment Assignment
x +=1 for standard operators such as *,/,%
>>> x = 2>>> x +=1>>> x *=2>>> x6
Third, Python statements
If statement, Else statement, elif statement, conditional expression, while statement, for statement, break statement, continue statement, pass statement, iterators (iterator), list resolution
1Name = Raw_input ('What is your name?')2 ifName.endswith ('Gumby'):3 ifName.startswith ('Mr.'):4 Print 'Hello,mr,gumby'5 elifName.startswith ('Mrs.'):6 Print 'Hello,mrs,gumby'7 Else:8 Print 'Hello,gumby'9 Else:Ten Print 'Hello,stranger'
Number = input (")if and number >=1: # Boolean operator print'great'else: Print'wrong'
Assertion: instead of letting the program crash at a later time, it's better to crash the error condition as it appears.
if not Condition:crash program
assert 0 < Age < +, 'The agemust is realistic' # condition, you can add a string to explain the assertion Traceback (most recent call last ):"<pyshell#99> " in <module> assert 0 < Age < +, 'The agemust is realistic 'assertionerror:the age must being realistic
Cycle:
1. While loop
while a<B: Print A, # Note the comma at the end, which causes all output to appear on one line a+=1 1 2 3 4 5 6 7 8 9
2. For loop
Python base 8 (expressions and statements)