Python has many built-in data types that can be used for various purposes in programming. Let’s go over this at some of the most popular ones!
1. Numbers: int, float, complex
These are used to represent numbers, both whole (int) and decimal (float), as well as imaginary numbers (complex). For example:
# Defining variables and assigning values
num1: int = 42 # Assigning an integer value to the variable "num1"
num2: float = 3.14159 # Assigning a float value to the variable "num2"
complex_num: complex = 2 + 3j # Assigning a complex number value to the variable "complex_num"
# Printing the data type of each variable
print(type(num1)) # Output: <class 'int'> - Prints the data type of the variable "num1"
print(type(num2)) # Output: <class 'float'> - Prints the data type of the variable "num2"
print(type(complex_num)) # Output: <class 'complex'> - Prints the data type of the variable "complex_num"
2. Strings (str): a sequence of characters that can be used to represent anything from a simple message to an entire novel. For example:
# Define a variable "my_string" and assign it the value "Hello, world!"
my_string = "Hello, world!"
# Print the type of the variable "my_string" using the "type()" function
print(type(my_string)) # Output: <class 'str'>
3. Lists (list): a sequence of items that can be modified and accessed using indexing. For example:
# Creating a list with different data types
my_list = [1, 2, "three", True]
# Printing the type of the list
print(type(my_list)) # Output: <class 'list'>
4. Tuples (tuple): a sequence of items that cannot be modified once it’s created. For example:
# Creating a tuple with four elements
my_tuple = (1, 2, "three", True)
# Printing the type of the tuple
print(type(my_tuple)) # Output: <class 'tuple'>
5. Ranges (range): a sequence of numbers within a given range. For example:
# Creating a list of numbers using the range function
numbers = list(range(5)) # range function creates a sequence of numbers from 0 to 4 and converts it into a list
print(type(numbers)) # Output: <class 'list'> - prints the type of the variable "numbers" which is a list
6. Dictionaries (dict): used for storing key-value pairs in a single variable. For example:
# Dictionaries (dict): used for storing key-value pairs in a single variable. For example:
# Creating a dictionary with key-value pairs
my_dict = {"name": "John", "age": 30}
# Printing the type of my_dict variable
print(type(my_dict)) # Output: <class 'dict'>
# Output: {'name': 'John', 'age': 30}
print(my_dict)
# Output: John
print(my_dict["name"]) # Accessing the value associated with the key "name"
# Output: 30
print(my_dict["age"]) # Accessing the value associated with the key "age"
# Adding a new key-value pair to the dictionary
my_dict["occupation"] = "Engineer"
# Output: {'name': 'John', 'age': 30, 'occupation': 'Engineer'}
print(my_dict)
# Updating the value associated with the key "age"
my_dict["age"] = 35
# Output: {'name': 'John', 'age': 35, 'occupation': 'Engineer'}
print(my_dict)
# Removing a key-value pair from the dictionary
del my_dict["occupation"]
# Output: {'name': 'John', 'age': 35}
print(my_dict)
# Checking if a key exists in the dictionary
if "name" in my_dict:
print("The key 'name' exists in the dictionary")
# Output: The key 'name' exists in the dictionary
7. Sets (set): used for storing unique items in a single variable. For example:
# Sets (set): used for storing unique items in a single variable. For example:
# Creating a set of numbers
numbers = {1, 2, 3, 4, 5} # Set containing unique numbers
print(type(numbers)) # Output: <class 'set'> # Printing the type of the variable "numbers" which is a set
We can use the type() function to check the data type of any variable in Python. For example:
# Define a variable named "my_variable" and assign it the value "hello"
my_variable = "hello"
# Use the type() function to check the data type of the variable "my_variable"
# and print the result
print(type(my_variable)) # Output: <class 'str'>
Python’s built-in data types are a sweet treat for any programmer! So go ahead and indulge yourself in the world of Python’s data types you won’t regret it!