当前位置: 首页 > 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.jsqmd.com/news/18390/

相关文章:

  • 用 Python 轻松克服 PDF 指定页替换为图片的痛点难题
  • Java面试题总结
  • 读书笔记:Oracle分区技术详解
  • 2025精密光电厂家推荐:柯依努UV固化设备专业定制,品质保障!
  • 徐老师2025新版Nodejs课程含项目实战
  • Moe-ctf Misc部分题解
  • Oracle故障分析:启用与禁用表的约束是否会导致存储过程无效
  • 详细介绍:isis整体知识梳理
  • DBA必备脚本:Oracle获取正在运行SQL的字面SQL文本
  • 一文读懂字符、字形、字体
  • Moe-ctf Misc
  • 智联笔记项目——251021为分享功能添加有效期
  • 直方图
  • 深入解析:手撕哈希全家桶!unordered_map/set 底层 + 位图布隆过滤器----《Hello C++ Wrold!》(24)--(C/C++)
  • Flink 方案配置从 0 到可部署
  • 昂贵多目标优化
  • Redis为什么快 - 实践
  • 操作系统应用开发(二十一)RustDesk 域名访问故障—东方仙盟筑基期 - 详解
  • 详细介绍:【多线程】无锁数据结构(Lock-Free Data Structures)是什么?
  • 完整教程:【STM32】通用输入输出端口GPIO
  • AI元人文:岐金兰的“阴谋”
  • prufer板子
  • 军用混合现实头盔EagleEye的技术解析
  • 2025电子数据取证分析师WriteUp
  • 03.Python百行代码实现点赞系统
  • --- 常见排序算法汇总 --- - 指南
  • Search-R1论文浅析与代码实现
  • Promise多个then、catch、finally的执行结果分析与总结
  • vSAN物理磁盘故障处理
  • Ai元人文构想:技术介入人文领域的辩证思考与路径探索