Introduction
In this lesson, we will delve into the fundamental data types in Python, essential for building a robust understanding of this dynamic programming language. Mastering these data types is crucial as they form the building blocks of any program. By exploring strings, integers, floats, lists, tuples, and dictionaries, we'll learn how to handle and manipulate data effectively. Additionally, we'll cover how to convert between different data types, a key skill for ensuring data compatibility and correctness in Python programs.
Basic Data Types
Python's basic data types include strings, integers, and floats, each serving different purposes.
Strings
Strings are sequences of characters used to store text. They can be defined using either single or double quotes. Strings are immutable, meaning their characters cannot be changed once the string is created.
# Defining a string
greeting = "Hello, World!"
print(greeting)
Integers
Integers are whole numbers without a decimal point. They can be positive, negative, or zero.
# Defining an integer
age = 30
print(age)
Floats
Floats are numbers that contain a decimal point. They are used for precise calculations, like those involving measurements.
# Defining a float
height = 5.9
print(height)
Using Lists and Tuples
Lists
Lists are versatile data structures that hold an ordered sequence of items. They are mutable, allowing modifications such as adding, removing, or changing elements after creation.
# Creating a list
fruits = ['apple', 'banana', 'cherry']
# Modifying a list
fruits.append('orange')
print(fruits)
Tuples
Tuples are similar to lists, but they are immutable, meaning once defined, their elements cannot be altered. Tuples are useful for fixed data collections.
# Creating a tuple
coordinates = (10.0, 20.0)
print(coordinates)
Dictionary: Key-Value Pair Structure
Dictionaries are unordered data structures that store data in key-value pairs, providing a fast way to retrieve values when you know the key.
# Creating a dictionary
student = {'name': 'John', 'age': 21, 'course': 'Python'}
# Accessing a value
print(student['name'])
Type Conversion Methods in Python
Type conversion in Python allows you to convert data from one type to another, essential for operations involving different data types.
- int(): Converts values to an integer
number = "123"
converted_number = int(number)
print(converted_number)
- float(): Converts values to a float
num_str = "123.45"
converted_float = float(num_str)
print(converted_float)
- str(): Converts values to a string
num = 678
converted_str = str(num)
print(converted_str)
Summary / Key Takeaways
In this lesson on Data Types in Python, you have learned:
- Strings are sequences of characters used to store text and are immutable.
- Integers represent whole numbers, while floats are used for decimal numbers.
- Lists are mutable sequences, allowing updates and changes.
- Tuples are immutable sequences, suitable for fixed collections of data.
- Dictionaries store data in key-value pairs, facilitating fast retrieval.
- Python provides type conversion functions like
int(),float(), andstr()for converting between data types, crucial for ensuring compatibility in operations.
