Python基础教程 – 分支语句

作者 : 慕源网 本文共3801个字,预计阅读时间需要10分钟 发布时间: 2021-10-2 共88人阅读

决策是所有编程语言中最重要的话题。它允许我们为特定决策运行特定的代码块。Python 在决策过程中具有特定条件。条件检查是 Python 项目的支柱。现在,我们非常简要地讨论这个话题。现在,我将教你各种决策的例子和不同的方法。

开始吧,

下图清楚地了解控制结构或决策过程。现在我们将看到分支语句类型。

Python基础教程 - 分支语句

if condition

  • 此语句仅用于测试条件。

句法

Python基础教程 - 分支语句

流程图

Python基础教程 - 分支语句

if语句的简单方法

Input:

a=int(input("enter a number : "))
if a>0:
    print('postive values')

Output:
enter a number : 2
postive values
Input:

num=int(input("enter the number : "))
if (num>=0):
    print('whole number')

Output:
enter the number : 5
whole number
Input:

x=int(input("enter your age : "))
if (x>=18):
    print("you are eligible for voting")

Output:
enter your age : 21
you are eligible for voting

如果语句使用逻辑运算符(和、或),

Input:

a = 200
b = 33
c = 500
if a > b and c > a: #AND
  print("Both conditions are True")
if a > b or a > c: #OR
  print("At least one of the conditions is True")

Output:
Both conditions are True
At least one of the conditions is True
If else condition,
  • 该语句用于测试条件。
  • 当条件为真时,则执行真语句。
  • 当条件为假时,则执行假语句。

句法

Python基础教程 - 分支语句

流程图

Python基础教程 - 分支语句

简单的 if-else 语句

Input:

num=int(input("enter the age : "))
if (num>18):
    print('you can vote')
else:
    print('you are not eligible')

Output:

#1st run
enter the age 25
you can vote

#2nd run
enter the age 12
you are not eligible

使用 if-else 的直接条件 

Input:

a= int(input("Enter Id no :"))
if ( a==101) :
    print("Id is correct, please come")
else:
    print("Good bye!")

Output:

1st Run:
Enter Id no :101
Id is correct, please come

2nd Run:
Enter Id no.:206
Good bye!

使用 if 语句进行字符串匹配

Input:

word1 = input("Enter any Word : ")
word2 = input("Enter another Word : ")

if sorted(word1) == sorted(word2):
    print("Both are same")
else:
    print("NOT same words")

Output:

1st Run
Enter any Word : hello
Enter another Word : hi
NOT same words

2nd Run
Enter any Word : magic
Enter another Word : magic
Both are same

使用逻辑运算符 (AND) 进行三标记比较

Input:

mark1 = int(input("\nEnter First mark : "))
mark2 = int(input("Enter Second mark : "))
mark3 = int(input("Enter Third mark : "))

if (mark1 == mark2) and (mark2 == mark3):
    print("All marks are same")
else:
    print("NOT all Three are same")


Output:

1st Run
Enter First mark : 56
Enter Second mark : 68
Enter Third mark : 78
NOT all Three are same

2nd Run
Enter First mark : 75
Enter Second mark : 75
Enter Third mark : 75
All marks are same

寻找奇数或偶数

Input:

a = int(input("Enter the number : "))
if (a%2 == 0):
    print("The given number is EVEN")
else:
    print("The given number is ODD")

Output:

1st Run
Enter the number : 215634
The given number is EVEN

2nd Run
Enter the number : 54821
The given number is ODD

if elif else 条件

  • 三句话—— 如果
  • 再检查一次 – elif
  • 错误 – 否则

句法

Python基础教程 - 分支语句

使用 elif 的简单程序

Input:

age=int(input('enter the age is : '))
if age <= 5:
    print("free ticket")
elif age <=18:
    print("ticket Rs: 10")
elif age <=30:
    print("ticket Rs: 20")
else:
    print("ticket Rs:30")

Output:

1st run:
enter the age is 5
free ticket

2nd run:
enter the age is 12
ticket Rs: 10

3rd run:
enter the age is 55
ticket Rs:30

if – elif – else 使用逻辑运算符

Input:

age=int(input("Enter your age : "))

if age < 6:
    print('Hello little one')
elif age >= 6 and age < 10:
    print('Are you enjoying school?')
elif age >= 10 and age < 13:
    print('You are a Tween now')
elif age >= 13 and age < 20:
    print('Now you are officially a teenager')
else:
    print('Welcome to the real world')

Output:

1st Run:
Enter your age : 56
Welcome to the real world

2nd Run:
Enter your age : 5
Hello little one

3rd Run:
Enter your age : 10
You are a Tween now

使用 if-else 语句查找最大数

Input:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))

if (num1 > num2) and (num1 > num3):
   largest = num1
elif (num2 > num1) and (num2 > num3):
   largest = num2
else:
   largest = num3
print("The largest number is", largest)

Output:

1st Run:
Enter 1st number: 7
Enter 2nd number: 68
Enter 3rd number: 48
The largest number is 68

2nd Run:
Enter 1st number: 99
Enter 2nd number: 47
Enter 3rd number: 83
The largest number is 99

我们正在非常清楚地讨论 if – elif- else 语句。下一个主题是嵌套的 if-else 语句。

嵌套的 If 语句

Input:

x = 12
if x > 10:
    print("Above ten,")
    if x > 20:
        print("and also above 20!")
    else:
        print("but not above 20")
else:
    print("bye")

Output:
Above ten,
but not above 20.

嵌套 if 语句

age = int(input("Enter your age : "))

if age > 5:
       nationality = input("Enter your Nationality : ")
       if nationality.upper() == 'INDIAN' :
              print("You are eligible for Aadhar card")
       else:
              print("Not for other nationalities")
else:
       print("Age criteria not met")

Output:

1st Run
Enter your age : 23
Enter your Nationality : INDIAN
You are eligible for Aadhar card

2nd Run
Enter your age : 4
Age criteria not met

3rd Run
Enter your age : 15
Enter your Nationality : GERMAN
Not for other nationalities

我想现在,您清楚地了解 if-else(分支)语句。我举了很多例子,如果你有任何疑问,问我什么。下一节我们将讨论循环语句。


慕源网 » Python基础教程 – 分支语句

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

发表评论

开通VIP 享更多特权,建议使用QQ登录