Basic Python Tutorial Chapter 5th-Conditional loops and other statements

Source: Internet
Author: User

1. A statement block is a set of statements that executes or executes multiple times (a loop statement) when the condition is true (a conditional statement). Put a space in front of the code to indent the statement to wear a statement block. Each row in the block should be indented in the same amount. In Phyton, a colon (:) is used to identify the beginning of a statement block, and each statement in the block is indented (the same amount of indentation). When you fall back to the same amount of indentation as you have closed, it means that the current block is closed.

2, Boolean: false None 0 "" () [] {} that is, the standard value False and None, all types of number 0 (including floating-point long integers and other types) empty sequences (such as empty strings, tuples, lists) and empty dictionaries are false, everything else is interpreted as true, Includes the special value TRUE.

3. If related

Name=raw_input ("What's your name?")

If Name.endswith (' Gumby '):

print ' Hello.Mr.Gumby '

Else

print ' Hello. Stranger '

______________________________

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 '

_______________________________

  

The = = Budget character Determines whether 2 objects are equal, and is to determine whether the two are equivalent (the same object)

An Assert statement is available if you need to make sure that a condition in the program must be true for the program to function

4. Circulation

While loop

While x<10:

Print X

X=x+1

For loop

words=[' This ', ' was ', ' an ', ' ex ', ' Parrot ']

For word in words:

Print Word

Range function Range (0,5) =[0,1,2,3,4]

The zip function compresses 2 sequences together, and then returns a list of 1 tuples

Statement that jumps out of the loop

Break jumps out of the loop, ends the loop, continue the current iteration, jumps to the next cycle.

While True:/break loop

6. List derivation use other lists to create a new list

[(x, Y) for x in range (3) for Y in range (3)]

7, pass the shrimp do not execute, Python can not have empty code block, so you may add pass

Del Delete objects that are not in practical

8 exec can dynamically create a code string

Eval Evaluation Expression

Basic Python Tutorial 5th-conditional loops and other statements

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.