"Basic Python Tutorial (second edition)" Learning note statements/loops/conditions (Chapter 5th)

Source: Internet
Author: User

"Basic Python Tutorial (second edition)" Learning note statements/loops/conditions (Chapter 5th)

print ' AB ', 123 ==> AB 123 # Insert a space
print ' AB ', ' CD ' ==> AB CD # Inserted a space
Print ==> 1 2 3
Print (==>) (1, 2, 3)
#在脚本中以下ABCD连在一起输出
print ' AB ',
print ' CD '

Import Somemodule #导入模块
From Somemodule import somefunction #导入函数
From Somemodule import Function1,function2 ... #导入函数
From somemodule Import *
Module1.open () #使用模块中函数
Module2.open ()
Import Math as Foobar #为模块提供别名
FOOBAR.SQRT (4)
From math import sqrt as Foobar #为函数提供别名
Foobar (4)

Sequence Unpacking
x,y,z=1,2,3; Print x, y, z ==> 1 2 3
x,y,z=1,2,3; X,y=y,z; Print x, y ==> 2 3
x=1,2,3; A,b,c=x; Print A,b,c ==> 1 2 3
d={' key1 ': ' value1 ', ' key2 ': ' value2 '}; Key,value=d.popitem (); Print Key,value ==> key2 value2

Chained assignment
x=y=z={' key1 ': ' value1 '}; X is y ==> True
x=y=z={' key1 ': ' value1 '}; X==y ==> True

Increment assignment
X+=1; X*=1;

You can indent the code with a tab or a space, and it is recommended to indent with four spaces. tab indentation is more convenient and faster than whitespace.

: Represents the beginning of a statement block

Boolean value: True, False, BOOL ()
These are seen as False:false, None, 0, "", (), [], {}
True==1 ==> True
False==0 ==> True
True+false+4 ==> 5

BOOL (' ABC ') ==> True
BOOL (") ==> False
BOOL (3) ==> True
BOOL (0) ==> False

If expression:
Block1

If expression:
Block1
Else
Block2

If expression1:
Block1
Elif expression2:
Block2
Else
Block3

Comparison operators
X==y equality operator
X!=y Unequal operators
X is y identity operator
X is not Y
The x in Y membership operator
X not in Y

0<AGE<100 # python can be used this way

' AB ' = = ' ab ' ==> True
' AB ' = = ' CD ' ==> False
12==34 ==> False

x=y=[1,2]; X==y ==> True
x=y=[1,2]; X is y ==> True
x=y=[1,2]; z=[1,2]; X is Z ==> False
x=y=[1,2]; z=[1,2]; X==z ==> True

' ABC ' < ' abc ' ==> False
[1,2]<[2,1] ==> True

Boolean operators:
And, or, not

Assertion
Age=-1; Assert 0<age<100, ' NOTE '
Traceback (most recent):
File "<pyshell#21>", line 1, in <module>
Age=-1; Assert 0<age<100, ' NOTE '
Assertionerror:note

While expression:
Block

For item in Itemset:
Block

The range function contains a lower bound and no upper limit;
Range (0,3) ==> [0, 1, 2]
Range (3) ==> [0, 1, 2]

Traverse Dictionary
d={' key1 ': ' value1 ', ' key2 ': ' value2 '};
For key in D:
Print Key,d[key]

For Key,value in D.items ():
Print Key,value

Parallel iterations
a=[1,2,3]; B=[' A ', ' B ', ' C ']; Zip (b) ==> [(1, ' a '), (2, ' B '), (3, ' C ')]

Numbering iterations
For index, string in Enumerate (strings):
If ' xxx ' in string:
strings[index]= ' NewValue '

Invert and sort iterations
Sorted ([3,1,2]) ==> [1, 2, 3]
x=[1,2,3]; List (reversed (x)) ==> [3, 2, 1] # reversed returns an iterative object

Break End Loop
Continue for the next cycle

While True:
Block1
If expression:
Break
Block2

For item in items:
Block1
If expression:
Break
Block2
Else
BLOCK3 # When the for execution is complete and no break has been performed, the BLOCK3 is executed at this time

List-derived
[X*x for X in range (3)] ==> [0, 1, 4]
[X*x for X in range (3) if x%2==0] ==> [0, 4]
[(x, Y) for x in range (3) for Y in range (3)] ==> [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2), 1) ]

Pass # Placeholder, similar to an empty statement
Del # Delete Object
EXEC # Executes a string
EXEC "print ' abc '" ==> ABC
from math import sqrt; scope={}; exec ' sqrt=1 ' in scope; sqrt (4); scope[' sqrt '];

Eval evaluation, similar to exec
Eval (Raw_input (""))
Up
2

scope={}; scope[' x ']=2; scope[' y ']=3; Eval (' x*y ', scope) ==> 6

"Basic Python Tutorial (second edition)" Learning note statements/loops/conditions (Chapter 5th)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.