Learn python (5) Step by Step-conditional loops and other statements

Source: Internet
Author: User

1. print

It is also feasible to use a comma to output-print multiple expressions, but separate them with commas.

>>> Print 'chentongxin', 23

SyntaxError: invalid syntax

After python3.0, print is no longer a statement, but a function. Brackets must be added to the function.

>>> Print ('chentongxin', 23)
Chentongxin 23
>>> 1, 2, 3
(1, 2, 3)
>>> Print 1, 2, 3
SyntaxError: invalid syntax
>>> Print (1, 2, 3)
1 2 3


2. import one thing as another

Import somemodule

From somemodule import somefunction

From somemodule import somefunction, anotherfunction ....

From somemodule import *

3. Sequential unpacking

Assign multiple values at the same time

>>> X, y, z = 1, 2, 3
>>> Print (x, y, z)
1 2 3

Exchange variable

>>> X, y = y, x
>>> Print (x, y, z)
2 1 3

Popitem: Get the key-value pairs in the dictionary

4. chained assignment

X = y = somefunction ()

5. Incremental assignment

>>> X = 2
>>> X + = 1
>>> X * = 2
>>> X
6

String type is also applicable
>>> Str = 'str'
>>> Str + = 'in'
>>> Str * = 2
>>> Str
'Stringstring'

6. Statement Block

A group of statements executed or executed multiple times when the actual condition of the statement block is true. Place spaces in the code to indent the statement block.

In Python, colon (:) is used to identify the beginning of the statement block. Every statement in the block is indented. When it is rolled back to the same indentation as the closed one, it indicates that the current block is over.

7. Condition and condition statements

False None 0 "" () [] {} these symbolic interpreters are treated as false, and everything else is true.

If and else usage

Name = input ('What is your name? ')

If name. endswith ('gumby '):
Print ('hello, mr, gumby ')
Else:
Print ('hello, stranger ')
Elif usage:

8. nested code blocks

If statements can be nested.

9. More Complex Conditions

Comparison Operators

Equal Operators

Identity operator is

Membership operator in

String and Sequence Comparison

10. Assertions

Assert

11. Loop

While Loop

Name =''
While not name:
Name = input ('Please enter your name :')
Print ('Hell, % s' % name)

For Loop

Words = ['on', 'is ', 'any', 'x', 'Parrot']
For word in words:
Print (word)

12. Some iteration tools

Parallel Iteration

Number Iteration

13. Skip Loop

Break

Continue

14. else clause in a loop

From math import sqrt
For n in range (99,80,-1 ):
Root = sqrt (n)
If root = int (root ):
Print (n)
Break
Else:
Print ("Didn't find it ")

15. List derivation-lightweight cycle

The list derivation is to use another list to create a new list.

[X * x for x in range (10)]

16. Nothing happened

The pass program does nothing.

17. Delete with del

18. execute and evaluate strings using exec and eval






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.