Python Learning Notes (statement)

Source: Internet
Author: User

For more information on print and import, use the comma output

>>> print ' Age: ', 42
Age:42

>>>
(1, 2, 3)
>>> Print
1 2 3
>>> print (+/-)
(1, 2, 3)

>>> name= ' Gumby '
>>> salutation= ' Mr. '
>>> greeting= ' Hello, '

>>> print greeting, salutation, name
Hello, Mr Gumby.

print ' Hello, ',
print ' world! '

------------------

Hello, world!.

To import something as another thing

When importing functions from a module, you can use the

Import Somemodule

Or

From Somemodule import somefunction

Or

Form Somemodule Import Somefunction.anotherfunction.yetanotherfunction

Or

Form Somemodule Import *

Two modules have the same function, you can use an alias

>>> import Math as foobar# to provide aliases for modules
>>> Foobar.sqrt (4)
2.0
>>> from math import sqrt as Foobar #为函数提供别名
>>> Foobar (4)
2.0
>>> from Module1 import open as Open1

>>> from module2 import open as Open2

Assignment Magic Sequence Unpacking

>>> x,y,z=1,2,3
>>> print x, Y, Z
1 2 3
>>> x,y=y,x
>>> print x, Y, Z
2 1 3

>>> values=1,2,3
>>> values
(1, 2, 3)
>>> x,y,z=values
>>> x
1
>>> scoundre={' name ': ' Robin ', ' girlfriend ': ' Marion '}
>>> Key,value=scoundre.popitem ()
>>> Key
' Girlfriend '
>>> value
' Marion '
>>>

Chained assignment

X=y=somefunction ()

Equivalent to

X=somefunction ()

Y=x

Increment assignment

>>> x=2
>>> x+=1
>>> x*=2
>>> x
6
>>> fnord= ' foo '
>>> fnord+= ' Bar '
>>> fnord*=2
>>> Fnord
' Foobarfoobar '
>>>

Conditional statements

False None 0 "" () [] {} is false and the rest is interpreted as true

>>> True
True
>>> False
False
>>> true==1
True
>>> false==0
True
>>> true+false+42
43

>>> BOOL (' I think, therefore I am ')
True
>>> BOOL (42)
True
>>> bool (')
False
>>> BOOL (0)
False

If statement

Name=raw_input (' What's your name? ')
If Name.endswith (' Gumby '):
print ' Hello,mr.gumby '

ELSE clause

Name=raw_input (' What's your name? ')
If Name.endswith (' Gumby '):
print ' Hello,mr.gumby '
Else
print ' Hello,stranger '

elif clause

Num=input (' Enter a number: ')
If num>0:
print ' The number is positive '
Elif num<0:
print ' The number is negative '
Else
print ' The number is zero '

Nested code blocks

Name=raw_input (' What's your name? ')
If Name.endswith (' Gumby '):
If Name.startswith (' Mr. '):
print ' Hello,mr.gumby '
Elif name.startswith (' Mrs. '):
print ' Hello,mrs.gumby '
Else
print ' Hello,gumby '
Else
print ' Hello,stranger '

Assertion

Use keyword assert in statement

>>> age=10
>>> assert 0<age<100
>>> Age=-1
>>> assert 0<age<100

Traceback (most recent):
File "<pyshell#3>", line 1, in <module>
Assert 0<age<100
Assertionerror
>>> Age=-1
>>> assert 0 <age<100, ' The must be realistic '

Traceback (most recent):
File "<pyshell#5>", line 1, in <module>
Assert 0 <age<100, ' The must be realistic '
Assertionerror:the must be realistic

Loop while loop

X=1
While x<=100:
Print X
X+=1

Name= "
While not name:
Name=raw_input (' Please enter your name: ')
print ' hello,%s! ' %name

For

words=[' This ', ' was ', ' an ', ' ex ', ' Parrot ']
For word in words:
Print Word

numbers=[0,1,2,3,4,5,6,7,8,9]
For number in numbers:
Print number

Range (10)

-----------------------

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

For number in range (1,101): #包含下限, with no upper bound
Print number

-----------------------

1

2

.

.

100

Range and Xrange

>>> A=range (0,100)
>>> print Type (a)
<type ' list ' >
>>> Print a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 3, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> Print A[0],a[1]
0 1
>>> A=xrange (0,100)
>>> print Type (a)
<type ' xrange ' >
>>> Print a
Xrange (100)
>>> Print A[0],a[1]
0 1
>>>

Traverse Dictionary

d={' x ': 1, ' Y ': 2, ' Z ': 3}
For key in D:
Print key, ' corresponds to ', D[key]

---------------------

>>>
Y corresponds to 2
x corresponds to 1
Z corresponds to 3

d={' x ': 1, ' Y ': 2, ' Z ': 3}
For Key,value in D.items ():
Print key, ' corresponds to ', value

--------------------------------

Y corresponds to 2
x corresponds to 1
Z corresponds to 3

Some iterative tools parallel iterations

names=[' Anne ', ' Beth ', ' George ', ' Damon '
AGES=[12,45,32,102]
For I in range (len (names)):
Print Names[i], ' is ', ages[i], ' years '

----------------------------

>>>
Anne is-years old
Beth is years old
George is years
Damon is 102 years old
>>>

The ZIP function is used for parallel iterations

>>> names=[' Anne ', ' Beth ', ' George ', ' Damon '
>>> ages=[12,45,32,102]
>>> Zip (names,ages)
[(' Anne ', '), (' Beth ', '), (' George ', ' + '), (' Damon ', 102)]

>>> for name,age in Zip (names,ages):
Print name, ' is ', age, ' years '


Anne is-years old
Beth is years old
George is years
Damon is 102 years old
>>> Zip (Range (5), xrange (10000000))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
>>>

Numbering iterations

>>> index=0
>>> for string in strings:
If ' xxx ' in string:
strings[index]=[' censored ']
Index+=1

Using the built-in function enumerate

>>> for index,string in Enumerate (strings):
If ' xxx ' in string:
strings[index]=[' censored ']

Flip and sort iterations

>>> sorted ([4,3,5,8,6])
[3, 4, 5, 6, 8]
>>> sorted (' hello,world! ')
['! ', ', ', ' d ', ' e ', ' h ', ' l ', ' l ', ' l ', ' o ', ' o ', ' R ', ' W ']
>>> list (Reversed (' hello,world! '))
['! ', ' d ', ' l ', ' r ', ' O ', ' w ', ', ', ' o ', ' l ', ' l ', ' e ', ' H ']
>>> ". Join (Reversed (' hello,world! '))
'!dlrow,olleh '
>>>

Jump out of Loop break

>>> from math import sqrt
>>> for N in range (99,0,-1):
ROOT=SQRT (N)
If Root==int (root):
Print n
Break


81
>>>

Continue

For x in SEQ:
If Condition1:continue
If Condition2:continue
If Condition3:continue

Do_someting ()

Do_someting_else ()

Do_another_thing ()

ECT ()

While True/break

>>> word= ' Dummy '
>>> while Word:
Word=raw_input (' Please enter a world: ')
print ' The word was ' +word


Please enter a World:first
The word was first
Please enter a World:sencond
The word was Sencond
Please enter a World:third
The word was third
Please enter a world:dummy
The word was dummy
Please enter a world:
The word was
>>>
>>> while True:
Word=raw_input (' Please enter a word: ')
If not word:break
print ' The word was ' + word


Please enter a word:d
The word wasd
Please enter a word:
>>>

Else clause in a loop

>>> from math import sqrt
>>> for N in range (99,81,-1):
ROOT=SQRT (N)
If Root==int (root):
Print n
Break
Else
print "didn ' t find it!"


Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
Didn ' t find it!
>>>

List derivation--lightweight loops

>>> [x*x for X in range (10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [x*x for x in range (ten) if x%3==0]
[0, 9, 36, 81]

>>> [(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)]
>>> result=[]
>>> for x in range (3):
For y in range (3):
Result.append ((x, y))

>>> List (Result)
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1)]
>>>

Pass

If name== ' Ralph Auldus melish ':
print ' welcome! '
Elif name== ' Enid ':
Pass
Elif name== ' Bill Gates ':
print ' Access Denied '

Del

>>> x=[' hello ', ' world '
>>> y=x
>>> y[1]= ' Pyhton '
>>> x
[' Hello ', ' Pyhton ']
>>> del X
>>> y
[' Hello ', ' Pyhton ']

Python Learning Notes (statement)

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.