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

news/2025/10/21 13:30:53/文章来源:https://www.cnblogs.com/wzzkaifa/p/19154809

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.mzph.cn/news/942208.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用 Python 轻松克服 PDF 指定页替换为图片的痛点难题

用 Python 轻松克服 PDF 指定页替换为图片的痛点难题2025-10-21 13:21 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; dis…

Java面试题总结

一、Java集合框架是什么?说出一些集合框架的优点? 每种编程语言中都有集合,最初的Java版本包含几种集合类:Vector、Stack、HashTable和Array。随着集合的广泛使用,Java 1.2提出了囊括所有集合接口、实现和算法的集…

读书笔记:Oracle分区技术详解

我们的文章会在微信公众号IT民工的龙马人生和博客网站( www.htz.pw )同步更新 ,欢迎关注收藏,也欢迎大家转载,但是请在文章开始地方标注文章出处,谢谢! 由于博客中有大量代码,通过页面浏览效果更佳。本文为个人学…

2025精密光电厂家推荐:柯依努UV固化设备专业定制,品质保障!

2025精密光电厂家推荐:柯依努UV固化设备专业定制,品质保障! 在精密光电行业快速发展的当下,UV固化技术作为关键工艺环节,其设备性能直接影响生产效率和产品质量。随着2025年的临近,行业对UV固化设备提出了更高要…

徐老师2025新版Nodejs课程含项目实战

在数字化转型浪潮持续席卷各行各业的今天,后端开发技术栈的迭代速度令人目不暇接。而Node.js自2009年诞生以来,凭借其独特的非阻塞I/O模型和事件驱动架构,始终占据着服务器端开发的重要位置。随着2025年的到来,Nod…

Moe-ctf Misc部分题解

MISC misc入门指北 常见pdf隐写,直接复制即可 moectf{We1c0m3_7o_tH3_w0R1d_0f_m1sc3111aN3ous!!} RUSH “冲刺,冲刺!”你正走在路上,耳边传来这样的声音,还没反应过来,就被撞倒了。 你费劲地爬起来,好像看到了…

Oracle故障分析:启用与禁用表的约束是否会导致存储过程无效

我们的文章会在微信公众号IT民工的龙马人生和博客网站( www.htz.pw )同步更新 ,欢迎关注收藏,也欢迎大家转载,但是请在文章开始地方标注文章出处,谢谢! 由于博客中有大量代码,通过页面浏览效果更佳。Oracle故障分…

详细介绍:isis整体知识梳理

详细介绍:isis整体知识梳理pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco",…

DBA必备脚本:Oracle获取正在运行SQL的字面SQL文本

我们的文章会在微信公众号IT民工的龙马人生和博客网站 ( www.htz.pw )同步更新 ,欢迎关注收藏,也欢迎大家转载,但是请在文章开始地方标注文章出处,谢谢! 由于博客中有大量代码,通过页面浏览效果更佳。前天发布了…

一文读懂字符、字形、字体

一文读懂字符、字形、字体完整内容也可以在公众号「非专业程序员Ping」查看 一、引言 什么是Character?什么是Glyph?Character和Glyph是否一一对应?我们常说的Font又包含哪些东西?如果要自己实现一套文本的分词、测…

Moe-ctf Misc

MISC misc入门指北 常见pdf隐写,直接复制即可 moectf{We1c0m3_7o_tH3_w0R1d_0f_m1sc3111aN3ous!!} RUSH “冲刺,冲刺!”你正走在路上,耳边传来这样的声音,还没反应过来,就被撞倒了。 你费劲地爬起来,好像看到了…

智联笔记项目——251021为分享功能添加有效期

注:在原有增删改查,导出分享功能上强化分享功能,为分享功能添加了有效期。 在日常使用笔记系统时,我们经常需要分享笔记给他人,但永久有效的分享链接存在安全隐患。本文将介绍如何在原有笔记系统基础上,新增分享…

WPF 具有跨线程作用的UI元素

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

深入解析:手撕哈希全家桶!unordered_map/set 底层 + 位图布隆过滤器----《Hello C++ Wrold!》(24)--(C/C++)

深入解析:手撕哈希全家桶!unordered_map/set 底层 + 位图布隆过滤器----《Hello C++ Wrold!》(24)--(C/C++)pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; displ…

Flink 方案配置从 0 到可部署

Flink 方案配置从 0 到可部署2025-10-21 13:05 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important…

昂贵多目标优化

Computationally Expensive High-dimensional Multiobjective Optimization via Surrogate-assisted Reformulation and Decomposition(TEVC, 2024) 基于代理辅助重构与分解的高维计算昂贵多目标优化(TP-SAEA) 源码…

Redis为什么快 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

操作系统应用开发(二十一)RustDesk 域名访问故障—东方仙盟筑基期 - 详解

操作系统应用开发(二十一)RustDesk 域名访问故障—东方仙盟筑基期 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family:…

详细介绍:【多线程】无锁数据结构(Lock-Free Data Structures)是什么?

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

完整教程:【STM32】通用输入输出端口GPIO

完整教程:【STM32】通用输入输出端口GPIOpre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "…