Python basic conditional statements
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 ...(省略)...
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("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")
Run result:
Result 2: Situation without ticket.
chePiao = 0 # 用1代表有车票,0代表没有车票 if chePiao == 1: print("有车票,可以上火车") print("终于可以见到Ta了,美滋滋~~~") else: print("没有车票,不能上车") print("亲爱的,那就下次见了,一票难求啊~~~~(>_<)~~~~")
Result 2: Without ticket, running result:
三、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
说明:
- 当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('本次考级,等级为A') elif score>=80 and score<90: print('本次考试,等级为B') elif score>=70 and score<80: print('本次考试,等级为C') elif score>=60 and score<70: print('本次考试,等级为D') elif score>=0 and score<60: print('本次考试,等级为E')
运行结果:
2. 和else一起使用
if 性别为男性: 输出男性的特征 ... elif 性别为女性: 输出女性的特征 ... else: 第三种性别的特征 ...
代码说明:
- 当 “性别为男性” 满足时,执行 “输出男性的特征”的相关代码。
- 当 “性别为男性” 不满足时,如果 “性别为女性”满足,则执行 “输出女性的特征”的相关代码。
- 当 “性别为男性” 不满足,“性别为女性”也不满足,那么就默认执行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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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.

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 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 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 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.

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.

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".
