= "Alice"
name = 25
age = 1.68
height = True
is_student
print(name)
print(age)
print(height)
print(is_student)
Alice
25
1.68
True
Python is a high-level, general-purpose programming language known for its readability and simplicity. It was created by Guido van Rossum and first released in 1991.
Python was named after the British comedy show “Monty Python’s Flying Circus,” not the snake!
Python is designed to be: - Simple to read and write: The syntax is clean and English-like. - Beginner-friendly: Python has a gentle learning curve. - Powerful and versatile: It’s used by professionals in many fields.
Here’s what Python code looks like compared to another language:
Python:
Java:
Python has rapidly become one of the most widely used programming languages in the world. Here are some of the key reasons why developers love using Python:
Feature | Python | Java | C++ |
---|---|---|---|
Syntax simplicity | ✅ Easy | ❌ Verbose | ❌ Complex |
Speed | ❌ Slower | ✅ Fast | ✅ Very Fast |
Learning curve | ✅ Gentle | ⚠️ Moderate | ⚠️ Steep |
Use cases | 🌐 Broad | 💼 Enterprise | 🕹️ Systems |
Python prioritizes developer time over machine execution speed. This makes it ideal for rapid prototyping, teaching, and solving real-world problems efficiently.
In Python, variables are used to store data, and types describe the kind of data a variable holds. Python is dynamically typed, so you don’t need to declare a variable’s type explicitly.
A variable is like a container for storing a value. You create one using the =
operator.
Python automatically assigns a type based on the value. You can use the type()
function to check a variable’s type.
Used for whole numbers (positive or negative), without decimals.
Used for decimal or fractional values.
Text enclosed in quotes. Can include letters, numbers, or symbols.
Used for logic-based values: True
or False
.
An ordered collection of items. Lists can contain values of different types.
Stores key-value pairs, like a mini-database or object.
Variables in Python can be reassigned to values of different types.
type()
to inspect variablesA list is an ordered, mutable collection of items.
It can hold values of the same type or mix different types.
# Same-type list
colors = ["red", "green", "blue"]
# Mixed-type list
person = ["Alice", 30, 5.6, True]
You can also modify lists by appending, removing, or updating elements:
In Python, a dictionary represents one item (e.g., one student). To store multiple such items, we use a list of dictionaries.
One Dictionary for One Student
student = {
"name": "Alice",
"age": 21,
"major": "Computer Science"
}
print(student["name"])
print(student["major"])
Alice
Computer Science
List of Dictionaries for Multiple Students
students = [
{"name": "Alice", "age": 21, "major": "CS"},
{"name": "Bob", "age": 22, "major": "Math"},
{"name": "Cara", "age": 20, "major": "Biology"}
]
# Accessing second student's major
print(students[1]["major"])
Math
Looping Through a List of Dictionaries
students
list.# Original list of students
students = [
{"name": "Alice", "age": 21, "major": "CS"},
{"name": "Bob", "age": 22, "major": "Math"},
{"name": "Cara", "age": 20, "major": "Biology"}
]
# Add a new student
new_student = {"name": "David", "age": 23, "major": "Engineering"}
students.append(new_student)
# Print each student's name and age
for student in students:
print(student["name"], "is", student["age"], "years old.")
Alice is 21 years old.
Bob is 22 years old.
Cara is 20 years old.
David is 23 years old.