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 :
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 :
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 :
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 :
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 :
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 .
Another example , When used for input, let the user constantly input content , Until a certain condition is met , sign out .
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 :
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 :
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 :
break and continue And... In loop statements else sentence
break
Statement is used to terminate a loop statement , Examples are as follows :
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 :
Loop statements can have else
Clause , It's exhausting the list ( With for
loop ) Or the condition becomes fals
e ( With while
loop ) Causes the loop to terminate when executed , but Cyclic quilt break
Do not execute on termination . Examples are as follows :
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 !