1. If statement

2.1 Basic format of IF judgment

ifJudgment condition: The judgment condition isTrue, the code that will be executed is judged asTrue, the code that will execute... The code written on top represents andifIn Python, indentation is used instead of code hierarchyifThe indent of the statement belongs toifBlock of code for statement (multiple lines of code meaning)Copy the code
Basic format Case
"" case requirements 1. Obtain the age through the user's keyboard input 2. Determine whether the age meets 18, meet the output I am 18 years old, adult 3. End of program """

# 1. Get age input()
age = input('Please output your age')
We need to convert a string age to an int age
age = int(age)
I am 18 years old and come of age
if age >= 18:
    # execute only if conditions are met
    print('I am eighteen years of age and come of age.')


# 3. At the end of the program output 'if judge end '
print('If judgment ends')

Copy the code

If the else structure

If the condition is True, the code that will be executed... Else: Code that will be executed if the condition is False..... If and else will only execute one of them,Copy the code
"" case requirements 1. Obtain the age through the user's keyboard input 2. Determine whether the age meets 18, meet the output I am 18 years old, adult 3. End of program """

# 1. Get age input()
age = input('Please output your age')
We need to convert a string age to an int age
age = int(age)
I am 18 years old and come of age
if age >= 18:
    # execute only if conditions are met
    print('I am eighteen years of age and come of age.')

else:
    # Judge the condition not to cheat, will execute the code
    print('You're under 18 and you're growing up.')


# 3. At the end of the program output 'if judge end '

print('If judgment ends')
Copy the code

If elif structure

If judgment condition 1: judgment conditions 1, execute code elif judgment condition 2: judge condition 1, judging condition 2, the code executes the else: judgment 2 don't set up condition 1 and judgment, execute code -- -- -- -- -- -- -- -- if judgment condition 1: If Condition 2: Indicates the code to be executedCopy the code
# demand
# 1. The score is greater than or equal to 90 and the output is excellent
# 2. Score greater than or equal to 80, less than 90, good output
# 3. A score greater than or equal to 60, less than 80, pass the output
# 4. Output below 60 fails

score = eval(input('Please enter your score'))
# 1. The score is greater than or equal to 90 and the output is excellent
if score >= 90:
    print('good')
# 2. Score greater than or equal to 80, less than 90, good output
elif(score >= 80) and score < 90:
    print('good')
# 3. A score greater than or equal to 60, less than 80, pass the output
elif(score >= 60) and score < 80:
    print('pass')
else:
    print('Fail')

print('End of program')
Copy the code

The case of the guessing game

# import random number
import random
# 1. The user enters his own punch
user = int(input('Please enter the fists to play: 1(rock) 2 (scissors) 3 (paper)'))
# 2, let the computer throw random punches
computer = random.randint(1.3)
# 3. Call the winners
# 3.1 Draw input content as user == computer
# 3.2 User wins
The rest is computer control
if user == computer:
    print('draw')
elif(user == 1 and computer == 2) or (user == 2 and computer == 3) or (user == 3 and computer == 1) :print('Congratulations, you've won.')
else:
    print('You lost')
Copy the code

Three unary

If else structure is distorted

If (1) if (1) else (2) if (2) if (1) else (2) if (1) else (2) if (2) else (2) The value of expression 1 is true, the value of expression 2 is not trueCopy the code
a = int(input('Enter a number :'))
b = int(input('Enter a number :'))

result = a - b if a > b else b - a
result = (a - b) if a > b else (b - a)
print(result)

Copy the code

cycle

The while loop

While condition: The condition is true, the code executed is true, the code executed is not in the indentation of the while, indicating that there is no relation to the loop: If block, if true, will execute once while block, as long as the condition is true, will executeCopy the code

# Use loops to solve laps
# 1. Record how many laps you have run
i = 0
# 2. Write the loop to determine if the condition is met
while i < 100:
    print('Running laps in the playground... ')
    # 3. After one lap, add one to the number of laps recorded
    i += 1
print('Lap completed')
Copy the code

Application case

# Count lacrimal moles between 1 and 100
# 1 + 2 + 3.... + 99 + 100
# Loop to generate numbers between 1 and 100
# Define the variable to record the initial value
my_sum = 0
i = 1
while i <= 100:
    # sum
    my_sum += i
    Change the value of I
    i += 1
print('The sum will output'., my_sum)

Copy the code
My_sum = 0 I = 2 while I <= 100: my_sum += I # change the value of I += 2Copy the code

A nested loop

While condition 1: code 1 while condition 2: code 2 ====== if code 1 is executed once, the code will be executed multiple timesCopy the code
I need to soak 5 laps in the playground
Do 3 push-ups per lap
# 1. Define variables to record the number of laps run
i = 0
while i < 5:
    # 2. Define variables and record how many push-ups you did per lap
    j = 0
    # 3. Playground laps
    print('Running laps in the playground... ')
    # 4. Do push-ups
    while j < 3:
        print('Did a push-up')
        j += 1
        The number of complete laps is increased by 1
    i += 1

Copy the code

The for loop iterates

Syntax for variable in string: The code for loop, also known as a for loop, fetches all the characters in the stringCopy the code
# for i in 'hello':
    # I a loop is one character in a string
    # print(i, end='')
# range(n) generates a sequence of [0, n) data, excluding b
# for i in range(5):
# # print(i)
# print(' Running laps in the playground... ')


# range(a, b) produces a sequence of [a, b] integers, excluding b
# for i in range(3, 7):
# print(i)

# range(a, b, step) produces a sequence of integers [a,b] but the interval (step) between each number is step
for i in range(1.20.4) :print(i)
Copy the code

Circular printing of right triangles

# for loop printed triangles
for i in range(n):
    for j in range(i+1) :print("*", end=' ')
    print(a)Copy the code

Break and continue

Break and continue are python keywords. Break and continue can only be used in loops. Break terminates the execution of loops. A "continue" is the end of this loop and the next loop, so the rest of the code in this loop is not executed, but the next loop is executedCopy the code
Break
# There are five apples
# 1. After eating three apples, I'm full. The subsequent apples are not eaten
# 2. After eating three apples. In eating the fourth apple, found half a worm, this apple does not eat, but also eat the rest of the apple

for i in range(1.6) :if i == 4:
        print('Full, no more.')
        break  # terminates the execution of the loop
    print(F prime is eating labeled{i}Apple ')

Copy the code

continue

   # There are five apples
# 1. After eating three apples, I'm full. The subsequent apples are not eaten
# 2. After eating three apples. In eating the fourth apple, found half a worm, this apple does not eat, but also eat the rest of the apple

for i in range(1.6) :if i == 4:
        print('Found half a worm, this apple is not eaten, not full, continue to eat the rest')
        continue  # will end the loop and continue the next loop

    print(F prime eats the number{i}Apple ')
Copy the code

Loop else structure

for x in xx:
    if xxx:
        xx  # if the judgment condition is executed
    else:
        xxx  If the condition is not true, it will be executed
else:
    xxx  The # for loop completes, but not when terminated by breakRequirement: There is a string'hello python'If p is included in the string, print it. If p is included, print it. If not, print itCopy the code
# There are five apples
# 1. After eating three apples, I'm full. The subsequent apples are not eaten
# 2. After eating three apples. In eating the fourth apple, found half a worm, this apple does not eat, but also eat the rest of the apple

my_str = 'hello python! '
# my_str = 'hello itcast! '

for i in my_str:
    if i == 'p':
        print('contains the character P')
        # It has been determined that it is included. Do you need to continue judging
        break
else:
    print('Does not contain the character P')

Copy the code