Python the next day

Source: Internet
Author: User

First, Process Control

1. Conditional statements

1.1 Single Branch

1.2 Multi-Branch

Conditional judgment

Computers can do a lot of automated tasks, because it can make their own conditions to judge.

A = 5if a > 2:    print (' Yes ')

According to the indentation rules of Python, if the if statement is judged True , the indented two lines of the print statement are executed, otherwise, nothing is done.

It is also possible to if add a else statement, meaning that if the if judgment is False , do not execute if the content, go to put the else executed:

A = 5if a > 8:    print (' yes ') Else:    print (' No ')

Be careful not to write a colon less : .

Of course, the above judgment is very sketchy, can be used to elif do more detailed judgment:

A = 5if a > 8:    print (' yes ') elif a > 3:    print (' no ') Else:    print (' Bey ')

elifis else if the abbreviation, can have more than one elif , so if the complete form of the statement is:

If < condition judgment 1>:    < execution 1>elif < conditional judgment 2>:    < execution 2>elif < conditional judgment 3>:    < execution 3>else:    < Executive 4>

ifStatement execution has a characteristic, it is to judge from the top, if in a certain judgment True , the statement corresponding to the execution of the sentence, will ignore the remaining elif andelse

ifThe judging conditions can also be abbreviated, such as writing:

If x:    print (' True ')

As long as a non- x 0 value, a non-empty string, a non-empty list, etc., it is judged True , otherwise False .

2. Cycle

There are two types of Python loops, one is the for...in loop, and each of the elements in the list or tuple is iterated in turn to see an example:

For I in Range (0,5):    print (i) Else:    print (' A For loop was over ')
names = [' Michael ', ' Bob ', ' Tracy ']for name in Names:    print (name)

So the for x in ... loop is to take each element into the variable and x then execute the indented block of the statement.

For example, if we want to calculate the sum of 1-100 integers, we can use a sum variable to accumulate:

sum = 0for x in range (101):    sum+=xprint (SUM)

Exercise: Rooster 5 Text Money A, hen 3 text money, chicken 3 only a penny, with 100 money to buy 100 chickens, where the rooster, hen, chicken must have, ask the rooster, hen, chicken to buy how many just just fill up 100 money.

For x in range (1,20): A for    y in range (1,33):        z = 100-x-y        if (z% 3 = = 0) and (x * 5 + y * 3 + Z/3 = =): 
   s = "Rooster:%d; hen:%d; chick:%d;"% (x, y, z)            print (s)

Results:

Rooster: 4; Hens: 18; Chickens: 78 cocks: 8; Hens: 11; Chicks: 81 Cocks: 12; hens: 4; Chickens: 84

The second loop is the while loop, which, as long as the condition is satisfied, loops continuously, exiting the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:

sum = 0n = 99while n > 0:    sum + = n    n = n-2print (sum)

The loop internal variable is n continuously self-reducing until -1 it becomes, and the while condition is no longer satisfied, and the loop exits.

Cycle reading: 516621 cycles

To calculate 1+2+3, we can write an expression directly:

>>> 1 + 2 + 36

To calculate 1+2+3+...+10, you can barely write it.

However, to calculate 1+2+3+...+10000, it is impossible to write the expression directly.

In order for the computer to calculate thousands of repetitions, we need to loop the statements.

There are two types of Python loops, one is the for...in loop, and each of the elements in the list or tuple is iterated in turn to see an example:

names = [‘Michael‘, ‘Bob‘, ‘Tracy‘]for name in names: print(name)

Executing this code will print names each element in turn:

MichaelBobTracy

So the for x in ... loop is to take each element into the variable and x then execute the indented block of the statement.

For example, if we want to calculate the sum of 1-10 integers, we can use a sum variable to accumulate:

0for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + xprint(sum)

If you want to calculate the sum of 1-100 integers, writing from 1 to 100 is a bit difficult, but fortunately Python provides a range() function that generates a sequence of integers that list() can then be converted to a list by a function. For example, range(5) the generated sequence is an integer starting from 0 and less than 5:

list(range(5))[0, 1, 2, 3, 4]

range(101)You can generate an integer sequence of 0-100, which is calculated as follows:

0for x in range(101):    sum = sum + xprint(sum)

Please run the above code on your own, to see if the result is 5050 of the students ' mental arithmetic.

The second loop is the while loop, which, as long as the condition is satisfied, loops continuously, exiting the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:

0n = 99while n > 0:    sum = sum + n    n = n - 2print(sum)

The loop internal variable is n continuously self-reducing until -1 it becomes, and the while condition is no longer satisfied, and the loop exits.

Break

In a loop, the break statement can exit the loop prematurely. For example, a number that would have been circulating to print 1~100:

n = 1while n <=:    if n > 10: # when n = 11 o'clock, condition is met, execution break statement break        # break statement ends current loop    print (n)    n + = 1prin T (' END ')

Execute the above code can see, after printing out 1~10, immediately after printing END , the program ends.

breakthe visible effect is to end the loop prematurely.

Continue

In the loop process, you can also continue skip the current loop and start the next loop directly through the statement.

n = 0while N <:    n + = 1    if n 2 = = 0: # If n is an even number, execute continue statement        Continue # Continue statement will proceed directly to the next cycle, subsequent print ( ) statement does not execute    print (n)

Execute the above code to see that the print is no longer 1~10, but 1,3,5,7,9.

The visible continue effect is to end the cycle in advance and start the next cycle directly.

Summary: Break is used to exit this layer loop; Continue is used to exit this loop and continue the next cycle

Python the next day

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.