1/22
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What are conditionals?
Statements that run if a certain condition is true
Symbols for asking conditional questions:
> | Greater than |
>= | Greater than or equal to |
< | Less than |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
Assignment vs equality:
Single sign (=) represents assigning a value
Double sign (==) represents comparing two values (if they’re the smame)
How do you start a if statement
Using the if keyword:
if x < y:
then the following code block executes.
Key syntax points in these conditionals:
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
No parentheses are needed for an if statement,
the colon is required at the end of an if statement
indentation is key
Boolean expresssion
Expression that evaluates to either true or false
Importance of well-designed code
As programs get more complex, programs need to be well-designed.
What is elif (short for else if)
If statement that ONLY runs if the previous if statement is false
Why is using elif better than using a bunch of if statements?
Using elif reduces the number of questions asked, as questions are only asked if the previous one is false.
Meanwhile, if you only using if statements, all the questions will STILL be asked even if the previous one was false.
What is else
A catch-all that assumes a condition is true if all previous conditions have been ruled out.
How do you combine conditions into a single if statement in python?
You can combine conditions using logical operators such as and
, or
, and not
:
if x < y or x > y:
if not x > y:
if x < y and x > y:
How do you check for equality and inequality in python?
You check for equality using ==
and for inequality using !=
.
What does python use to define blocks of code
It uses indentation, rather than curly braces like other programming languages.
How can you check if a value is greater/less than but also less/greater than something?
if 90 <= score <= 100:
What happens if you use a series of if statements instead of elif?
Multiple conditions can be true, leading to unexpected results.
Conditional statements are _______________
mutually exclusive (don’t happen at the same time)
What is parity
Refers to whether a number is even or odd
Modulo operator (%)
calculates the remainder when dividing one number by another:
5 % 2 = 1 (remainder of 1)
How can you check for even and odd?
number % 2
Here, if a number has a remainder of 0, its even. If it’s 1, it’s odd.
Does python allow the passing of variables by reference?
No python does not allow passing by reference (address of variables), everything is treated as object.
What is pythonic code?
The idiomatic way of writing code in Python, leveraging python-specifc features
Collapsing Conditionals
Simplifying conditionals into a single line statement, like this:
return True if n % 2 == 0 else False
What are match statements
Like switch in other languages, allows you to perform different actions based on a variable’s value:
match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("Slytherin")
case _:
print("Who?")
match name specifies that we are matching on the value of the variable name, and each case keyword species a value to match against.
We use the | keyword to combine cases into a single case.