In Python, conditional selection and cyclic statements are also available. Their style is similar to that in C and java. However, there are still some differences in the writing syntax. Next, let's take a look at them together, I hope this will help you better learn python. if you are interested, don't miss it. like C and Java, conditional selection and loop statements also exist in Python, its style is similar to that of C and java, but there are still some differences in writing and usage. Let's take a look at it today.
I. condition selection statement
Keyword of the condition selection statement in Python: if, elif, else. The basic form is as follows:
The code is as follows:
If condition:
Block
Elif condition:
Block
...
Else
Block
Here, the elif and else statement blocks are optional. The if and elif statements are executed only when the condition is True. the else branch is executed only when the condition of if and all elif statements are False. Note the difference between the condition selection statement and C in Python. in C, the condition must be enclosed in parentheses and not used in Python. However, note that the condition is followed by a colon.
The following is an example of score classification.:
The code is as follows:
Score = input ()
If score <60:
Print "D"
Elif score <80:
Print "C"
Elif score <90:
Print "B"
Else:
Print ""
II. cyclic statements
Like the C language, Python also provides a for loop and a while loop (there is no do... while loop in Python. However, the for loop usage in Python is very different from that in C language (similar to the for loop usage in Java and C #). the while loop usage is similar to that in C language.
The basic form of a for loop is as follows:
The code is as follows:
For variable in list:
Block
For example, calculate the sum from 1 to 100:
The code is as follows:
Sum = 0
For var in range (1,101 ):
Sum + = var
Print sum
Range () is a built-in function that generates a list of numbers within a certain range. For example, range () generates a list like [, 5], while range (8) generates a list like.
Of course, there can be nested loops. for example, there is a list = ['China', 'England ', 'America'], and each letter needs to be traversed and output.
The code is as follows:
List = ['China', 'England ', 'America']
For I in range (len (list )):
Word = list [I]
For j in range (len (word )):
Print word [j]
The built-in function len () can be used not only to calculate the length of a string, but also to calculate the number of members in a list or set.
The basic form of the while loop is as follows:
The code is as follows:
While condition:
Block
A loop is executed only when the condition is True. Once the condition is False, the loop ends.
For example:
The code is as follows:
Count = 2
While count> 0:
Print "I love python! "
Count = count-1
If you want to terminate the loop during the statement block process, use break or continue. Break jumps out of the entire loop, while continue jumps out of the loop.
The code is as follows:
Count = 5
While True:
Print "I love python! "
Count = count-1
If count = 2:
Break
The code is as follows:
Count = 5
While count> 0:
Count = count-1
If count = 3:
Continue
Print "I love python! "
The basic usage of conditional statements and cyclic statements is basically the same. If you are interested, you are advised to perform hands-on training on your own.