Table of Contents
1. What is a conditional statement? " >1. What is a conditional statement?
2. if-else" >2. if-else
1. The usage format of if-else" >1. The usage format of if-else
2. Application" >2. Application
三、elif" >三、elif
1. elif的使用格式如下" >1. elif的使用格式如下
例:改变score的值对应不同的考试等级" >例:改变score的值对应不同的考试等级
2. 和else一起使用" >2. 和else一起使用
4. Summary" >4. Summary
Home Backend Development Python Tutorial Python basic conditional statements

Python basic conditional statements

Jul 25, 2023 pm 03:11 PM
python

1. What is a conditional statement?

Python conditional statements are code blocks that are executed based on the execution results (True or False) of one or more statements.


2. if-else

Think about it:

When using if, it can only do what it needs to do when the conditions are met. So what should you do if you need to do something when the conditions are not met?

Answer: else

1. The usage format of if-else

if 条件:
    满足条件时要做的事情1
    满足条件时要做的事情2


    ...(省略)...
else:
    不满足条件时要做的事情1
    不满足条件时要做的事情2
    ...(省略)...
Copy after login

2. Application

The following uses an example of buying a ticket to help everyone understand.

Result 1: There is a ticket.

    chePiao = 1 # 用1代表有车票,0代表没有车票
    if chePiao == 1:
        print("有车票,可以上火车")
        print("终于可以见到Ta了,美滋滋~~~")
    else:
        print("没有车票,不能上车")
        print("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")
Copy after login

Run result:

Python basic conditional statements

Result 2: Situation without ticket.

chePiao = 0  # 用1代表有车票,0代表没有车票
if chePiao == 1:
    print("有车票,可以上火车")
    print("终于可以见到Ta了,美滋滋~~~")
else:
    print("没有车票,不能上车")
    print("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")
Copy after login

Result 2: Without ticket, running result:

Python basic conditional statements


三、elif

想一想:

if能完成当xxx时做事情

if-else能完成当xxx时做事情1,否则做事情2

如果有这样一种情况:当xxx1时做事情1,当xxx2时做事情2,当xxx3时做事情3,那该怎么实现呢?

答:elif

1. elif的使用格式如下

    if xxx1:
        事情1
    elif xxx2:
        事情2
    elif xxx3:
        事情3
Copy after login

说明:

- 当xxx1满足时,执行事情1,然后整个if结束。

- 当xxx1不满足时,那么判断xxx2,如果xxx2满足,则执行事情2,然后整个if结束。

- 当xxx1不满足时,xxx2也不满足,如果xxx3满足,则执行事情3,然后整个if结束。

- 当xxx1不满足时,xxx2也不满足,当xxx3不满足时....以此类推,直到整个if结束。

例:改变score的值对应不同的考试等级

    score = 77


    if score>=90 and score<=100:
        print(&#39;本次考级,等级为A&#39;)
    elif score>=80 and score<90:
        print(&#39;本次考试,等级为B&#39;)
    elif score>=70 and score<80:
        print(&#39;本次考试,等级为C&#39;)
    elif score>=60 and score<70:
        print(&#39;本次考试,等级为D&#39;)
    elif score>=0 and score<60:
        print(&#39;本次考试,等级为E&#39;)
Copy after login

运行结果:

Python basic conditional statements

2. 和else一起使用

   if 性别为男性:
       输出男性的特征
       ...
   elif 性别为女性:
       输出女性的特征
       ...
   else:
       第三种性别的特征
       ...
Copy after login

代码说明:

- 当 “性别为男性” 满足时,执行 “输出男性的特征”的相关代码。

- 当 “性别为男性” 不满足时,如果 “性别为女性”满足,则执行 “输出女性的特征”的相关代码。

- 当 “性别为男性” 不满足,“性别为女性”也不满足,那么就默认执行else后面的代码,即 “第三种性别的特征”相关代码。

  elif必须和if一起使用,否则出错。


4. Summary

This article is based on the basics of Python and introduces several common conditional judgment statements. Through the actual operation of small projects, we can better understand the conditional judgment statements. usage.

A detailed explanation of the difficulties encountered during the project practice and the points that need attention are given.

The above is the detailed content of Python basic conditional statements. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles