Chapter 5: Data Types
In this chapter, “Mastering Python Fundamentals”, we’ll explore Python’s data types, which are essential for handling different kinds of data in your programs. Data types define the nature of the data that can be stored and manipulated within a program. Python supports several built-in data types like numbers, strings, lists, tuples, sets, and dictionaries. Understanding these data types and how to use them effectively will enhance your programming skills, allowing you to manage and organize data efficiently.
5.1 Numbers
Numbers are one of the most basic data types in Python. They include integers, floating-point numbers, and complex numbers.
5.1.1 Integers
Integers are whole numbers without a fractional part.
Example:
x = 10
y = -5
print("x:", x)
print("y:", y)
- Explanation:
x
andy
are integer variables.10
and-5
are examples of positive and negative integers.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
integer_example.py
. - Run the program by typing:
python integer_example.py
- The output will display
x: 10
andy: -5
.
5.1.2 Floating-Point Numbers
Floating-point numbers are numbers with a decimal point.
Example:
a = 3.14
b = -0.001
print("a:", a)
print("b:", b)
- Explanation:
a
andb
are floating-point variables representing numbers with decimal points.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
float_example.py
. - Run the program by typing:
python float_example.py
- The output will display
a: 3.14
andb: -0.001
.
5.1.3 Complex Numbers
Complex numbers consist of a real part and an imaginary part.
Example:
c = 2 + 3j
print("Complex number:", c)
- Explanation:
c
is a complex number where2
is the real part and3j
is the imaginary part.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
complex_example.py
. - Run the program by typing:
python complex_example.py
- The output will display
Complex number: (2+3j)
.
5.2 Strings
Strings are sequences of characters enclosed in quotes.
Example:
greeting = "Hello, World!"
name = 'Alice'
print(greeting)
print(name)
- Explanation:
greeting
andname
are string variables. Strings can be enclosed in either double quotes (" "
) or single quotes (' '
).
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
string_example.py
. - Run the program by typing:
python string_example.py
- The output will display
Hello, World!
andAlice
.
5.3 Lists
Lists are ordered collections of items (of any data type) enclosed in square brackets []
.
Example:
fruits = ["apple", "banana", "cherry"]
print("Fruits:", fruits)
- Explanation:
fruits
is a list containing three string items:"apple"
,"banana"
, and"cherry"
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
list_example.py
. - Run the program by typing:
python list_example.py
- The output will display
Fruits: ['apple', 'banana', 'cherry']
.
5.4 Tuples
Tuples are similar to lists, but they are immutable (i.e., they cannot be changed after creation). Tuples are enclosed in parentheses ()
.
Example:
coordinates = (10, 20)
print("Coordinates:", coordinates)
- Explanation:
coordinates
is a tuple containing two integer items:10
and20
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
tuple_example.py
. - Run the program by typing:
python tuple_example.py
- The output will display
Coordinates: (10, 20)
.
5.5 Sets
Sets are unordered collections of unique items enclosed in curly braces {}
.
Example:
unique_numbers = {1, 2, 3, 3, 4}
print("Unique numbers:", unique_numbers)
- Explanation:
unique_numbers
is a set containing the numbers1, 2, 3, 4
. Note that the duplicate3
is automatically removed.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
set_example.py
. - Run the program by typing:
python set_example.py
- The output will display
Unique numbers: {1, 2, 3, 4}
.
5.6 Dictionaries
Dictionaries are collections of key-value pairs enclosed in curly braces {}
.
Example:
student = {"name": "Alice", "age": 20, "grade": "A"}
print("Student:", student)
- Explanation:
student
is a dictionary with keys"name"
,"age"
, and"grade"
associated with their respective values"Alice"
,20
, and"A"
.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
dictionary_example.py
. - Run the program by typing:
python dictionary_example.py
- The output will display
Student: {'name': 'Alice', 'age': 20, 'grade': 'A'}
.
5.7 Type Conversion
Type conversion is the process of converting one data type to another.
5.7.1 Implicit Type Conversion
Python automatically converts one data type to another when required.
Example:
x = 10
y = 3.5
result = x + y
print("Result:", result)
print("Type of result:", type(result))
- Explanation:
- Python automatically converts the integer
x
to a float when adding it to the floaty
. The result is a float.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
implicit_conversion.py
. - Run the program by typing:
python implicit_conversion.py
- The output will display
Result: 13.5
andType of result: <class 'float'>
.
5.7.2 Explicit Type Conversion
You can manually convert one data type to another using functions like int()
, float()
, and str()
.
Example:
a = "100"
b = int(a) + 50
print("b:", b)
print("Type of b:", type(b))
- Explanation:
- The string
"100"
is converted to an integer usingint()
before adding50
. The result is an integer.
Step-by-Step Guide:
- Open your text editor.
- Type the code above into your editor.
- Save the file as
explicit_conversion.py
. - Run the program by typing:
python explicit_conversion.py
- The output will display
b: 150
andType of b: <class 'int'>
.
Understanding data types is fundamental to writing effective Python programs. Each data type has its own properties and methods that make it suitable for specific tasks. By mastering these data types and their conversions, you’ll be able to handle and manipulate data in your programs with precision and flexibility. Happy coding!