Today we look at conditional statements and loop statements.
Preview:
1. Use a while loop to output 1 2 3 4 5 6 8 9 10
2. Find the sum of all numbers from 1 to 100
3. Output all odd numbers from 1 to 100
4. Output all even numbers within 1-100
5, find the sum of all numbers from 1-2+3-4+5...99
6. User login (three chances to retry)
1. Conditional statement
When the program we write needs to branch, it can also be said that when an event occurs and different processing situations occur under specific circumstances, our conditional statements will be used.
if...else statement:
##Single branch:
1 '''2 if 条件 :3 满足条件后执行的代码4 '''5 6 age = 187 if age == 18 :8 print("我成年了!")
Double branch:
1 ''' 2 if 条件 : 3 满足条件后执行的代码 4 else 5 不满足if时执行 6 ''' 7 8 age = 19 9 if age <div class="cnblogs_code"></div><p><span style="font-size: 18px">Multiple branch:<strong></strong></span></p><pre class="brush:php;toolbar:false"> 1 ''' 2 if 条件 : 3 满足条件后执行的代码 4 elif 条件 : 5 不满足上面条件执行 6 elif 条件 : 7 不满足上面条件执行 8 ... 9 else10 不满足上面条件执行11 '''12 13 age = 1914 if age = 18 :17 print("我已经成年了!")18 else :19 print("我今年刚成年!")
Indentation:
In other languages, mostly by {} to determine the code block, but there is no {} in python. This is a major feature of python. So how does python determine the code block to be executed? This leads to the concept of forced indentation. The purpose is to let the program know which condition each piece of code depends on. If it is not distinguished by indentation, the program cannot determine the code block to be executed.
Python’s indentation principle:
Top-level code must be written on the top line, that is, if a line of code itself does not depend on Under any conditions, then it must not be indented at all
Codes at the same level must be indented the same
Official recommendations for indentation 4 spaces, of course you can also indent in the way you are used to.
2. Loop statement
while statement:
1 '''2 while 条件 :3 满足条件后执行的代码4 '''5 6 count = 0 7 while count <div class="cnblogs_code"></div><p><span style="font-size: 18px">while...else statement: <strong></strong></span></p><p class="comments-section"> Unlike other languages, else is generally only paired with if. In Python There is also a while...else statement in it. The function of else after while means that when the while loop is executed normally and <span style="font-size: 16px"> is not interrupted by break, the statement <span style="color: #ff0000"> after else will be executed. </span></span></p><p class="comments-section"><span style="font-size: 18px">Infinite loop: <strong></strong></span></p><p class="comments-section">There is a kind of loop called an infinite loop. Once it enters the infinite loop, the program will run You will never be able to quit until the end of time. <span style="font-size: 16px"></span></p><p class="comments-section">while is executed as long as the following <span style="font-size: 16px"> condition is always true<span style="color: #ff0000"> (that is, the condition result is always true). </span></span></p><p class="comments-section">For example: In the above code, if there is no code count += 1, the program will enter an infinite loop. Because count = 0, count </p><p class="comments-section"><span style="font-size: 18px">Loop termination statement: <strong></strong></span></p><p class="comments-section">If during the loop, for some reason, you do not want to continue the loop , it is necessary to use the break or continue termination statement. <span style="font-size: 16px"></span></p><p class="comments-section"><strong>break:<span style="font-size: 16px"></span></strong> Break out of the loop completely and execute the code after the loop. <span style="font-size: 16px"></span></p><p class="comments-section"><strong>continue:<span style="font-size: 16px"></span></strong> Jump out of this loop, do not execute the code after continue, and re-enter the loop to judge the condition of the loop. <span style="font-size: 16px"></span></p><p class="comments-section"><span style="font-size: 18px">for loop: <strong></strong></span></p> <p class="comments-section"></p><pre class="brush:php;toolbar:false">1 for i in range (4) : # i 为变量 (4)取值范围2 print(">>:",i) # 0 1 2 33 4 for i in range (1,5) : # 顾头不顾尾5 print(">>:",i) # 1 2 3 46 7 for i in range (1,5,2) : # 步长2 每两个取一个值8 print(">>:",i) # 1 3
Multiplication table practice:
1 for i in range(1,10) :2 for j in range(1,i+1) :3 print("%s*%s=%s" %(j,i,i*j),end=" ")4 print()
结果:
预习解答:
1 #使用while循环输出1 2 3 4 5 6 8 9 10 2 count = 1 3 while count
1 #求1-100的所有数的和2 count = 13 sum = 04 while count
1 #输出 1-100 内的所有奇数2 count = 13 while count
1 #输出 1-100 内的所有偶数2 count = 23 while count
1 #求1-2+3-4+5 ... 99的所有数的和 2 count = 1 3 sum = 0 4 while count
1 #用户登陆(三次机会重试) 2 username = "oldbody" 3 password = 10086 4 count = 1 5 print("请输入账户密码共三次尝试机会!") 6 while count
小知识点:
print()自带一个换行符。
如果想取消默认换行符加end(""),详情可以参考九九乘法表的代码。
The above is the detailed content of python--conditional statements and loop statements. For more information, please follow other related articles on the PHP Chinese website!

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools