1 of 16

Binary Tree

Digital Literacy

2 of 16

Week 4

Basic Programming Fundamentals (Python)

3 of 16

Basic Fundamentals

In Python we use many different methodologies and logic to make things happen. First we can start out with one of the most useful features of Python: printing code to the console.

Try it for yourself!

4 of 16

Booleans

Booleans are simple and easy to use concepts in programming. A boolean represents an idea of “true” or “false.” While writing any program, we often want to execute different code in different situations. Booleans help our code to do just that easy and effective. More often, a boolean value is returned as a result of some kind of comparison operations.

There are 2 important keywords: True and False

5 of 16

Booleans In Action

  • We can use booleans to create a conditional for an action to occur
  • For example, we can have a boolean which can be inside an if statement (conditional), and use that to cause another action
  • Booleans help control program flow by determining whether a block of code executes based on true or false conditions.
  • They are often used to evaluate comparisons, such as x > y, to decide the outcome of decisions in a program.

6 of 16

Variables

Variables are an important part of coding. Variables allow us to store both individual data or large amounts of it. Variables also have different types. For instance for words are strings, numbers are integers and finally come lists which have more than one value.

7 of 16

Operators

Operators are special symbols in Python that are used to perform arithmetic or logical computations. The values on which operation is to be done are called operands, while the operation is denoted by its operator (eg. +, -, /, *, %, etc.) There is a list of some common operators:

8 of 16

Repeated Actions

We can perform actions over and over again in Python. To do this we can use loops. Some common loops are for loops and while.

9 of 16

Conditionals in Python

  • Conditionals are always followed by a colon (:)
  • The line after a conditional is ALWAYS indented
  • Three conditionals: if, elif, and else
  • If conditionals check if something is true. For example, consider the code on the next slide:

10 of 16

Conditionals in Python

The code above sets x as a random integer between 0 and 10. If x is equal to 7 when the code is run, then “hi!” will be printed in the console. Otherwise, nothing will happen.

In this case, x = 7, so ‘hi!’ was printed.

In this case, x was not equal to �7, so nothing happened.

11 of 16

Conditionals in Python

  • Next is the elif (else-if) conditional. This conditional can only be run if there is a preceding if conditional statement.
  • The elif conditional tests for a condition in the event the previous condition is not true. Example:

In this case, x was not equal to �7, meaning the first condition was false. However, x was equal to 4, indicating that the elif condition was true and so it ran.

12 of 16

Conditionals in Python

  • Lastly, we will consider the else conditional.
  • The else conditional, like the elif conditional, can only be used if there is a preceding if conditional. However, there does not need to be a preceding elif conditional.
  • The else conditional is only run in the event that the preceding if (and elif if they are used) conditionals are not true.
  • The else conditional is not followed by a condition: it only runs in the event the other conditions are not true.

13 of 16

Conditionals in Python

In the code above, the program first checks if x is equal to 7. If it is, then it will “print” hello and stop. However, if x is not equal to 7, then the program will check the else condition and print “?”

As we can see, the randomly selected integer x was not 7, because the output of this program was “?”

14 of 16

Mini Activity

15 of 16

Mini Activity

Search for Relevant Code

  • Use GitHub or Stack Overflow to find small programs or code snippets related to conditionals (search terms: "if-else examples," "switch case examples," or "conditional logic").

Identify the Purpose

  • Determine the goal of the code snippet. What problem is it solving?
  • Look for context—e.g., is the conditional part of a user input validation, feature toggle, or algorithm?

Break Down the Logic

  • Analyze how the conditional statements are structured (e.g., if-else, switch, or ternary operators).
  • Note any nested conditionals and their purpose within the larger logic.

Evaluate the Operators

  • Identify the operators used (==, >=, &&, ||, etc.).
  • Understand how they contribute to the logic. Are they comparing values, combining conditions, or negating outcomes?

16 of 16

Navigating Github!

(This is an in-lesson activity)

Share screen and explore code snippets on Github!