Data Types in Python

In Python, data types are like different boxes that can hold different types of things. Each type of box is best suited for certain types of items. Let’s dive deeper into some of these types and learn how to use them effectively.

Working with Integers and Floats

Integers are numbers without decimals. They are great for counting things or keeping track of whole items, like the number of students in a class.

Floats are numbers that have decimals. They are useful when you need more precision, like when you"re measuring something in centimeters or calculating a price with cents.

Example:

Python
# Integers and Floats
apples = 10          # Integer
price_per_apple = 0.75  # Float

total_cost = apples * price_per_apple  # This will give 7.5, which is a float
print("Total cost:", total_cost)

Playing with Strings

A string is a sequence of characters, like letters, numbers, and symbols, all put together in a line. Strings are used for text, like names, sentences, or any other kind of text.

You can do a lot of cool things with strings in Python, like combining them, repeating them, or picking out specific letters.

Example:

Python
# Working with Strings
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name  # Combine strings
greeting = "Hello, " * 3  # Repeat the string 3 times

print("Full name:", full_name)
print("Greeting:", greeting)

Lists: A Collection of Items

A list is like a to-do list or a shopping list where you can keep multiple items. Lists are super flexible because you can add, remove, or change items whenever you want.

Example:

Python
# Creating and Using Lists
shopping_list = ["eggs", "milk", "bread"]
shopping_list.append("butter")  # Add an item to the list
shopping_list[0] = "cheese"     # Change the first item to "cheese"

print("Shopping List:", shopping_list)

Tuples: Fixed Collections

A tuple is like a list, but once you create it, you can’t change it. This is useful when you have a collection of items that should stay the same, like the days of the week or the coordinates of a point on a map.

Example:

Python
# Creating and Using Tuples
days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

print("Days of the Week:", days_of_week)

Sets: Unique Collections

A set is a collection of items where each item must be unique. Sets are great for storing items where duplicates don’t make sense, like a collection of unique numbers or names.

Example:

Python
# Creating and Using Sets
unique_numbers = {1, 2, 3, 4, 4, 5}  # Duplicate "4" will only appear once

print("Unique Numbers:", unique_numbers)

Dictionaries: Key-Value Pairs

A dictionary is like a real dictionary where each word has a definition. In Python, you use a key to look up a value. This is really handy when you want to store information about something, like a person’s name and age.

Example:

Python
# Creating and Using Dictionaries
student = {
    "name": "Alice",
    "age": 20,
    "grades": [85, 90, 92]
}

print("Student Name:", student["name"])
print("Student Age:", student["age"])
print("Student Grades:", student["grades"])

Using Type Conversion

Sometimes, you might need to change one type of data into another. For example, you might want to turn a number into a string so you can combine it with other text. Python makes it easy to convert data types.

Example:

Python
# Converting Data Types
age = 21
age_str = str(age)  # Convert integer to string

print("I am " + age_str + " years old.")

Understanding Mutability

In Python, some data types can be changed after you create them, and some cannot. If you create a list, you can add or remove items from it, but if you create a string, you can’t change it directly. Knowing whether a data type is mutable or immutable helps you avoid errors in your code.

Example:

Python
# Mutable vs Immutable
my_list = [1, 2, 3]
my_list.append(4)  # You can change lists

my_string = "Hello"
# my_string[0] = "J"  # This will cause an error because strings are immutable

print("Updated List:", my_list)
print("String:", my_string)

Special Data Types

Python also provides several special data types that have specific uses. These include:

None

None is a special type that represents the absence of a value or a null value. It's commonly used to signify that a variable has no value.

Example:

Python
# Using None
result = None
if result is None:
    print("No result yet.")

Complex Numbers

Complex numbers have a real part and an imaginary part. They are used in mathematical computations involving complex numbers.

Example:

Python
# Creating and Using Complex Numbers
complex_number = 3 + 4j
print("Real part:", complex_number.real)
print("Imaginary part:", complex_number.imag)