Python-Based Conditional loop statements and basic python statements
The first two articles talk about data types and operations. This article describes conditional statements and cyclic statements.
0x00. Condition Statement
A condition statement is a code block that is determined by the execution result (True or False) of one or more statements.
You can briefly understand the execution process of conditional statements:
Python interprets non-zero valuesTrue
.None
And0
Are interpretedFalse
.
Python determines that values other than 0 are True, while None and 0 are considered False. Note that True and False are capitalized, and Python is case sensitive.
The basic format of the Condition Statement is:
If judgment condition: execution statement ...... Else: Execute the statement ......
You can use the following form to determine multiple values:
If condition 1: Execute Statement 1 ...... Elif condition 2: Statement 2 ...... Elif Condition 3: Statement 3 ...... Else: Execute Statement 4 ......
0x01. Cyclic statements
When we need to execute a statement or statement group multiple times, it is impossible to write the same statement multiple times. First, it is complicated, and second, it is not conducive to maintenance. At this time, the loop statement came into being. The loop statements are divided into a for loop and a while loop.
For Loop
A for loop can traverse projects in any sequence, such as a list or a string. The process is as follows:
The syntax format of the for Loop is as follows:
for iterating_var in sequence: statements(s)
While Loop
The while statement is used to execute a program cyclically under a certain condition to process the same task that needs to be processed repeatedly. The process is as follows:
The syntax format of the while loop is as follows:
While judgment condition: execution statement ......
0x02. Instance
The following example uses nested loop output 2 ~ Prime number between 100:
#! /Usr/bin/python #-*-coding: UTF-8-*-I = 2 while (I <100): j = 2 while (j <= (I/j )): if not (I % j): break j = j + 1 if (j> I/j): print I, "is a prime number" I = I + 1 print "Good bye! "
0x03. References
Https://www.programiz.com/python-programming/variables-datatypes of variable and Data Type
Condition Statement https://www.programiz.com/python-programming/if-elif-else
Loop statement https://www.programiz.com/python-programming/for-loop
Https://www.programiz.com/python-programming/while-loop
W3cshool https://www.w3cschool.cn/python/python-tutorial.html
The basics end here, the next article, python advanced object-oriented