Week 1: Conditionals - Lecture

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards

What are conditionals?

Statements that run if a certain condition is true

2
New cards

Symbols for asking conditional questions:

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

==

Equal to

!=

Not equal to

3
New cards

Assignment vs equality:

Single sign (=) represents assigning a value

Double sign (==) represents comparing two values (if they’re the smame)

4
New cards

How do you start a if statement

Using the if keyword:

if x < y:

then the following code block executes.

5
New cards

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

6
New cards

Boolean expresssion

Expression that evaluates to either true or false

7
New cards

Importance of well-designed code

As programs get more complex, programs need to be well-designed.

8
New cards

What is elif (short for else if)

If statement that ONLY runs if the previous if statement is false

9
New cards

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.

10
New cards

What is else

A catch-all that assumes a condition is true if all previous conditions have been ruled out.

11
New cards

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:

12
New cards

How do you check for equality and inequality in python?

You check for equality using == and for inequality using !=.

13
New cards

What does python use to define blocks of code

It uses indentation, rather than curly braces like other programming languages.

14
New cards

How can you check if a value is greater/less than but also less/greater than something?

if 90 <= score <= 100:

15
New cards

What happens if you use a series of if statements instead of elif?

Multiple conditions can be true, leading to unexpected results.

16
New cards

Conditional statements are _______________

mutually exclusive (don’t happen at the same time)

17
New cards

What is parity

Refers to whether a number is even or odd

18
New cards

Modulo operator (%)

calculates the remainder when dividing one number by another:

5 % 2 = 1 (remainder of 1)

19
New cards

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.

20
New cards

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.

21
New cards

What is pythonic code?

The idiomatic way of writing code in Python, leveraging python-specifc features

22
New cards

Collapsing Conditionals

Simplifying conditionals into a single line statement, like this:

return True if n % 2 == 0 else False

23
New cards

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.