Python 3 conditional control, loop statement

Article directory

if statement

In Python, elif is used instead of else if, so the keyword of if statement is: if elif else

Be careful:

  • Each condition is followed by a colon:, which indicates the block of statements to be executed after the condition is met.
  • Use indentation to divide statement blocks. Statements with the same number of indentation form a statement block together.
  • There is no switch – case statement in Python.
    General form:
if condition_1:
    statement_block_1
elif condition_2:
    statement_block_2
else:
    statement_block_3

Example demonstration:

age=19
tall=186
if age <= 0:
    print("You're not born yet!")
elif age < 18:
    print("You're still a minor!")
elif age:
    print("You're an adult!")
if tall < 170:
    print("You lack nutrition!")
elif tall:
    print("You have good nutrition!")


if nesting demonstration:

#Grade 5
num=int(input("Please enter a score:"))
if num >=90:
    print('A')
else:
    if num >= 80:
        print('B')
    else:
        if num >= 70:
            print('C')
        else:
            if num >=60:
                print('D')
            else:
                print('E')

while Loop

In Python, there is no do while Loop

while condition:
    Execute statements
a=1
while a<10:
    print(a)
    a=a+2

Infinite cycle

Use Ctrl+c to exit the loop
Infinite loops are useful for real-time client requests on the server.

a=1
while a==1:
    num=int(input("Please enter a number:"))
    print("The numbers you enter are:",num)

while loop use else statement

while Else the block of statements that execute else when the conditional statement is false.

a=5
while a <16:
    print(a,"Less than 16")
    a=a+4
else:
    print(a,"Greater than or equal to 16")

Simple statement group

Similar to the syntax of if statement, if there is only one statement in your while loop body, you can write the statement on the same line as while

while(1):print("Can't stop! Press quickly ctrl+c")  
print("Goodbye")

for statement

The Python for loop can traverse any sequence of items, such as a list or a string.
The general format is as follows:

for <variable> in <sequence>:
    <statements>
else:
    <statements>
languages=["C","C++","Python","Perl"]
for x in languages:
    print(x)

Use break in for statement

languages=["C","C++","Python","Perl"]
for x in languages:
    if x == "Python":
        print("Python!")
        break
    print("Circular data", x)
else:
    print("No circular data")
print("End of cycle!")

rang() function

  • If you need to traverse a sequence of numbers, use the built-in range() function. It generates a sequence
for i in range(6):
    print(i)
    

  • You can also use range to specify the value of the interval
for i in range(5,9):
    print(i)

  • You can make range start with a specified number and specify different increments (even negative numbers, sometimes called 'steps')
for i in range(0,10,3):
    print(i)


negative

for i in range(-1,-12,-3):
    print(i)

  • Combine the run() and len() functions to traverse the index of a sequence
a=['Google', 'Baidu', 'Runoob', 'Taobao', 'QQ']
for i in range(len(a)):
    print(i,a[i])

  • You can also use the range() function to create a list
a=list(range(10))
print(a)

break and continue statements and else clauses in loops

  • The break statement can jump out of the loop bodies of for and while. If you terminate from a for or while loop, any corresponding loop else block will not execute.
  • The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue to the next loop.

break

Example demonstration:
Use break in while:

m=5
while m> 0:
    m=m-1
    if m == 2:
        break
    print(m)
print("Loop end")

continue

Use continue in while:

m=5
while m> 0:
    m=m-1
    if m == 3:
        continue
    print(m)
print("Loop end")

Query prime (else application)

In python, for Else means that there is no difference between the statements in for and the ordinary ones. The statements in else will be executed after the normal execution of the loop (that is, for is not interrupted by break ing out) So is else.

for n in range(2,20):
    for x in range(2,n):
        if n % x == 0:
            print(n,'Be equal to',x,'*',n//x)
            break
    else:
            print(n,"Prime number")

pass statement

Python pass is an empty statement to maintain the integrity of the program structure.
pass does nothing. It is generally used as a placeholder statement, as shown in the following example:

for x in "Kobe bryant":
    if x =='o':
        pass
        print("pass block")
    print(x)
print("forever")

for and while exercises

Print 1-9 triangle array

for i in range(1,11):
    for k in range(1,i):
        print(k,end=" ")
    print("\n")

99 multiplication table

i=1
while i <= 9:#Number of external loop control lines
    j=1
    while j<=i:#Number of internal circulation control columns
        s=i*j
        print("%dx%d=%d"%(i,j,s),end=" ")
        j=j+1
    print("")#Line feed
    i=i+1

Maximum common divisor and minimum common multiple

m=int(input("First number:"))
n=int(input("Second number:"))
min=min(m,n)
for i in range(1,min+1):
    if(m % i == 0 and n % i == 0):
        r=i
    k=int((m*n)/r)
print("greatest common divisor:",r)
print("Minimum common multiple:",k)

45 original articles published, 98 praised, 7477 visited
Private letter follow

Tags: Python less Google

Posted on Fri, 31 Jan 2020 17:42:52 -0500 by CoB-Himself