00x1 python branch cycle

Keywords: Python less Google github


Three Structures

  • order
  • branch
  • loop
 

branch

  • Basic syntax for branching

    • if conditional expression:
      • Statement 1
      • Statement 2
      • Statement 3
      • ......
  • A conditional expression is an expression whose result must be a Boolean value
  • There must be no fewer colons after the expression
  • Notice the statement that appears after if, if it belongs to the if statement block, it must have the same locking level
  • Conditional expression results in True executing indented statement blocks after if
In [4]:
 
 
 
 
 
# if statement exercise
# If age is less than 18 years old, print the message "Minors can't get on the bus"    
age = 17
if age < 18:
    print('Juveniles can't get on the bus')
    print('Come on, my mother won't let me play with my children')

  

 
 
 
Juveniles can't get on the bus
 Come on, my mother won't let me play with my children
In [2]:
 
 
 
 
 
# if statement exercise
# If age is less than 18 years old, print the message "Minors can't get on the bus"
age = 19
if age < 18:
    print('Juveniles can't get on the bus')
    print('You go, my mother won't let me play with my children')
print('Start getting on, guys')
 

  

 
 
 
Start getting on, guys
In [3]:
 
 
 
 
 
# if statement exercise
# If age is less than 18 years old, print the message "Minors can't get on the bus"
age = 19
if age < 18:
    print('Juveniles can't get on the bus')
print('We won't play with you')
​
print('Start getting on')
 

  

 
 
 
We won't play with you
 Start getting on
In [5]:
 
 
 
 
 
print('Learn Today for loop')
gender = "male"
if gender == "female":
    print('Come on, uncle gives you Candy')
print('Start speaking for Circulated')
​

  

 
 
 
Learn for loop today
 Start talking about the for loop
 

Bi-directional branch

  • if...else...statement

    • if conditional expression:
      • Statement 1
      • Statement 2
      • ...
    • else:
      • Statement 1
      • Statement 2
      • .....
  • Two-way branches have two branches. When a program executes an if... else... statement, it must execute either one of the if or else statements, or only one.

  • Indentation problem, if and else one level, the rest of the statements one level

In [8]:
 
 
 
 
 
# The function of input is to
# 1. Output bracketed strings on screen
# 2. Accept user input and return to the program
# 3. input must return a string type
gender = input('Please enter gender:')
print('The gender you entered is:{0}'.format(gender))
​
if gender == 'male':
    print('Come on, let's commemorate today, and hit the code ten times')
​
else:
    print('Glycoglol')
    print('Glycoglol')
​
print('End')
 

  

 
 
Please enter gender: male
 The gender you entered is: male
 Come on, let's commemorate today, and hit the code ten times
 End
In [9]:
 
 
 
 
 
#Judgment of test results
 #90 or above: Output excellent
 # 80-90: Good
 # 70-80:
# 60-70:Ping
 Below # 60: Output: I don't have you as a monk

  

 
 
 
In [10]:
 
 
 
 
 
# score stores student results
# Notice the return value type of input
score = input("Please enter student results:")
# str needs to be converted to int
score = int(score)
​
if score >= 90:
    print('A')
if score >= 80 and score < 90:
    print('B')
if score >= 70 and score < 80:
    print('C')
if score >= 60 and score < 70:
    print('D')
​
if score < 60:
    print("You go, my mother won't let me play with a fool")
 
 

  

 
Please enter student achievement: 80
B
 

switch

  • The case of many branches, referred to as multibranch

    • if conditional expression:
      • Statement 1
      • ....
    • elif conditional expression:
      • Statement 1
      • ...
    • elif conditional expression:

      • Statement 1
      • ...
    • .....

    • else: -statement 1....
  • elif can be composed of many songs

  • else optional

  • Only one execution will be selected for multibranch
In [11]:
 
 
 
 
 
# score stores student results
# Notice the return value type of input
score = input("Please enter student results:")
# str needs to be converted to int
score = int(score)
​
if score>=90:
    print("A")
elif score>= 80:
    print("B")
elif score >= 70:
    print("C")
elif score >=60 :
    print("D")
else:
    print("You go, my mother won't let me play with a fool")

  

 
 
 
Please enter student achievement: 8
 You go, my mother won't let me play with a fool
 

if statement other:

  • if statements can be nested, but are not recommended
  • python has no switch-case statement
 

Loop statement

  • Repeat certain fixed actions or handle essentially fixed things
  • classification
    • for loop
    • while loop# for loop
  • for loop

    • for variable in sequence:
      • Statement 1
      • Statement 2
      • . ..
In [12]:
 
 
 
 
 
# A list is a list of numbers or other values, usually in square brackets
# For example, ['Google','Baidu','github','cnbolg']
​
# Print Student List Name
for name in  ['Google', 'baidu', 'github','cnbolg']:
    print(name)
 

  

 
 
 
Google
baidu
github
cnbolg
In [14]:
 
 
 
 
 
# Print Student List Name
# If it's a sister, it must be my favorite.
# If it's a male student, reject him cruelly
for name in  ['Quiet', 'Xiao Ming', 'King','Laobi']:
    print(name)
    if name == "Quiet":
        print('My favorite{0}Younger sister'.format(name))
    else:
        print('You go, my mother won't let me play with my male classmates')
​
 

  

 
 
 
Quiet
 My favorite quiet sister
 Xiao Ming
 You go, my mother won't let me play with my male classmates
 King
 You go, my mother won't let me play with my male classmates
 Laobi
 You go, my mother won't let me play with my male classmates
 

range introduction

  • Generate a sequence of numbers
  • The specific range can be set
In [15]:
 
 
 
 
 
# range exercise
# Print numbers from 1-10
# Note that in python, if two numbers are used to represent the range of numbers, the left number is usually included and the right number is not included
# randint is a special case, he contains both left and right
# The range function differs significantly between python2 and python3
for i in range(1,11):
    print(i)

 

1
2
3
4
5
6
7
8
9
10
 

for-else statement

  • When the for loop ends, the else statement is executed
  • The else statement is optional
In [16]:
 
 
 
 
 
# for-else Sentence# for-else 
# Print the list of students,
# If it's not in the list, or if it's over, we need to print a reminder that we're not in love
​
# Print Student List Name
# If it's quiet, it must be my favorite sister.
# If it's a boy, reject him cruelly
for name in ['Quiet', 'Xiao Ming', 'King','Laobi']:
    print(name)
    if name == 'Quiet':
        print('My favorite{0}Sister appears'.format(name))
​
    else:
        print('You go, my mother won't let me play with my male classmates')
​
else:
    print('I'm angry that my favorite sister isn't there')
    print('I'm angry that my favorite sister isn't there')
    print('I'm angry that my favorite sister isn't there')
    print('I'm angry that my favorite sister isn't there')
 
 
 

  

 
Quiet
 My favorite quiet sister appears
 Xiao Ming
 You go, my mother won't let me play with my male classmates
 King
 You go, my mother won't let me play with my male classmates
 Laobi
 You go, my mother won't let me play with my male classmates
 I'm angry that my favorite sister isn't there
 I'm angry that my favorite sister isn't there
 I'm angry that my favorite sister isn't there
 I'm angry that my favorite sister isn't there
 

Loop break,contineu,pass

  • break: unconditionally end the entire cycle, referred to as sudden cycle death
  • continue: unconditionally end this cycle, from new to next
  • pass: means skip, usually used for station
In [17]:
 
 
 
 
 
# In numbers 1-10, look for number 7, and once you find it, print it out, and do nothing else
# Variables i n the for loop are typically expressed as i, k, m, n, or indx, idx, item, etc.
# In python, if the name of a loop variable is not important, you can replace it with an underscore ()
​
for i in range(1,11):
    if i == 7:
        print("I found it")
        break
    else:
        print(i)
 

  

1
2
3
4
5
6
 I found it
In [18]:
 
 
 
 
 
# continue Statement Contact
# In numbers 1-10, find all even numbers, print even numbers after finding even numbers
​
for i in range(1,11):
    if i % 2 == 1:
        continue
    else:
        print("{0} Is Even".format(i))
 

  

 
 
 
2 is even
 4 is even
 6 is even
 8 is even
 10 is even
In [19]:
 
 
 
 
 
# continue Statement Version 2# contin 
# In numbers 1-10, find all even numbers, print even numbers after finding even numbers
​
for i in range(1,11):
    if i % 2 == 0:
        print("{0} Is Even".format(i))
 
 
 

  

 
2 is even
 4 is even
 6 is even
 8 is even
 10 is even
In [20]:
 
 
 
 
 
# continue statement version 2
# In numbers 1-10, find all even numbers, print even numbers after finding even numbers
# This case shows the use and use of continue in its entirety
for i in range(1,11):
    if i % 2 == 1:
        continue
    print("{0} Is Even".format(i))
 

  

 
 
 
2 is even
 4 is even
 6 is even
 8 is even
 10 is even
In [22]:
 
 
 
 
 
# pass example, commonly used for placeholders
# pass has no skip function
​
for i in range(1,10):
    pass
    print("test!")
 

  

 
 
 
test!
test!
test!
test!
test!
test!
test!
test!
test!
In [ ]:
 
 
 
 
 
 
 

Posted by Fusioned on Tue, 14 May 2019 22:42:04 -0700