1.print and Import
1.1 Print slightly
1.2 Import
(1) Impore Somemodule
(2) from Somemodule import somefunction
(3) from Somemodule import somefunction,anotherfunction
(4) from Somemodule import*
(5) Import somemodule as Somename #为整个模块提供别名
(6) From Somemodule import somefunction as Somename #为某个模块的某个函数提供别名
2. Assigning values
2.1 Sequence unpacking: The sequence of multiple values is untied and then placed in the sequence of variables.
2.2 Chain assignment: Assigns the same value to multiple variables.
2.3 Incremental Assignment: Eg:x+=1
3. Statement block: A set of statements that are executed or executed multiple times when the condition is true, created by placing a space indent statement before the code.
4. Conditional statements
4.1 Boolean variable in Python false: false None 0 "" "() [] {}
4.2 If statement
4.3 Else statement
4.4 elif Statements
4.5 Nested code blocks
4.6 Items
(1) Comparison operators
(2) Equality operator = =
(3) Identity operator is
(4) Membership operator in
(5) string and sequence comparisons
(6) Boolean operators
4.7 Assertion: Assert is placed in a checkpoint to ensure that a condition must be true for the program to work properly
5. Cycle
5.1 While loop
5.2 For Loop
5.3 Looping through dictionary elements
5.4 Iteration Tools
(1) Parallel iteration: Simultaneous iteration of two sequences
(2) Iterate by index
(3) Flip and sort iterations
5.5 Jumping out of the loop
(1) Break
(2) Continue
(3) While True/break
The ELSE clause in the 5.6 loop
6. List derivation-lightweight loops
7.pass,del,exec
This chapter related code:
#-*-Coding:utf-8-*-
#1. Print and Import
#1.1 print using a comma output
print ' Age: ', #Age: 42
Name= ' xiaming '
Age= ' 42 '
Print Name,age #xiaming 42
#2. Assignment statements
#2.1. Sequence unpacking: The sequence of multiple values is untied and then placed in the sequence of the variables
x,y,z=1,2,3
Print x, y, Z #1 2 3
X,y=y,x #2 1 3
Print x, y
v=1,2,3
X,y,z=v
Print x #1
#2.2. Chaining assigns a shortcut to assigning the same value to multiple variables
X=y=4
Print x, y
#2.3. Incremental assignment
x=2
X+=1
x*=2
A= ' Bo '
a+= ' x '
a*=2
Print X,a #6, Boxbox
#3: statement block: Indent
#4. Conditional and Conditional statements
#4.1. The role of Boolean variables
#4.2 If elif Else statements
Name=raw_input (' What's your name? ')
If Name.endswith (' Ming '):
print ' hello,ming '
Elif name.endswith (' Hong '):
print ' Hello,hong '
Else
print ' Hello '
#4.3 Nested statements
Num=raw_input (' Input a number ')
If num>10:
If num<20:
print ' 10<num<20 '
Else
print ' num>=20 '
Else
print ' num<=10 '
#5. Loops
#5.1.while Cycle
X=1
While x<=10:
Print X
X+=1
Name= "
While not name:
Name=raw_input (' Enter your Name: ')
print ' hello,%s! ' %name #Hello, ming!
#5.2.for Cycle
Names=[' Ming ', ' Hong ', ' Qiang ', ' Qing ']
For N in Names:
Print n
Print range #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] rang (0,10)
For NUM in range (1,10):
Print num
#5.3. Iterate through the dictionary elements
d={' x ': 1, ' Y ': 2, ' Z ': 3}
For key in D:
Print Key,d[key] #y 2 x 1 Z 3
#5.4. Some iterative tools
#5.4.1 Parallel iterations
Names=[' Ming ', ' Hong ', ' Qiang ', ' Qing ']
AGES=[10,11,12,13]
For I in range (len (names)):
Print Names[i], ' is ', ages[i], ' years old ' #ming are years old ...
Print zip (names,ages) #[(' Ming ', x), (' Hong ', one), (' Qiang ', '), (' Qing ', 13)] compress two sequences to return a list of tuples
For n,a in Zip (names,ages):
Print N, ' is ', A, ' years old ' #ming are years old ...
#5.4.2 Iteration by index
#5.4.3 Flipping and sorting iterations
#5.5. Jumping out of the loop
#5.5.1break
From math import sqrt
For n in range (99,0,-1):
Print n
ROOT=SQRT (N)
If Root==int (root):
Print "The Biggst is%s"%n
Break
#5.5.2 Continue
#5.5.3 While True/break
While True:
Word=raw_input (' Please enter a word: ')
If not word:break
print ' The word was ' +word
#5.6 Else clause in the loop body
From math import sqrt
For n in range (99,80,-1):
Print n
ROOT=SQRT (N)
If Root==int (root):
Print "The Biggst is%s"%n
Break
Else
print "didn ' t find it"
#6 List Derivation-lightweight loops: Create new lists with other lists
print [x*x for x in range (10)] #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
print [x*x for x in range (ten) if x%3==0] #[0, 9, 36, 81]
print [(x, Y, z) for x in range (2) for Y in range (2) for z in Range (2)]
#[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1))
#7 Three statements: pass,del,exec
#7.1 Pass:
#7.2 del: Delete objects that are no longer in use
#7.3 EXEC: Executes a series of PY statements Exel: evaluates the PY expression and returns the result value
[Python Basics (iv)] conditions and conditional statements