Learn the basics of Python 3-Chapter 2:Control Flow

本文介绍了Python程序中的控制流,包括从上到下的执行方式,布尔表达式的使用,比较运算符,以及如何通过if、else和elif语句实现条件判断。内容涵盖了变量类型、逻辑运算和不同情况下的代码执行路径。
摘要由CSDN通过智能技术生成

1.Introduction to Control Flow 

This is the control flow of your program. In Python, your script will execute from the top down, until there is nothing left to run. It is your job to include gateways, known as conditional statements, to tell the computer when it should execute certain blocks of code. If these conditions are met,then run this function.

2.Boolean Expressions

In order to build control flow into our program, we want to be able to check if something is true or not.A boolean expression is a statement that can either be True or False.

# Example statement:My dog is the cutest dog in the world.
example_statement = "No"

This is an opinion and not a boolean expression, so you would set example_statement to "No" in the editor to the right. Okay, now it’s your turn: 

# Statement one:Dogs are mammals.
statement_one = "Yes"

# Statement two:My dog is named Pavel.
statement_two = "Yes"

# Statement three:Dogs make the best pets.
statement_three = "No"

# Statement four:Cats are female dogs.
statement_four = "Yes"

3.Relational Operators: Equals and Not Equals

# Statement one:(5 * 2) - 1 == 8 + 1
statement_one = True

# Statement two:13 - 6 != (3 * 2) + 1
statement_two = False

# Statement three:3 * (2 - 1) == 4 - 1
statement_three = True

4.Boolean Variables

True and False are the only bool types, and any variable that is assigned one of these values is called a booleanvariable.

my_baby_bool = "true"

print(type(my_baby_bool))

my_baby_bool_two = True

print(type(my_baby_bool_two))

5.If Statement 

You’ll notice that instead of “then” we have a colon, :. That tells the computer that what’s comingnext is what should be executed if the condition is met. Enter a user name here, make sure to make it a string.

user_name = "Dave"

if user_name == "Dave":
    print("Get off my computer Dave!")

user_name = "angela_catlady_87"

if user_name == "angela_catlady_87":
    print("I know it is you, Dave! Go away!")

6.Relational Operators II

Now that we’ve added conditional statements to our toolkit for building control flow

> greater than

>= greater than or equal to

< less than

<= less than or equal to

x = 20
y = 20

# Write the first if statement here:
if x == y:
    print("These numbers are the same")

credits = 120

# Write the second if statement here:
if credits >= 120:
    print("You have enough credits to graduate!")

7.Boolean Operators: and

Often, the conditions you want to check in your conditional statement will require more than one boolean expression to cover.In these cases, you can build larger boolean expressions using boolean operators.There are three boolean operators that we will cover:and or not and combines two boolean expressions and evaluates as True if both its components are True, but False otherwise.

credits = 120
gpa = 3.4

if credits >= 120 and gpa >= 2.0:
    print("You meet the requirements to graduate!")

8.Boolean Operators: or

The boolean operator or combines two expressions into a larger expression that is True if either component is True.

credits = 118
gpa = 2.0

if credits >= 120 or gpa >= 2.0:
    print("You have met at least one of the requirements.")

9.Boolean Operators: not

The final boolean operator we will cover is not. This operator is straightforward: when applied to any boolean expression it reverses the boolean value. So if we have a True statement and apply a not operator we get a False statement.

credits = 120
gpa = 1.8

if not (credits >= 120):
    print("You do not have enough credits to graduate.")
if not (gpa >= 2.0):
    print("Your GPA is not  high enough  to graduate.")
if not (credits >= 120) and not (gpa >= 2.0):
    print("You do not meet either requirement to graduate!")

10.Else Statements

else statements allow us to elegantly describe what we want our code to do when certain conditions are not met.

credits = 120
gpa = 1.9

if (credits >= 120) and (gpa >= 2.0):
    print("You meet the requirements to graduate!")
else:
    print("You do not meet the requirements to graduate.")

11.Else If Statements

An elif statement checks another condition after the previous if statements conditions aren’t met.

grade = 86

if grade >= 90:
    print("A")
elif grade >= 80:
    print("B")
elif grade >= 70:
    print("C")
elif grade >= 60:
    print("D")
else:
    print("F")

 Learn the basics of Python 3-Code Challenges:Control Flow

 Learn the basics of Python 3-Code Challenges:Control Flow (Advanced)

  • 32
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮猴的路数

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值