1 of 22

BinaryTree

BinaryTree

Digital Literacy Program

Digital Literacy and Digital Entrepreneurship

2 of 22

BinaryTree

Week 5

Introduction to Python Programming

3 of 22

Last Week's Recap & Introduction

  • Last week we covered web development — what makes a website good or bad, and how HTML, CSS, and JavaScript work together.
  • This week serves as an introduction to Python — starting from print statements and ending at for loops.

4 of 22

What is Python?

  • Python is a programming language created in 1991, designed to be a high-level, general-purpose language.
  • It is the most popular languages in the world — known for its readability and simple syntax, making it easy for beginners to pick up.
  • Python is great for automating repetitive tasks and plays a huge role in machine learning (PyTorch, TensorFlow).

5 of 22

Setting Up Python

  • Choose an IDE — either an online IDE or a desktop IDE (VS Code, PyCharm, etc.).
  • For this course, we'll use BinaryTree's Python IDE on our website:

https://www.binarytree.us/ide.html

6 of 22

Basic Python Theory & Features

  • Libraries: Python has a massive library ecosystem — PyTorch (ML), PyGame (games), random, and more.
  • Community: Python has one of the largest communities of any language — you can find solutions to almost any common error online.
  • File types: .py (most common), .pyo, .pyc, .ipynb (Jupyter notebooks).
  • Dynamic typing: No need to declare variable types — Python figures them out automatically.
  • integer x= 5 (not needed)\
  • x=5
  • Indentation over braces: Python uses colons and whitespace (not {}) to define code blocks.

7 of 22

Discussion

Because Python has more forgiving syntax than Java or C++, would Python be more efficient in general?

Explain why or why not.

8 of 22

Python vs. C++ – Efficiency

Python is NOT faster than C++ or Java. Here's why:

  • Execution: Python uses an interpreter (line-by-line at runtime). C++ uses a compiler (translates to machine code before runtime and optimizes).
  • Typing: Python uses dynamic typing (type checked at runtime — slower). C++ uses static typing (declared upfront — faster).
  • Memory: Python uses automatic garbage collection (slow). C++ uses manual memory management (faster, but more complex).
  • Level: Python is a high-level language — further from machine code than low-level C++.

9 of 22

Data Types

  • Numeric – int (whole numbers: 1, 26, 1000) and float (decimals: 1.1, 26.5,52.4)
  • Text – str (String): ordered sequence of characters in quotes, e.g. "Hello World"
  • Boolean – bool: True or False values only
  • Sequence data:
    • List – ordered, modifiable, enclosed in [ ]
    • Tuple – ordered, unmodifiable, enclosed in ( )

10 of 22

Variables

  • Variables are containers for data — they store information for later use.
  • Scope: the region of code where a variable is accessible.
    • Local – only accessible within a function or specific code block
    • Global – accessible anywhere in the program
  • Assign a value using = (e.g. name = "Alice"). Call it later by just typing the variable name.

x = 42 name = "Alice" is_active = True

11 of 22

Hello, World!

  • The first program every beginner writes is 'Hello, World!' — a classic starting point.
  • Use the print() function. Put your message in quotes (it's a string).
  • When printing a variable, do NOT put it in quotes.

print("Hello, World!")

message =

"Hello"

print

(message) # print a variable — no quotes!

12 of 22

Task

5 min

Store "Hello, World" into a variable, then print it.

13 of 22

Getting User Input

  • input() is the main way to acquire user input. It displays a prompt and waits for the user to type a response.
  • Input always returns a str data type — even if the user types a number.
  • Store the result in a variable to use it later in the program.

name = input("What is your name? ")�print("Hello, " + name)

14 of 22

Task

5 min

Create a program that asks the user for something to print, then prints it. Use input(), a variable, and print().

15 of 22

Python Operators

Math Operators:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulus (remainder)
  • ** exponentiation (instead of ^)

Assignment Operators:

  • = assign
  • += add and assign
  • -= subtract and assign
  • *= multiply and assign
  • /= divide and assign

16 of 22

Task

5 min

Write a program that sets any integer variable x to itself divided by 5, then adds 3 to that result.

17 of 22

Converting Between Data Types

  • Converting between types is crucial — especially when using input(), which always returns a string.
  • int(x) converts x to an integer. str(x) converts x to a string. float(x) converts to a decimal.
  • type(x) tells you the data type of x — useful for debugging.

num_str = input("Enter a number: ")�num = int(num_str) # now it's an integer

18 of 22

Task

5 min

Take a number from the user via input(), add 5 to it, then print the result.

Hint: You need to convert the input from str to int first.

19 of 22

If-Else Conditionals

  • Conditionals let you execute code based on whether a condition is true or false.
  • elif = 'else if' — allows multiple conditions in a sequence.

x = 10

if x > 5:

print("Greater than 5")

elif x == 5:

print("Equal to 5")

else:

print("Less than 5")

20 of 22

Task

6 min

Using arithmetic operators and if-else conditionals, write a program that tells whether an integer is even or odd.

Hint: use the modulus operator %.

21 of 22

Conclusion

  • Python is a beginner-friendly, high-level programming language. Easy-to-learn syntax makes it popular, but the syntax compromises raw speed.
  • C++ is faster than Python in every case due to compilation, static typing, and manual memory management.
  • This week we covered: print statements, input statements, data types, variables, operators, type conversion, and if-else conditionals.

22 of 22

BinaryTree

Thank You For Attending!

Next week: Intermediate Python — data structures, libraries, and real-world applications!