Python if judgment while loop for loop

Source: Internet
Author: User

#判断
What is judgment:
If certain conditions are met, one can do something, and when the condition is not satisfied, it cannot be done, which is called judgment.
Not only in life, but also in software development, the function of "judging" is often used

The IF statement is used for judgment and is used in the following format:
If you want to judge the condition:
Things to do when the conditions are set

#比较, operator
= = checks whether the values of the two operands are equal, and if so, the condition becomes true. If a=3,b=3, then (a = = b) is True
! = checks whether the values of the two operands are equal, and if the values are not equal, the condition becomes true. If a=1,b=3, then (a! = b) is True
' > Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. If a=7,b=3, then (a > B) is True
' < checks if the value of the left operand is less than the value of the right operand, and if so, the condition is true. If a=7,b=3, then (a < b) is False
' >= checks if the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. If a=3,b=3, then (a >= b) is True
' <= checks if the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. If a=3,b=3, then (a <= b) is True

#逻辑运算符
AndX and Y Boolean "and":
If x is false,x and y returns False, it returns the value of Y. True and False, which returns false. (The condition is set up at the same time)
Orx or Y Boolean "or":
If x is true, it returns true, otherwise it returns the value of Y. False or True to return true. (One of the conditions is set up)
Notnot x boolean "non":
If x is True, returns FALSE. If X is False, it returns TRUE. Not true returns FALSE, not false returns True (the condition is not established)

#if-else
Use: When the condition is not established, also need to execute some code is how to do
If-else

Example: If condition:
Execute Event 1
Else
Execute Event 2 in

#elif
Use: Do event 1 when condition 1 is satisfied, do when condition 2 is satisfied, event 2

If xxx1:
Thing 1
Elif xxx2:
Thing 2
Elif xxx3:
Thing 3
#if嵌套
Use: When satisfies the condition of 1 o'clock, satisfies the condition 1 to continue to determine whether satisfies the condition 2, satisfies on the execution Event 1, does not satisfy the condition 2 executes the event 2, does not satisfy the condition 1 executes the event 3

If condition 1:

Meet the conditions 1 do things 1
Meet the conditions 1 do things 2

If condition 2:
Meet the conditions 2 do things 1
Meet the conditions 2 do things 2
#下面是一个小应用猜拳游戏:
Here is the method used by the Python3 input if else print random

Import Random
Player = input (' Please Enter: Scissors (0) stone (1) cloth (2): ')
Player = Int (player)
#产生随机整数: One of 0, 1, 2
Computer = Random.randint (0,2)
#用来进行测试
Print (' player=%d,computer=%d ', (player,computer))
if (player = = 0) and (computer = = 2)) or ((Player ==1) and (computer = = 0)) or ((player = = 2) and (computer = = 1)):
Print (' Win, haha, you're so awesome ')
elif player = = computer:
Print (' Draw, or one more game ')
Else
Print (' Lose, don't go, wash your hands and then come, battle till Dawn ')
#循环语句 while statement
What is a loop statement: When the condition is met, the loop executes the event until the condition is not established

While condition:
Things to do when the condition is met 1
Things to do when the condition is met 2
Things to do when the condition is met 3
... (omitted) ...
Small example:
Calculates the and of 1 to 100:

i = 1
sum = 0
While I <= 100:
sum = sum + I
i + = 1
Print (cumulative sum of "1~100:%d")

#while的循环嵌套
While nesting is: while inside there is while

While condition 1:

Condition 1 when satisfied, do things 1
Condition 1 when satisfied, do things 2
Condition 1 when satisfied, do things 3
... (omitted) ...

While condition 2:
Condition 2 When satisfied, do things 1
Condition 2 When satisfied, do things 2
Condition 2 When satisfied, do things 3
... (omitted) ...

Small example:
99 Multiplication Table:

i = 1
While i<=9:
J=1
While J<=i:
Print ("%d*%d=%-2d"% (j, I, i*j), end = ')
J+=1
Print (' \ n ')
I+=1
#for循环
In Python, A for loop can traverse any sequence of items, such as a list or a string.
Use:
A for temp variable in a list or a string that iterates over an object:
Code that executes when a loop satisfies a condition
Small example:

name = ' Itheima '

For x in Name:
Print (x)
#break
The function of break: Immediately end the loop where the break is
#continue
Continue: Used to end the cycle, followed by the next cycle
Attention:
Break/continue can only be used in loops and cannot be used alone
Break/continue in a nested loop that only works on the most recent layer of loops

Python if judgment while loop for loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.