What is Python?

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.

Key facts about Python:

  • High-level: You don’t need to manage memory or worry about hardware-level operations.
  • Interpreted: Python runs line-by-line, which makes it easier to test and debug.
  • Dynamically typed: You don’t need to declare variable types—they’re inferred at runtime.
  • Multi-paradigm: You can write code using procedural, object-oriented, or functional programming styles.

Python was named after the British comedy show “Monty Python’s Flying Circus,” not the snake!

Why Should You Care?

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:

Example: Hello, world!

Python:

print("Hello, world!")

Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

3. Variables and Types in Python

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.

What is a Variable?

A variable is like a container for storing a value. You create one using the = operator.

name = "Alice"
age = 25
height = 1.68
is_student = True

print(name)
print(age)
print(height)
print(is_student)
Alice
25
1.68
True

What are Data Types?

Python automatically assigns a type based on the value. You can use the type() function to check a variable’s type.

print(type(name))      # <class 'str'>
print(type(age))       # <class 'int'>
print(type(height))    # <class 'float'>
print(type(is_student))# <class 'bool'>
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>

Basic Types

int – Integer

Used for whole numbers (positive or negative), without decimals.

a = 42  # age, count, ID number

float – Floating Point Number

Used for decimal or fractional values.

pi = 3.14159  # measurements, scores, percentages

str – String

Text enclosed in quotes. Can include letters, numbers, or symbols.

name = "Alice"

bool – Boolean

Used for logic-based values: True or False.

is_raining = False

Container Types

list – List

An ordered collection of items. Lists can contain values of different types.

# Same-type list
colors = ["red", "green", "blue"]

# Mixed-type list
person = ["Alice", 30, 5.6, True]

dict – Dictionary

Stores key-value pairs, like a mini-database or object.

student = {"name": "Alice", "grade": 90}

Dynamic Typing

Variables in Python can be reassigned to values of different types.

x = 10         # x is an int
print(type(x))

x = "ten"      # now x is a str
print(type(x))
<class 'int'>
<class 'str'>

✅ Summary

  • Variables store data
  • Python figures out the type for you
  • Use type() to inspect variables
  • You can reassign variables to new types
  • Lists can contain mixed types, which is powerful but should be used carefully

List

A 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]
person = ["Alice", 30, 5.6, True]
print(person[0])  # "Alice"
print(person[1])  # 30
Alice
30

You can also modify lists by appending, removing, or updating elements:

person.append("New York")
person[1] = 31
print(person)
['Alice', 31, 5.6, True, 'New York']

Using Dictionaries to Represent Single and Multiple Items

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

for student in students:
    print(student["name"], "is studying", student["major"])
Alice is studying CS
Bob is studying Math
Cara is studying Biology

Exercise

  • Add a new student to the students list.
  • Print each student’s name and age.
  • Try changing a student’s major.
# 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.