1 of 14

Variables, Operators and Conditionals

2 of 14

Recap From Last Lecture

  • Computer programming is how we tell computers what to do
  • The computer language we will cover in this class is python
  • We will be using Google Colab in this course
  • https://colab.research.google.com/drive/1zIqXLcrAUxfoJzSvCQbBfVZWxd-Q2Oln?usp=sharing

3 of 14

Upcoming Schedule

4 of 14

Today's Agenda

  • Variables and Data Types
  • Arithmetic Operators
  • Logical and Relational Operators
  • Concept of Branching
  • If/Else Statements
  • Case Switch Statements

5 of 14

Variables and Naming Conventions

  • A variable is a name that refers to a value stored in computer memory.

  • In programming, there exist two standard naming conventions for variables. Snake case and Camel case.
  • Snake case means separating words using an underscore (e.g. my_name)
  • Camel case means separating words using capital letters for the first letter of each word except the first (e.g. myName)

6 of 14

Data Types and the Assignment Operator

  • In Python, variables have four primitive data types: Integer (int), Floating Point (float), String (str) and Boolean (bool).
  • You do not have to explicitly specify the data type as the compiler infers it using the value you assign
  • You can “type cast” between data types in Python using the built-in int(), float(), bool() and str() constructors, only where it makes sense.

The “=” used here is called the assignment operator and as its name explains, is used to assign values to variables

7 of 14

Arithmetic Operators

  • Arithmetic operators are used to perform mathematical operations on data.
  • In Python, there are 7 arithmetic operators:

  • Arithmetic operators can also be used in conjunction with the assignment operator to make code shorter.
  • For example, “a = a + b” can be rewritten as “a += b”

8 of 14

Logical and Relational Operators

  • Boolean Algebra is the branch of mathematics that deals with operations on logical values with binary (true or false) values
  • We are interested in using concepts from boolean algebra in our code
  • Why? How?

9 of 14

Why?

→ To keep track of T/F values within a program

→ Booleans take up very little memory

→ To create conditions that can control how a program acts in certain settings

10 of 14

How?

We can utilize logical operators like:

  •     “and” returns true if both statements are true       
    • x < 5 and x < 10
  •     “or” returns true if one of the statement is true   
    • x < 5 or x < 4
  •  “not” returns false if the result is true or false
    • not(x < 5 and x < 10)

 We can also use == (equal), != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to). These are known as relational operators.

11 of 14

Exercise: Branching

Could there be certain times when a program must act one way if something is true, and another way if not?

Think of one real-world example of a program that would require this

12 of 14

If Statements

If statements may have two more branches, only one of which is executed at a time.

The “if” branch is executed if the condition, and the “else” branch is executed if the condition is false.

13 of 14

If Statements continued

To address this we use “elif” (stands for else if)

For example, a program that sorts items into two boxes that needs to act differently when sorting odd, even, and an empty collection of objects.

Are there examples of a program needing to check more than one conditional in some process?

14 of 14

Individual Exercises