Getting started with Python Basics_ 3 conditional statements and iterative loops

lc013 2021-09-15 08:41:48

Python The first two chapters of Basics :

  • Python Basic introduction – Introduction and environment configuration
  • Python Basic introduction _2 Basic syntax and variable types

This is the third content , It mainly introduces conditional statements and iterative loop statements , The content is also relatively simple , Directory as follows :

Python Basic introduction _3 Conditional statements and iterative loops _Python


Conditional statements and iterative loops

1. Conditional statements

Python A conditional statement is the result of the execution of one or more statements ( Judge True perhaps False) To determine the code block to execute .

On the whole, it can be divided into four statements :

  • if sentence
  • if-else sentence
  • if-elif-else sentence
  • Nested statement ( Multiple if sentence )
if sentence

Given a binary condition , Execute the statement if the condition is met A, If you are not satisfied, skip , The code example is as follows :

a = 3
# if sentence 
if a > 0:
print('a =', a)

  • 1.
  • 2.
  • 3.
  • 4.
if-else sentence

Also given the binary condition , Execute the statement if the condition is met A, The execution statement... Is not satisfied B, The code example is as follows :

a = 3
# if-else
if a > 2:
print('a is ', a)
else:
print('a is less 2')

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
if-elif-else sentence

Given multivariate conditions , Meet the conditions 1, Execute statement 1, Meet the conditions 2, Execute statement 2, By analogy , A simple code example is as follows :

a = 3
# if-elif-else
if a > 5:
print('a>5')
elif a > 3:
print('a>3')
else:
print('a<=3')

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
Nested statement

Nested statements can contain more if sentence , Or is it if-else 、if-elif-else The sentence of , A simple code example is shown below :

a = 3
# Nested statement 
if a < 0:
print('a<0')
else:
if a > 3:
print('a>3')
else:
print('0<a<=3')

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

2. Iteration loop

Python There are mainly two kinds of circular statements in ,while Circulation and for loop , And then there was no do-while loop .

while loop

A simple while The cycle is as follows ,while The termination condition of the loop is while The following statement does not satisfy , That is to say False When , The following code example is when n=0 When , It will exit the loop .

n = 3
while n > 0:
print(n)
n -= 1

  • 1.
  • 2.
  • 3.
  • 4.

Another example , When used for input, let the user constantly input content , Until a certain condition is met , sign out .

promt = "\ninput something, and repeat it."
promt += "\nEnter 'q' to end the program.\n"
message = ""
while message != 'q':
message = input(promt)
print(message)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
for loop

for Loops can explicitly define the number of cycles , And often used in lists 、 Traversal of dictionaries, etc . A simple example is as follows :

# for
l1 = [i for i in range(3)]
for v in l1:
print(v)

  • 1.
  • 2.
  • 3.
  • 4.

The above example is actually used twice for loop , The first is used to generate a list from a list derivation l1 , And it's using range function , The specified number of cycles is 3 Time , The second time is used to traverse the list .

about range function , There are also the following usages :

l2 = ['a', 'b', 'c', 'dd', 'nm']
# Specify the interval 
for i in range(2, 5):
print(i)
# Specify the interval , And add a step of 10
for j in range(10, 30, 10):
print(j)
# combination len To traverse the list 
for i in range(len(l2)):
print('{}: {}'.format(i, l2[i]))

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

in addition , For the loop of the list , Sometimes you want to print the value and index value of the current element at the same time , May adopt enumerate function , A firm example is as follows :

l2 = ['a', 'b', 'c', 'dd', 'nm']
for i, v in enumerate(l2):
print('{}: {}'.format(i, v))

  • 1.
  • 2.
  • 3.
break and continue And... In loop statements else sentence

break Statement is used to terminate a loop statement , Examples are as follows :

# break
for a in range(5):
if a == 3:
break
print(a)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

This is what if a = 3 , It will end for Loop statement .

continue Used to skip the current loop , Enter the next cycle , Examples are as follows :

# continue
for a in range(5):
if a == 3:
continue
print(a)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Loop statements can have else Clause , It's exhausting the list ( With for loop ) Or the condition becomes false ( With while loop ) Causes the loop to terminate when executed , but Cyclic quilt break Do not execute on termination . Examples are as follows :

# else
for a in range(5):
print(a)
else:
print('finish!')

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

Reference resources

  • 《Python Programming from entry to practice 》
  • Python Basic course
  • One day quick start python
  • Super easy to understand Python Entry level tutorial , Hurry to collect !

Summary

This article is mainly a simple arrangement of Python The usage of conditional statements and loop statements .

Besides , The code of this article is uploaded to my github Yes :

https://github.com/ccc013/Python_Notes/tree/master/Practise

Welcome to my WeChat official account. – Machine learning and computer vision , Or scan the QR code below , Let's talk , Learning and progress !

Python Basic introduction _3 Conditional statements and iterative loops _github_02

 

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15