Operators, Booleans, and Conditionals

Objectives ⟶

  • Discuss operators.
  • Understand the bool data type.
  • Understand why booleans are important in programming.
  • Discuss the operators that return boolean values.
  • Understand if/else statements.

Operators

What are they?

Operators are symbols that perform various types of opertions. The most simple example is an addition (e.g., 1 + 1), which performs an arithmetic operation.

Python operations are categorized into the following groups:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading

Booleans

Overview

A boolean is a data type that can only represent two possible values - True or False. The name boolean originates from the English mathematician George Boole.

Although having only two possible values makes booleans look trivial, booleans will end up playing a critical role in writing your code.

A boolean True is NOT the same as a string "True" (note the double quotes around the text in string "True").

Will you marry me?
Heck yes True! I'll marry you.

Loading...

Loading...

Loading...

Why are booleans so important?

Evaluation

Even the most complex boolean expressions in programming eventually boils down to a single boolean value.

Logic

It controls the flow of your program. We will soon see how if statements can conditionally run some code.

If I have over $100 in my wallet, I will go grab a Ribeye steak.

Loading...

Loading...

Loading...

Expressions

Evaluated Chunk of Code

If you're new to programming, an expression may mean the look on your face, or a group of words used to signal how you feel.

In the context of mathematics, an expression is a combination of symbols that is well-formed according to the rules that depend on the context (thank you Wikipedia).

For Us

In the context of programming, an expression is a unit of code that can be evaluated to determine its value. This definition does not deviate far away from that of mathematics. We'll come back to how math and programming are intertwined.

A boolean expression is any expression that evaluates to either True or False.

Examples of Expressions ⟶

  • 1 + 1 is an expression that evaluates to 2.
  • 575 > 500 is an expression that evaluates to True.
  • "Five" + "Guys" is an expression that evaluates to "FiveGuys".
  • hometown == "Taipei" is an expression that evaluates to either Trueor False depending on the value of the hometown variable.
  • True is an expression too.
  • And yes, 1 is another expression. Constants themselves are expressions.

Loading...

Loading...

Loading...

If...Else

Conditional Blocks

Being able to use expressions to dynamically determine a value sounds... fancy. But how exactly is it useful?

This is where the conditional statements shine. Conditional statements allow your code to make decisions based on rules.

If I can't afford a Ribeye steak, I'm heading to Panda Express.

Loading...

Loading...

Loading...

Why are some lines indented?

Code Blocks

Python uses indentation to define a block of code. What do you mean block of code? A block of code refers to one or more lines of code that belong to if/elif/else statements, a function, or a loop.

You can use however many spaces you want. However, you must be consistent in the number of spaces you use. Python will throw an error if you mix the number of spaces used for indentation.

Loading...

Conditional Statements

if

if statements always come first. You must specify a condition here (if cash > 100). An if block is required before you use elif or else. You can only have one if statment in a set of if/elif/else statements.

elif

elif statements come after the firstif block. Similar to if statements, you must specify a condition for elif statements. You can have as many elif blocks as you'd like.

else

else statements come last. else statements caputure any cases that are not satisfied by the preceding if/elif statements.

Loading...

Loading...

Loading...

Loading...

Loading...