WEEK 6
Introduction to
Python Programming
From print statements to for loops
BinaryTree
Last Week's Recap & Introduction
BinaryTree
What Is Python?
BinaryTree
Setting Up Python: BinaryTree IDE
BinaryTree
How to Get Started
Python Features & Theory
BinaryTree
Libraries
Python has a massive library ecosystem: PyTorch (ML), PyGame (games), random, math, requests, pandas, and thousands more.
Community
One of the largest programming communities in the world. Extensive documentation and solutions to nearly every common error exist online.
Syntax
No curly braces — Python uses indentation and colons. No need to declare variable types. Simpler to read than Java or C++.
Python vs. C++: Speed & Efficiency
BinaryTree
Why Python Is Slower
Why C++ Is Faster
DISCUSSION
Because Python has more forgiving syntax than Java or C++, would Python be more efficient overall?
Explain why or why not.
BinaryTree
Python Fundamentals
BinaryTree
Data Types in Python
BinaryTree
Python
# Numeric
x = 42 # int
y = 3.14 # float
# Text
name = "Hello, World!" # str
# Boolean
is_valid = True # bool
# Sequence
my_list = [1, 2, 3] # list (mutable)
my_tuple = (4, 5, 6) # tuple (immutable)
Numeric (int, float), Text (str), Boolean (bool), and Sequence (list, tuple) are the core types.
Variables: Declaration and Scope
BinaryTree
Python
# Declaring a variable (no type needed!)
my_name = "BinaryTree"
age = 16
gpa = 3.8
# Global variable (accessible anywhere)
global_var = "I am global"
def my_function():
local_var = "I only exist here" # local
print(global_var) # can access global
print(local_var)
my_function()
Variables store data. Scope determines where a variable is accessible — local (inside a function) or global.
Hello, World! & Print Statements
BinaryTree
Python
# The classic first program
print("Hello, World!")
# Printing a variable
message = "Hello from BinaryTree!"
print(message)
# Printing multiple values
first = "Binary"
last = "Tree"
print(first, last) # outputs: Binary Tree
Always put strings in quotes. When printing a variable, do NOT use quotes around the variable name.
Task: Store and Print
BinaryTree
Store "Hello, World!" into a variable, then print it.
Solution
greeting = "Hello, World!"
print(greeting)
# Note: Use double quotes for strings.
# Single quotes are reserved for single characters (chars in other languages).
User Input with input()
BinaryTree
Python
# The input() function gets text from the user
name = input("What is your name? ")
print("Hello,", name)
# Input always returns a string!
age_str = input("How old are you? ")
age = int(age_str) # Convert to integer
print("In 5 years you will be", age + 5)
input() returns a str. Convert to int with int() or float with float() before doing math.
Math Operators & Assignment Operators
BinaryTree
Python
# Math operators
x = 10 + 3 # addition = 13
x = 10 - 3 # subtraction = 7
x = 10 * 3 # multiplication = 30
x = 10 / 3 # division = 3.333...
x = 10 // 3 # floor division = 3
x = 10 % 3 # modulus (remainder) = 1
x = 10 ** 2 # exponentiation = 100
# Assignment operators (shortcuts)
x += 5 # same as x = x + 5
x -= 2 # same as x = x - 2
x *= 3 # same as x = x * 3
Use ** for exponents (not ^). Modulus % gives the remainder — useful for checking even/odd.
If-Else Conditionals
BinaryTree
Python
number = int(input("Enter a number: "))
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
# Checking even or odd
if number % 2 == 0:
print("Even")
else:
print("Odd")
elif = "else if". Indentation defines the block — Python uses whitespace, not curly braces.
Task: Odd or Even Checker
BinaryTree
Using arithmetic operators and if-else conditionals, write a program that tells whether a user-entered integer is even or odd.
Solution
n = int(input("Enter an integer: "))
if n % 2 == 0:
print(n, "is even")
else:
print(n, "is odd")
Conclusion
BinaryTree
Thank You!
That wraps up Week 6. See you next week!
Next: Week 7: Intermediate Python Programming
BinaryTree