Introduction to Python Variables (video)

A video lesson about basic variable types in Python and operations on them has been published on YouTube. In Python, variables act as containers for data values. The kind of data a variable can hold is defined by its type. Understanding these types is essential for efficient programming.

Common Variable Types

  • int – integers (e.g., 1, -3)
  • float – decimal numbers (e.g., 1.5, -4.5)
  • str – strings (e.g., “hello”)
  • list – ordered collections
  • set – unordered collections of unique elements
  • dict – key-value pairs

Integers and Floats

Numeric data in Python comes in two main flavors: integers and floats.

In the following code and in other examples, there are comments written after the # sign that Python does not recognize as commands to execute:

# Integers
age = 15
score = -3

# Floats
price = 19.99
temperature = -4.5

# Basic arithmetic
total       = 5 + 3       # 8
difference  = 10 - 2      # 8
product     = 4 * 2       # 8
quotient    = 16 / 2      # 8.0

Strings

A str is a sequence of characters enclosed in quotes. Strings are immutable, meaning they cannot be changed in place.

name     = "Alice"
greeting = "Hello, World!"

# Accessing characters and substrings
first_letter = name[0]        # 'A'
substring    = greeting[7:12] # 'World'

# Concatenation
full_greeting = greeting + " " + name

Lists

A list is an ordered collection of items (which can be of mixed types).
Lists are mutable and defined with square brackets.

numbers = [1, 2, 3, 4, 5]
fruits  = ["apple", "banana", "cherry"]

# Accessing elements
first_number = numbers[0]  # 1
second_fruit = fruits[1]   # 'banana'

# Modifying elements
numbers[2] = 99

# Adding and deleting
fruits.append("date")
del fruits[0]

Dictionaries

A dict stores data as key → value pairs and is defined with curly braces.

passenger_capacity = {"bus": 35, "train": 120, "plane": 300}

# Accessing values
bus_capacity = passenger_capacity["bus"]

# Adding / updating
passenger_capacity["ship"] = 1000

# Deleting an entry
del passenger_capacity["plane"]

# Keys and values
keys   = passenger_capacity.keys()
values = passenger_capacity.values()

Transportation Example in Action

transport_types = ["bus", "train", "plane"]
passenger_capacity = {"bus": 35, "train": 120, "plane": 300}

# Accessing data
first_transport = transport_types[0]         # 'bus'
train_capacity  = passenger_capacity["train"] # 120

# Updating data
passenger_capacity["bus"] = 40

Examples can be found on GitHub.

Full version of the lecture:

The lecture is also available in other languages.

Polski
English
Русский