这是用户在 2024-5-25 16:02 为 https://edstem.org/au/courses/14775/lessons/47624/slides/340267 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

Week 2 - Further Python
第 2 周 - 进一步学习 Python

Comparisons

  • When we compare variables, they evaluate to a boolean (True or False).

  • Here are some of the comparison operators:

    • < less than

    • > greater than

    • <= less than or equal to

    • >= greater than or equal to

    • == equal

    • != not equal

  • We can combine multiple conditions using and or or

    • and: both conditions have to be True to evaluate as True

    • or: only one condition has to be True to evaluate as True

Conditionals

if, elif and else allows us to control the execution of different segments of code.

if condition_1: # code you execute if condition 1 is true elif condition_2: # code you execute if condition 2 is true elif condition_3: # code you execute if condition 3 is true ... else: # code you execute if none of the conditions are true
if condition_1: # code you execute if condition 1 is true elif condition_2: # code you execute if condition 2 is true elif condition_3: # code you execute if condition 3 is true ... else: # code you execute if none of the conditions are true

For loops

for value in iterable: # code you want to execute
for value in iterable: # code you want to execute

While loops

while condition: # code that executes while condition is true
while condition: # code that executes while condition is true