# Python_0 basic learning_day02

Time：2019-9-7

## Section II

### One, while

While is also called infinite loop and dead cycle.

1. While condition:

Indentation circulator

• Applications: Music playback: single track loop, list loop, random playback (also regular)

Landing Interface:…

Mathematical calculation: sum of 1-100, sum of 1-100 odd and/or even numbers…

`while True:    print('-------------')    print('Northwest Xuan Tian is a cloud,')    print(The crow falls into the Phoenix flock.    print('The table is full of heroes'.    print('Who is the king and who is the minister? )`

• break– Must be recycled while

braek– Terminates the current loop and the code below it does not execute.

`while True:    print('-------------')    print('Northwest Xuan Tian is a cloud,')    print(The crow falls into the Phoenix flock.    print('The table is full of heroes'.    print('Who is the king and who is the minister? )    break # After any conditions that can be placed under the circulating body`
• Changing the conditions of the circulating body— You can also terminate the cycle.

`f = Truewhile f:    print('-------------')    print('Northwest Xuan Tian is a cloud,')    print(The crow falls into the Phoenix flock.    print('The table is full of heroes'.    print('Who is the king and who is the minister? )f = False # After any conditions that can be placed under the circulating body`

• continue— Jump out of this loop and continue with the next one, and the code below will not execute.

(The last line of code disguised as a loop)

`while True:    print('-------------')    print('Northwest Xuan Tian is a cloud,')    print(The crow falls into the Phoenix flock.    print('The table is full of heroes'.    continue # Disguised as the last line of code, do not execute the following code, continue the wireless loop    print('Who is the king and who is the minister? )`
1. Exercises

1. `# Use the while statement to write the guess size game: Set an ideal number such as: 66, let the user enter the number, if larger than 66, then show the guess result is larger; if smaller than 66, it shows the guess result is smaller; only equal to 66, show the guess result is correct, and then exit the loop.num = 66 # Setting variables for later maintenancewhile True: # Set an infinite loop    s = int(input("Guess what integer the emperor is thinking of:")# User Interaction    if s > num:        print("Great results")    elif s < num:        print("The result is small")# If the input number does not satisfy the requirement, it will circulate indefinitely.    else:   # satisfies s = 66        print("The results are correct")        break  # Termination`
1. `# Upgrade on the basis of Question 1: Give users three guesses. If they guess correctly within three times, they will show that they guessed correctly. If they don't guess correctly within three times, they will automatically exit the circle and show that'You are too stupid'.count = 0num = 66while count < 3: # Set limits on variables    s = int(input("Guess what number the emperor is thinking of:")    if s > num:        print("Great results")    elif s < num:        print("The result is small")    else:        print("The results are correct")        break    count = count + 1  # Change the variable, change the condition to False, and terminate the loopelse:    print("It's silly of you!"`
1. `# Output 12 3 4 5 6 8 9 10 using while loop# Pay attention to reading, No 7# Method 1num = 0while num < 10:    num += 1    if num == 7:        continue  # When num is 7, end this cycle and continue next time    print(num)# Method 2num = 0while num < 10:    num += 1    if num == 7:        num += 1 # When num is 7, 7 + 1    print(num)   `
1. `# Output all odd numbers in 1-100# Method 1num = 1while num < 100:    print(num)    num += 2   # The variable is 1, plus two each time, and the output is odd.# Method 2num0 = 1while num0 < 101:    if num0 % 2 == 1: # Divide variables by 2 and then output        print(num0)    num0 += 1`
1. `# All Even Numbers in Output 1-100# Method 1num1 = 0while num1 < 100:    num1 += 2　# Every time you add two, the variable is zero, the output is even.    print(num1)# Method 2num2 = 1while num2 < 101:    if num2 % 2 == 0: # Variable 2, remainder 0, is even        print(num2)    num2 += 1​`
1. `# Find the sum of all numbers of 1-2+3-4+5... 99num4 = 0ss = 0while num4 < 99:    num4 += 1    if num4 % 2:  # Find the odd number and add it up.        ss += num4    else:         # Find the even number, then the odd number minus the even number.        ss -= num4print(ss)`

### Formatting

The positions occupied must be filled one by one, and filled in order, from left to right.

applicationFor example, make a business card, register an account…

1. %s—— String type

2. %d%i—— Number type

`name = input("name:")age = input("age:")addr = input("addr:")phone = input("phone:")info = """-----------info----------Name:%sAge:%sCompany:%sTelephone:%s------------end-----------"""%(name,age,addr,phone)print(info)`

3. %%—— Transliteration

`msg = "% s is 2%"%(python)print(msg)#%% Transliteration is two percent`

4. F “{Format}”—— The Best Filling

`# An Application of f"{}"msg = f"""-----------info----------Name: {input ('name:')}Age: {input ('age:')}Company: {input ('addr:')}Telephone: {input ('phone:')}------------end-----------"""print(msg)`
1. Exercises

`# User login (three error opportunities) prompts for the number of input errors, the number of remaining inputs (combination of while and formatted output)count = 4s1 = 0while count > 1:    count -= 1    id = input("Please enter your account:")    psd = input("Please enter your password:")    if id == "username" and psd == "password":        print("Landing Success")        break    else:        s1 += 1        print(F "Input error {s1} times and {count - 1} times again")`

### Operators

1. Arithmetic operator

operator describe Example
Subtraction, subtraction of two objects, may result in negative numbers a-b=-10
* Multiply, match two numbers, or return a string that has been repeated several times a*b=200
/ Except, two objects divide b/a=2
% Take the membrane and return the remainder of the Division b%a=0
** Power, returning the Y power of X a**b=100000000000000000000L
// The integer part of a quotient. a//b=0
2. Comparison operator

operator describe Example
== Comparing two objects is equal. A==b returns False
!= Not equal. Compare two objects to see if they are not equal. A!= B Returns True
<> Not equal. Compare two objects to see if they are not equal. A<>b Returns True
> Comparing whether x is greater than y, A > B returns to False
< Comparing whether x is less than y A < B returns to True
>= If x is greater than or equal to y, compare whether x is greater than or equal to y. A >= B returns False
<= If x is less than or equal to y, compare whether x is less than or equal to y. A<= B returns True
3. Assignment Operators

operator describe Example
= Assignment Operators C = a + b, assign the result of a + B to C
+= Additive assignment operator C += A is equivalent to C = C + A
-= Subtraction assignment operator C – = a is equivalent to C = C – A
*= Multiplication assignment operator C *= A is equivalent to C = C * a
/= Division assignment operator C/= A is equivalent to C = C / A
%= Modular assignment operator C% = a is equivalent to C = c% = a
**= Power assignment operator c = A is equivalent to C = C a
//= Dividing assignment operator C /= A is equivalent to C = C // a
4. Logical Operator

operator describe Example
and “And” A and B return true
or “Or” A or B returns true
not “No” No (a and b) returns false

andAll are true, that is true: choose the content after and

One is false, the other is false: when both are false, choose the content before and

or As long as there is one truth, it is true.

Both are true. Choose or

Both are false: the one behind or

notNo, No.

• Priority: () > not > and > or

`9 or 8 and 4 and not 4 < 5 or 89 or 8 and 4 and False or 89 or 4 and False or 89 or False or 89 or 89​not 3<5 and 6>3 or 6 and not True or False and 8 or 90False and 6>3 or 6 and not True or False and 8 or 90False and 6>3 or 6 and False or False and 8 or 90False or 6 and False or False and 8 or 90False or  False or False and 8 or 90False or False or False or 9090`
5. member operator

operator describe
in If a value is found in the specified sequence, it returns True, otherwise it returns False.
not in Returns True if no value is found in the specified sequence, otherwise false
`a = "alex"if "b" not in a:    print("No")else:    print("In"    a = "alex"if "b" in a:    print("In"else:    print("No")`
1. Operator exercises

`# I deduce it first, then print it.Judge True of the following logical statements,False.1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6# Truenot 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6# FalseFind the values of the following logical statements.8 or 3 and 4 or 2 and 0 or 9 and 7# 80 or 2 and 3 and 4 or 6 and 0 or 3# 4`

### Preliminary knowledge of coding

1. ASCII code—— American self-made, the earliest 256

English 1 byte

Chinese is not supported

2. Gbk—— Made in China includes ASCII

Chinese Standard Language-National Standard

English 8 bits 1 byte, Chinese 16 bits 2 bytes

3. unicode—— Universal Code

English 32-bit 4 bytes

Chinese 32-bit 4 bytes

4. utf-8—— Upgraded version of Universal CodeThe Most Popular Codebook

English 1 byte 8 bits

Europe 2 bytes 16 bits

Asia 3 bytes 24 bits

### V. Unit Conversion

• 1Bytes = 8bit 1 byte = 8 bits

• 1KB = 1024B

• 1MB = 1024KB

• 1GB = 1024MB

• 1TB = 1024GB

• 1PB = 1024TB

• 1EB = 1024PB

## Quick Experience of Gitbook Introductory Tutorials

This article mainly introduces three kinds of usage.gitbookThe ways aregitbookCommand-line tools,Gitbook EditorOfficial Editor andgitbook.comOfficial website. Generally speaking, the three approaches are suitable for different groups of people, and the basic operation process is the same. Command-line tools are more suitable for programmers with programming experience, and they are simple, efficient and easy to integrate. Editors […]