Gabriel Cseh

Understanding Basic Python Data Types

An introduction to the basic data types in Python for beginners.

Understanding Basic Python Data Types

In Python, data types are an essential concept, as they determine the kind of operations you can perform on the data. Let's explore some of the basic data types in Python.

1. Numbers

Integer (int)

Integers are whole numbers, positive or negative, without a decimal point.

age = 25
year = 2024
temperature = -5

Float (float)

Floats are numbers that have a decimal point.

price = 19.99
height = 5.75
pi = 3.14159

2. Strings (str)

Strings are sequences of characters enclosed in single, double, or triple quotes.

name = "Alice"
greeting = 'Hello, World!'
multiline = """This is a
multiline string."""

3. Boolean (bool)

Booleans represent one of two values: True or False.

is_student = True
has_license = False

4. Lists

Lists are ordered, mutable collections of items, which can be of different types. They are defined using square brackets.

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4.5, 6.7]
mixed = [1, "apple", True, 3.14]

Accessing List Elements

You can access elements in a list using their index, starting from 0.

first_fruit = fruits[0]  
# "apple"
second_number = numbers[1]  
# 2

5. Tuples

Tuples are ordered, immutable collections of items. They are defined using parentheses.

coordinates = (10.0, 20.0)
person = ("John", 25, "Engineer")

Accessing Tuple Elements

You can access elements in a tuple using their index, just like lists.

latitude = coordinates[0]  
# 10.0
name = person[0]  
# "John"

6. Dictionaries

Dictionaries are unordered collections of key-value pairs. They are defined using curly braces.

student = {
    "name": "Alice",
    "age": 25,
    "courses": ["Math", "Science"]
}

Accessing Dictionary Values

You can access values in a dictionary using their keys.

student_name = student["name"]  
# "Alice"
student_age = student["age"]  
# 25

7. Sets

Sets are unordered collections of unique items. They are defined using curly braces or the set() function.

colors = {"red", "green", "blue"}
unique_numbers = set([1, 2, 2, 3, 4])

Adding and Removing Set Elements

You can add and remove items from a set using the add() and remove() methods.

colors.add("yellow")
colors.remove("red")

Summary

  • Numbers: Integers (int) and Floats (float)
  • Strings: Text data (str)
  • Boolean: Logical values (bool)
  • Lists: Ordered, mutable collections (list)
  • Tuples: Ordered, immutable collections (tuple)
  • Dictionaries: Unordered collections of key-value pairs (dict)
  • Sets: Unordered collections of unique items (set)

Understanding these basic data types is essential for writing efficient and effective Python code. As you continue learning, you'll see how these data types are used in various programming scenarios.

Ready to practice? Try creating some variables using different data types and perform basic operations on them. Happy coding!