python if judgment, while loop, for loop

Article directory

Conditional statement

If statement is the expression between if and colon (:) is true, then the content can be executed in multiple lines to distinguish the same range; else is executed when it is false. Check multiple conditions to use elif

if condition judgment

1. Single judgment condition

Basic form:

if judgment condition:
    Execute statement 1
else:
    Execute statement 2      

for instance:

a = 3
if a == 3:
    print('yes') #Output is yes
else:
    print('no')

2. Multiple judgment conditions

Basic form:

if judgment condition 1:
      Execute statement 1
 elif judgment condition 2:
      Execute statement 2
 elif judgment condition 3:
      Execute statement 3
 else judgment condition 4:
      Execute statement 4

for instance:

a = 5
if  a<0:
    print('Incorrect')
elif a==0:
    print('Incorrect')
elif a==2:
    print('Incorrect')
else:
    print('Finally found you!')
    print('good')

Operation result:

3. if conditional statement nesting

Basic form:

if judgment condition 1:
   Execute statement 1
    if judgment condition 1.1:
      Execute statement 1.1
    else:
      Execute statement 1.2
else:
   Execute statement 2         

for instance:

high = 167
weight = 50
if high > 165:
    print('You are tall.')
    if weight < 55:
        print('You're tall and thin')
        print('I envy!!')
    else:
        print('You're a little fat')
else:
    print('You need to lose weight')

Operation result:

4. if statement exercise

Judgement of leap year

Leap year: a year divisible by 4 but not by 100 or divisible by 400

year = int(input('Please enter the year:'))
if year%400 == 0:
    print('It is a leap year.')
elif year%4 == 0 and year%100 != 0:
    print('It is a leap year.')
else:
    print('Not a leap year.')

Operation result:

Loop statement

while Loop

Basic form:

while judgment condition 1:
     Execute statement 1  

There is also a while else:

while judgment condition 1:
     Execute statement 1  
else:
     Execute statement 2   

1. range() function usage

for i in range(5):
    print(i) #Output 0, 1, 2, 3, 4
for i in range(1,5):
    print(i) #Output 1, 2, 3, 4
for i in range(1,10,2):
    print(i) #Output 1, 3, 5, 7, 9
for i in range(2,11,2):
    print(i) #Output 2, 4, 6, 8, 10

2. break, continue, exit usage

break: jump out of the whole cycle and do not recycle the contents inside
Continue: jump out of this loop, the code after continue will not execute, but the loop will continue
eixt(): end program

break usage:
i=0
while i < 6:
    i = i + 1
    if i == 4:
       break #Jump out of the loop when i==4
    print(i)  #Output is 1, 2, 3
continue usage:
i=0
while i < 6:
    i = i + 1
    if i == 4:
       continue#When i==4, jump out of this cycle and carry out the next cycle
    print(i)  #Output is 1, 2, 3, 5, 6
exit() usage:
i=0
while i < 6:
    i = i + 1
    if i == 4:
       exit()#End program at i==4
    print(i)  #Output is 1, 2, 3

3. while loop adds 1-100

sum = 0
i = 1
while i<101:
    sum = sum +i
    i = i + 1
print(sum) #Output 5050

4. while loop

a = 1
while a:
  print('hello python')

5. while nesting (print 9 * 9 multiplication table)

a = 1
while a <= 9:
    b = 1
    while b <= a:
        print('%d * %d =%d\t'%(a,b,a*b),end='')
        b+=1
    print('') #Line breaking
    a+=1

Operation result:

6. while loop exercise

Guess the number game:
import random #Import random template
number=random.randint(1,100) #Generate a random number from 1 to 100 and assign it to number
chance=10 #10 chance
while chance > 0:
    guess = int(input('Guess this number:'))
    if guess > number:
        print('It's too big!')
    elif guess < number:
        print('It's too small!')
    else:
        print('Congratulations, you guessed it right!')
        exit()
else:
    print('The chance is up! game over!')

Operation result:

for cycle

Basic form:

for variable in range():
    Execute statement 1
else:
    Execution statement 2 at the end of the loop

1. pass statement

Python pass is an empty statement to maintain the integrity of the program structure.

pass doesn't do anything. It's usually used as a placeholder.

#Output every letter of python
for i in 'python':
    if i=='h':
        pass
        print('This is pass block')
    print('Current letter:',i)

print('good bye!')

2. A series of problems about 1-100 in for loop

Find the sum of 1-100:
sum=0
for i in range(1,101):
    sum=sum+i
print(sum) #Output 5050
Find the sum of all even numbers between 1 and 100:
sum=0
for i in range(2,101,2):
    sum=sum+i
print(sum) #Output 2550
Find the sum of all odd numbers between 1 and 100:
sum=0
for i in range(1,101,2):
    sum=sum+i
print(sum) #Output 2500

3. for loop to find the factorial of a number

number=int(input('Enter a number:'))
sum = 1
for i in range(1,number+1):
    sum = sum* i
print('%d The factorial is:'%number,sum)

Operation result:

4. for loop exercise

Guess the number game:
import random #Import random template
number=random.randint(1,100) #Generate a random number from 1 to 100 and assign it to number
chance=10 #10 chance
for i in range(1,11):
    guess = int(input('Guess this number:'))
    if guess > number:
        print('It's too big!')
    elif guess < number:
        print('It's too small!')
    else:
        print('Congratulations, you guessed it right!')
        exit()
else:
    print('The chance is up! game over!')

Operation result:

Find the maximum common divisor and the minimum common multiple of two numbers:
a = int(input('First number:'))
b = int(input('Second number:'))

min=min(a,b)
for i in range(1,min+1):       #The greatest common divisor is the largest number that can be divided by two numbers
    if a % i ==0 and b % i ==0:#The greatest common divisor must be less than or equal to the smaller of these two numbers
     c = i         #c is the greatest common divisor
d = int((a * b)/c) #d is the minimum common multiple = the multiplication of two numbers / the maximum common divisor of two numbers
print('The maximum common divisor is:',c)
print('The minimum common multiple is:',d)

Operation result:

Published 20 original articles, won praise 51, visited 7584
Private letter follow

Tags: Python less

Posted on Wed, 29 Jan 2020 21:41:49 -0800 by madwormer2