Python, known for its flexibility and power, supports a wide range of data types. These data types are used to store and manipulate data. For beginners, learning about Python’s basic data types and how to use them is an essential step in mastering programming. In this article, we will explore Python’s core data types and examine how they are used with example code snippets.
1. Numeric Data Types
Python has three main numeric data types: integers, floating-point numbers, and complex numbers.
Integers
Integers include both negative and positive whole numbers.
x = 10
y = -3
print(type(x)) # <class 'int'>
print(type(y)) # <class 'int'>
Floating-Point Numbers
Floating-point numbers represent numbers with decimal points.
a = 3.14
b = -2.71
print(type(a)) # <class 'float'>
print(type(b)) # <class 'float'>
Complex Numbers
Complex numbers consist of a real part and an imaginary part. The letter j
represents the imaginary part.
c = 2 + 3j
d = -1j
print(type(c)) # <class 'complex'>
print(type(d)) # <class 'complex'>
2. String Data Type
In Python, the str
data type is used to store text data.
Strings
Strings are sequences of characters enclosed in either double or single quotes.
text1 = "Hello, World!"
text2 = 'Python is fun!'
print(type(text1)) # <class 'str'>
print(type(text2)) # <class 'str'>
You can perform operations like string concatenation and slicing on strings.
3. List Data Type
Lists store multiple items in a single variable and are mutable, meaning you can change their contents.
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>
print(fruits[0]) # apple
print(fruits[1:3]) # ['banana', 'cherry']
Lists can contain elements of different data types.
fruits[0] = "mango"
print(fruits) # ['mango', 'banana', 'cherry']
mixed_list = [1, "hello", 3.14, True]
print(mixed_list) # [1, 'hello', 3.14, True]
4. Tuple Data Type
Tuples, like lists, are used to store multiple items, but they are immutable (unchangeable). Tuples are written inside parentheses.
coordinates = (10.0, 20.0)
print(type(coordinates)) # <class 'tuple'>
print(coordinates[0]) # 10.0
print(coordinates[1]) # 20.0
Tuples are useful for storing fixed sets of data.
5. Dictionary Data Type
Dictionaries (dict
) store key-value pairs and are written inside curly braces.
person = {
"name": "John",
"age": 30,
"city": "New York"
}
print(type(person)) # <class 'dict'>
print(person["name"]) # John
print(person["age"]) # 30
Dictionaries are mutable, and keys must be unique.
person["age"] = 31
print(person) # {'name': 'John', 'age': 31, 'city': 'New York'}
person["country"] = "USA"
print(person) # {'name': 'John', 'age': 31, 'city': 'New York', 'country': 'USA'}
6. Set Data Type
Sets (set
) are collections of unique elements that are unordered. They can be created using curly braces or the set()
function.
unique_numbers = {1, 2, 3, 4, 5}
print(type(unique_numbers)) # <class 'set'>
print(unique_numbers) # {1, 2, 3, 4, 5}
Sets support mathematical set operations such as union, intersection, and difference.
unique_numbers.add(6)
print(unique_numbers) # {1, 2, 3, 4, 5, 6}
unique_numbers.remove(3)
print(unique_numbers) # {1, 2, 4, 5, 6}
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2)) # {1, 2, 3, 4, 5}
print(set1.intersection(set2)) # {3}
print(set1.difference(set2)) # {1, 2}
Conclusion
Python’s data types provide flexible and powerful tools to organize and manipulate data. Data types such as integers, floating-point numbers, strings, lists, tuples, dictionaries, and sets allow you to perform a variety of programming tasks. Understanding these data types and how to use them will help you build a strong foundation in Python programming.