当前位置: 首页 > news >正文

实用指南:【ATBS with Python】QA Chap2 If-else and Flow Control

Automate The Boring Stuff with Python

Practice Questions



Chap2 If-else and Flow Control

  1. What are the two values of the Boolean data type? How do you write them?

True and False


  1. What are the three Boolean operators?
    and, or ,not

  1. Write out the truth tables of each Boolean operator (that is, every possible combination of Boolean values for the operator and what they evaluate to).
andTrue and TrueTrue
True and FalseFalse
False and TrueFalse
False and FalseFalse
orTrue or TrueTrue
True or FalseTrue
False or TrueTrue
False and FalseFalse
notnot TrueFalse
not FalseTrue

4. What do the following expressions evaluate to?

(5 > 4) and (3 == 5) : False
not (5 > 4) : False
(5 > 4) or (3 == 5) : True
not ((5 > 4) or (3 == 5)) : False
(True and True) and (True == False) : False
(not False) or (not True) :True

  1. What are the six comparison operators?
    ==, !=, <, >, <=, >=

  1. What is the difference between the equal to operator and the assignment operator?

The equal to operator asks whether two values are the same as each other.

The assignment operator puts the value on the right into the variable on the left


  1. Explain what a condition is and where you would use one.

A condition is a statement or expression that evaluates to either true or false, often used to control the flow of a program. Conditions are commonly used in if statements, loops, and switch cases to determine which code should run based on specific criteria.


  1. Identify the three blocks in this code:
spam = 0
if spam == 10:
print('eggs')
if spam > 5:
print('bacon')
else:
print('ham')
print('spam')
print('Done')
  • Block 1: Outer if Statement
if spam == 10:
print('eggs')
if spam > 5:
print('bacon')
else:
print('ham')
print('spam')

This is the outermost block, controlled by the condition spam == 10. It includes all indented lines beneath it.

  • Block 2: Inner if-else Statement
if spam > 5:
print('bacon')
else:
print('ham')

Nested within the first block, this if-else structure checks the condition spam > 5. The indented lines under each clause form sub-blocks within this block.

  • Block 3: Unconditional print Statement
print('Done')

This line is outside all conditional blocks and executes regardless of the conditions above. It is not indented, marking it as separate from the other blocks.


  1. Write code that prints Hello if 1 is stored in spam, prints Howdy if 2 is stored in spam, and prints Greetings! if anything else is stored in spam.
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else: print('Greetings!')
http://www.zskr.cn/news/26243.html

相关文章:

  • Java面试题总结
  • 读书笔记:Oracle分区技术详解
  • DBA必备脚本:Oracle获取正在运行SQL的字面SQL文本
  • 直方图
  • 昂贵多目标优化
  • 详细介绍:【多线程】无锁数据结构(Lock-Free Data Structures)是什么?
  • 军用混合现实头盔EagleEye的技术解析
  • --- 常见排序算法汇总 --- - 指南
  • Search-R1论文浅析与代码实现
  • 2025年10月仓储管理系统推荐榜:鸿链云仓领衔对比评测排行
  • 一款优秀笔记软件的自我修养 - 实践
  • NITEX:构建时尚新供应链的数字平台与技术架构
  • 电子人速囤!正点原子万用表,电烙铁,电桥镊子等商品!
  • 在Java中,如何实现封装
  • 2025年10月超声波清洗机厂家排行:十家主流企业深度评测
  • 详细介绍:rabbitMQ续谈
  • 2025年10月超声波清洗机厂家推荐榜:十强对比评测
  • 2025年10月长白山旅游度假酒店推荐:口碑榜与实景对比排行
  • Reactor 模式结合 epoll
  • 2025年10月不锈钢水箱厂家排行:十家对比评价
  • skynet.dispatch 使用详解
  • 元推理:自指生产力,自洽生产关系
  • skynet.start 的作用详细解析
  • 深入解析:计算机网络物理层
  • 第三次作业-结对项目
  • 【2025-10-20】连岳摘抄
  • 2025无人机在农业生态中的应用实践
  • 2025年10月代理记账公司实力榜:五强对比评测与合规选型指南
  • 2025年10月代理记账公司对比评测榜:秀之宇领衔全生命周期服务排名
  • 2025年国内中厚板供应推荐:全流程服务助力工程高效交付