Home  >  Article  >  Backend Development  >  Must learn! In-depth analysis of commonly used flow control statements in Python

Must learn! In-depth analysis of commonly used flow control statements in Python

WBOY
WBOYOriginal
2024-01-20 09:37:06493browse

Must learn! In-depth analysis of commonly used flow control statements in Python

Must-see for beginners! Analysis of commonly used flow control statements in Python requires specific code examples

Introduction: Python, as a concise and powerful programming language, is easy to learn and is suitable for beginners to get started. The flow control statement is the core of programming. By mastering the flow control statement, you can make your program writing more flexible and efficient. This article will give you a detailed analysis of the commonly used flow control statements in Python, along with specific code examples. I hope it will be helpful to your learning.

1. If statement

The if statement is one of the most basic flow control statements in Python, which is used to execute different code blocks based on conditional judgments. Its general syntax structure is:

if condition:

# code block to be executed if the condition is True

where condition is the condition that needs to be judged. If the condition is true, the indented code block below is executed.

Example 1: Determine whether a number is odd or even

num = 10
if num % 2 == 0:

print("该数为偶数")

else:

print("该数为奇数")

The output result is: "The number is an even number"

Example 2: Determine whether a student has passed

score = 90
if score >= 60:

print("恭喜你,你及格了!")

else:

print("很遗憾,你没有及格。")

The output result is: "Congratulations, you passed!"

2. for loop

The for loop is used to traverse an iterable object ( Such as list, tuple, string, etc.) and perform the same operation on each element in it. Its general syntax structure is:

for element in iterable:

# code block to be executed for each iteration

where element represents each element in the iterable object, and iterable represents the iterable object, which contains multiple elements.

Example 3: Iterate through the elements in the list and output

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:

print(fruit)

The output results are: "apple", "banana", "cherry"

Example 4: Calculate the sum of all numbers between 1 and 10

sum = 0
for i in range(1, 11):

sum += i

print(sum)

The output result is: 55

3. while loop

while loop is in Repeat a block of code when a specific condition is met until the condition is no longer true. Its general syntax structure is:

while condition:

# code block to be executed repeatedly

where condition is the condition that needs to be judged. Only when the condition is true, the loop will continue to execute.

Example 5: Calculate the sum of all numbers between 1 and 10

sum = 0
i = 1
while i

sum += i
i += 1

print(sum)

The output result is: 55

Example 6: Guessing Number Game

import random

number = random.randint(1 , 100)
guess = int(input("Please enter a number:"))

while guess != number:

if guess > number:
    print("猜大了,请继续猜!")
else:
    print("猜小了,请继续猜!")
guess = int(input("请输入一个数字:"))

print("Congratulations, you guessed it right! ")

4. Break and continue statements

In a loop, break and continue statements can control the execution flow of the program.

The break statement is used to terminate the entire loop, even if the loop condition is still true. The continue statement is used to skip the remaining code of the current loop and proceed to the next loop.

Example 7: Traverse the list until an element is encountered

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:

if fruit == "banana":
    break
print(fruit)

The output result is: "apple"

Example 8: Traverse the list and skip an element

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:

if fruit == "banana":
    continue
print(fruit)

The output results are: "apple", "cherry"

Summary:

This article introduces the commonly used process control statements in Python. Includes if statements, for loops, while loops, and break and continue statements, and demonstrates their use with specific code examples. Mastering these basic flow control statements, you will be able to write more flexible and efficient programs. I hope this article will be helpful to your learning, and I wish you become a master of Python as soon as possible!

The above is the detailed content of Must learn! In-depth analysis of commonly used flow control statements in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn