Control Flow in Python

Control Flow (if, else, for, while) Control flow statements let you make decisions and repeat actions in your code. If Statements age = 18 if age >= 18: print("You are an adult.") else: print("You are a minor.") For Loops for i in range(5): print(i) While Loops count = 0 while count < 3: print(count) count += 1 Try using if, for, and while in your own programs! ← Previous: Python Data Types Next: Functions →

June 22, 2025

Dictionaries and Sets in Python

Dictionaries and Sets Dictionaries Dictionaries store key-value pairs. person = {"name": "Alice", "age": 30} print(person["name"]) Sets Sets store unique items. unique_numbers = {1, 2, 3, 2} print(unique_numbers) # Output: {1, 2, 3} Use dictionaries for key-value data, and sets for unique items! ← Previous: Lists and Tuples Next: Input and Output →

June 22, 2025

Functions in Python

Functions Functions are reusable blocks of code that perform a specific task. Defining a Function def greet(name): print(f"Hello, {name}!") Calling a Function greet("Alice") Return Values Functions can return values using the return statement: def add(a, b): return a + b result = add(2, 3) print(result) Try writing your own functions! ← Previous: Control Flow Next: Lists and Tuples →

June 22, 2025

Input and Output in Python

Input and Output Input Use input() to get user input: name = input("What is your name? ") print(f"Hello, {name}!") Output Use print() to display output (see the Printing Text lesson for more details). Try asking the user for input in your own programs! ← Previous: Dictionaries and Sets Next: Modules and Packages →

June 22, 2025

Lists and Tuples in Python

Lists and Tuples Lists Lists are ordered, changeable collections. fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) Tuples Tuples are ordered, unchangeable collections. point = (10, 20) print(point) Use lists when you need to change items, and tuples when you don’t! ← Previous: Functions Next: Dictionaries and Sets →

June 22, 2025

Modules and Packages in Python

Modules and Packages Modules A module is a file containing Python code. You can use code from other modules using import. import math print(math.sqrt(16)) Packages A package is a collection of modules in a directory with an __init__.py file. ← Previous: Input and Output

June 22, 2025

Printing Text in Python

Printing Text in Python The print() function is used to display text or other output in Python. Basic Usage print("Hello, world!") This will show the text Hello, world! on the screen. Printing Variables You can print the value of variables: name = "Alice" print(name) Printing Multiple Items Separate items with commas: print("Name:", name) Formatting Output You can use f-strings for formatted output: age = 30 print(f"{name} is {age} years old.") Try using print() with your own text and variables! ...

June 22, 2025

Python Data Types

Python has several built-in data types that are used to store different kinds of information. Understanding these is fundamental to programming in Python. 1. Numbers Python supports integers, floating-point numbers, and complex numbers. x = 5 # Integer pi = 3.14 # Float z = 2 + 3j # Complex number 2. Strings Strings are sequences of characters, used to store text. name = "Alice" greeting = 'Hello, world!' 3. Booleans Booleans represent True or False values. is_active = True is_admin = False 4. Lists Lists are ordered, mutable collections of items. ...

June 22, 2025

Variables and Assignment in Python

Variables and Assignment Variables are used to store information in Python. You assign a value to a variable using the = sign. Creating Variables x = 5 name = "Alice" Changing Values You can change the value of a variable at any time: x = 10 Variable Names Must start with a letter or underscore Can contain letters, numbers, and underscores Are case-sensitive (age and Age are different) Practice creating and using variables in your code! ...

June 22, 2025