How to type the greater than sign in python, how to type the greater than or less than sign in python

This article mainly introduces the codes for numbers greater than and less than one in python, which has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.

When we write a program, we often need to specify two or more execution paths. When the program is executed, one of the paths is allowed to be selected, or one of the statements is executed when a given condition is true. In this process, we need to use conditional statements to help us determine which python or java is more worth learning . In python, the most common conditional statement is if. How is if used? Let’s take a look below.

The judgment conditions of the if statement can be expressed in terms of > (greater than), < (less than), == (equal to), >= (greater than or equal to), and <= (less than or equal to).

In use, if can determine the condition

If the condition does not hold, use else

1. Basic usage of if conditional statement:

if 判断条件:
    执行语句……
else:
    执行语句……
实例::输入一个数字并判断是否大于10
a = int(input("请输入一个数:"))
if a >= 10:
    print("大于等于10")
else:
    print("小于10")

2. If nested statements

When the judgment condition is multiple values, the following two forms can be used:

The first

if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
执行语句4……

The second kind


if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
执行语句4……

3. Use of if – elif – else statement

#猜拳游戏
import random #random() 方法返回随机生成的一个实数,它在[0,1)范围内
player_imput =input("请输入(0剪刀、1石头、2布):")
player =int(player_imput)
computer = random.randint(0,2)
if (player ==0 and computer == 2) or (player ==1 and computer == 0)\
    or (player == 2 and computer == 1):
    print("恭喜你,你赢了")
elif (player ==2 and computer == 0) or (player ==0 and computer == 1)\
    or (player == 1 and computer == 2):
    print("电脑赢了,你是个猪吗")
else:
    print("平局,还不错")

The above is how to use the conditional statement if in Python. It is often used in high-level programming languages, so you must master it~

This article is reproduced from the SDK community: Latest - sdk community | Technology first

Guess you like

Origin blog.csdn.net/mynote/article/details/132940908