Skip to content

Data Types and Structures

In programming, data type is an important concept. Variables can store data of different types, and different types can do different things.

Python has the following data types built-in by default, in these categories:

  • Text Type: str
  • Numeric Types: int, float, complex
  • Sequence Types: list, tuple, range
  • Mapping Type: dict
  • Set Types: set, frozenset
  • Boolean Type: bool

Lists are used to store multiple items in a single variable. They are ordered and changeable.

fruits = ["apple", "banana", "cherry"]
fruits[1] = "blackcurrant"
print(fruits)

Tuples are used to store multiple items in a single variable. They are ordered and unchangeable.

coordinates = (10, 20)
print(coordinates[0])

Dictionaries are used to store data values in key:value pairs. A dictionary is a collection which is ordered, changeable and does not allow duplicates.

car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(car["brand"])

title: Control Flow description: Learn about if-else statements, for loops, and while loops in Python.

Section titled “title: Control Flow description: Learn about if-else statements, for loops, and while loops in Python.”

Python supports the usual logical conditions from mathematics.

a = 33
b = 200
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

With the while loop we can execute a set of statements as long as a condition is true.

i = 1
while i < 6:
print(i)
i += 1