python--conditional statements and loop statements

巴扎黑
Release: 2017-07-17 16:00:48
Original
2090 people have browsed it

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("我成年了!")
Copy after login

Double branch

 1 ''' 2 if 条件 : 3     满足条件后执行的代码 4 else 5     不满足if时执行 6 ''' 7  8 age = 19 9 if age <= 18 :10     print("我未年!")11 else :12     print("我成年了!")
Copy after login

Multiple branch:

 1 &#39;&#39;&#39; 2 if 条件 : 3     满足条件后执行的代码 4 elif 条件 : 5     不满足上面条件执行 6 elif 条件 : 7     不满足上面条件执行 8 ... 9 else10     不满足上面条件执行11 &#39;&#39;&#39;12 13 age = 1914 if age <= 18 :15     print("我还未年!")16 elif age >= 18 :17     print("我已经成年了!")18 else :19     print("我今年刚成年!")
Copy after login

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 <= 100 :    #只要count<=100就不断执行下面的代码8     print("loop ", count )9     count +=1    #每执行一次,就把count+1,要不然就变成死循环啦,因为count一直是0
Copy after login

while...else statement:

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 is not interrupted by break, the statement after else will be executed.

Infinite loop:

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.

while is executed as long as the following condition is always true (that is, the condition result is always true).

For example: In the above code, if there is no code count += 1, the program will enter an infinite loop. Because count = 0, count <= 100 is always true.

Loop termination statement:

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.

break: Break out of the loop completely and execute the code after the loop.

continue: Jump out of this loop, do not execute the code after continue, and re-enter the loop to judge the condition of the loop.

for loop:

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
Copy after login

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()
Copy after login

结果:

 

预习解答:

 1 #使用while循环输出1 2 3 4 5 6     8 9 10 2 count = 1 3 while count <= 10 : 4     print(count) 5     count += 1 6     if count == 7 : 7         count += 1 8  9 #count = 010 #while count < 10 :11 #   count += 112 #   if count == 7 :13 #       continue14 #       print(count)
Copy after login
1 #求1-100的所有数的和2 count = 13 sum = 04 while count <= 100 :5     sum += count6     count += 17 print(sum)
Copy after login
1 #输出 1-100 内的所有奇数2 count = 13 while count <= 100 :4     print(count)5     count += 2
Copy after login
1 #输出 1-100 内的所有偶数2 count = 23 while count <= 100 :4     print(count)5     count += 2
Copy after login
 1 #求1-2+3-4+5 ... 99的所有数的和 2 count = 1 3 sum = 0 4 while count < 100 : 5     if count % 2 == 1 : 6         sum += count 7     else : 8         sum -= count 9     count += 110 print(sum)
Copy after login
 1 #用户登陆(三次机会重试) 2 username = "oldbody" 3 password = 10086 4 count = 1 5 print("请输入账户密码共三次尝试机会!") 6 while count <= 3 : 7     name = input("请输入账户:") 8     pswd = int(input("请输入密码:")) 9     if name == username and pswd == password :10         print("输入正确!")11         break12     else :13         print("第",count,"输入错误请重新输入!")14         count += 1
Copy after login

小知识点:

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template