Summaries
Week 2 - Further Python
第 2 周 - 进一步学习 Python
Comparisons
When we compare variables, they evaluate to a boolean (
True
orFalse
).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
oror
and
: both conditions have to beTrue
to evaluate asTrue
or
: only one condition has to beTrue
to evaluate asTrue
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