Python the next day

Source: Internet
Author: User

1.while Cycle

Format: While conditional expression:

Loop Body Statement 1

Loop Body Statement 2

Printing 1 to 10

1 number = 12 while number < one:3     print(number) 4     Number = number + 1
5 Print (number)

Note: Python uses the same indentation to represent a block of code, so 3 rows and 4 rows are the entire loop body statement of the while loop,

The process of running the program, 1. Number is assigned a value of 1 as the initial value

2. Execute while loop condition Expression Judge number < 11, at this time number=1, so 1<11 is true

3. The while condition expression is true, the loop body statement is executed, the number is printed, the console prints 1,

4. Continue execution of line 4th, number +1, after

5. After executing the Loop body statement, the 2nd line of code is executed again, and the conditional expression is judged, at this point 2 < 11, is true, so the loop body statement continues

6. This is the loop execution process, number of the value, each time the execution is added 1, until number = 11 o'clock, execute one < 11 is false, exit the loop, do not execute the Loop body statement

7. Execute the 5th line of code and print the value of the last number is 11

2. While loop body statement, use the break keyword

Number = 1 while True:     print (number) Number number     = number + 1     if number = =        #: Break  print (number)

If the condition is expressed as true, the loop body statement is always executed, which is certainly unreasonable.

The break statement is the instruction that exits the loop

When number = 10 o'clock, a break is performed to indicate an exit loop.

3. While else format

While conditional expression:

Loop Body Statement 1

Else

Statement 1

The Else statement is executed when the while loop executes the end, that is, exits the loop

Note: When you exit a loop by using the break statement in the while loop body, the Else statement is not executed

4. Operators

Plus + minus-multiply * divide/withdraw% power * * Rounding//

Count = 1

Count = Count + 1

Count = Count * 1

For data such as this pair of variables, after the operation, and then re-copied to the action of the variable,

Provides a shortcut + = = *=/=%= **=//=

Count + = 1

Count *= 1

5. Comparison operators

equals = = is not equal to! = (<> less)

Greater than > less than <

Greater than or equal to >= less than or equal to <=

6. Logical operators

Not and OR

Not non-arithmetic

And is true for both the operation and the true result

Or OR operation, a true result is true

Note Precedence of operators () > Not > and > or

For example, the following statements are 1 > 2 or 3 > 4 and 1 < 5

Comparison operators have higher precedence than logical operators, so a comparison operation is performed first

The result is: false or False and True

Perform and operation: false or False

Last execution or operation, the result is False

In actual development, such statements 1>2 or 3> 4 and 1<5, because it is too unfriendly to read.

So the basic will not write, in order to facilitate reading, will be enclosed in parentheses to facilitate reading

into: 1>2 or (3> 4 and 1<5)

7. Logical comparison of numbers

3 or 5 results of 3

The calculation logic for x or y is that if X is not true, the X is returned

otherwise returns y

Note that in the logic of the numbers, it is stated that not 0 is true and 0 is False

It's not clear how to use it at the moment, it feels like Python alone.

The C language is also non-0 true, 0 false, 3 or 5, the calculation result is true

8. Encoding

Communicate and communicate with people like people in Mandarin

People speak their own dialect, it must not be communicated,

The Chinese and the Americans communicate, to be able to communicate through translation conversion

The communication between computers, so also to make some rules of the designation, to achieve consensus, it is necessary to encode,

Encoding also contains how to decode, if the decoding method error, get the information is also wrong

The original encoding method ASCII code

Unicode encoding, common UTF-8 Chinese three bytes

UTF-8 in order to be variable-length byte storage,

If the 1th bit is 0, it is a single-byte encoding

If the 1th bit is 1, then the number of consecutive 1, which means that the code is a few bytes,

GB Code GBK Chinese two bytes

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.