Python notes (3)

Source: Internet
Author: User

Basic operation expression of Python

(1) Judgment statement

I will not introduce the rules of if statements. I will only introduce the usage and features of IF Statements in Python.

#! /Usr/bin/python <br/> # filename: If. PY </P> <p> Number = 23 <br/> guess = int (raw_input ('enter an integer :')) </P> <p> If guess = Number: <br/> Print 'Congratulations, you guessed it. '# new block starts here <br/> Print "(but you do not win any prizes !) "# New block ends here <br/> Elif guess <number: <br/> Print 'no, it is a little higher than that '# Another block <br/> # You can do whatever you want in a block... <br/> else: <br/> Print 'no, it is a little lower than that '<br/> # You must have guess> number to reach here </P> <p> Print 'done' <br/> # This last statement is always executed, after the if statement is executed </P> <p>

 

The output is as follows:

 

$ Python if. PY <br/> enter an integer: 50 <br/> NO, it is a little lower than that <br/> done <br/> $ Python if. PY <br/> enter an integer: 22 <br/> NO, it is a little higher than that <br/> done <br/> $ Python if. PY <br/> enter an integer: 23 <br/> congratulations, you guessed it. <br/> (but you do not win any prizes !) <Br/> done </P> <p>

Here, we use a function raw_input () to obtain user input.

For the built-inraw_inputThe function provides a string that is printed on the screen and waits for user input. Once we enter something and press the Enter key, the function returns the input. Forraw_inputFunction is a string. We passintConvert the string to an integer and store it in a variable.guess. In fact,intIs a class, but all you need to know about it is that it converts a string to an integer.

 

Note: The indentation level is used to tell which block each statement belongs to in Python. This is why indentation is so important in Python.

Not in PythonswitchStatement. However, you can useif..elif..elseStatement.

 

 

(2) Cyclic statements

 

1. While statement

The while statement of python is not much different from the while statement of other languages.

#! /Usr/bin/python <br/> # filename: While. PY </P> <p> Number = 23 <br/> running = true </P> <p> while running: <br/> guess = int (raw_input ('enter an integer: ') </P> <p> If guess = Number: <br/> Print 'Congratulations, you guessed it. '<br/> running = false # This causes the while loop to stop <br/> Elif guess <number: <br/> Print' No, it is a little higher than that '<br/> else: <br/> Print 'No, it is a little lower than that' <br/> else: <br/> Print the while loop is over. '<br/> # Do anything else you want to do here </P> <p> Print 'done'

 

Output:

 

$ Python while. PY <br/> enter an integer: 50 <br/> NO, it is a little lower than that. <br/> enter an integer: 22 <br/> NO, it is a little higher than that. <br/> enter an integer: 23 <br/> congratulations, you guessed it. <br/> the while loop is over. <br/> done </P> <p>

 

Note: In python, you canwhileUseelseClause.

 

2. For statement

For statement, here, I use an example to explain

#! /Usr/bin/python <br/> # filename:. PY </P> <p> for I in range (1, 5): <br/> print I <br/> else: <br/> Print 'the for loop is over'

 

Output:

$ Python. PY <br/> 1 <br/> 2 <br/> 3 <br/> 4 <br/> The for loop is over </P> <p>

 

Note:elseSome are optional. If else is included, it is always inforRun the command once after the loop ends, unlessBreakStatement.

Compared with C/C ++, Python's If statement is much simpler.

 

(3) Other statements

1. Break statement

The break statement is used in the for loop and while loop.

While true: <br/> S = raw_input ('enter something: ') <br/> If S = 'quit ': <br/> Break <br/> Print 'length of the string is ', Len (s) <br/> Print 'done'

 

The output is

$ Python break. PY <br/> enter something: programming is Fun <br/> length of the string is 18 <br/> enter something: when the work is done <br/> length of the string is 21 <br/> enter something: If you wanna make your work also fun: <br/> length of the string is 37 <br/> enter something: Use python! <Br/> length of the string is 12 <br/> enter something: Quit <br/> done </P> <p>

 

 

2. Continue statement

Here, I will illustrate it with only one example.

#! /Usr/bin/python <br/> # filename: continue. PY </P> <p> while true: <br/> S = raw_input ('enter something: ') <br/> If S = 'quit ': <br/> Break <br/> If Len (s) <3: <br/> continue <br/> Print 'input is of sufficient length' <br/> # Do other kinds of processing here... </P> <p>

 

Output:

$ Python continue. PY <br/> enter something: A <br/> enter something: 12 <br/> enter something: ABC <br/> input is of sufficient length <br/> enter something: Quit

 

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.