This tutorial aims to guide you in understanding the five standard data types in Python: numbers, string, list, tuple, and dictionary. By the end of this tutorial, you will have a clear understanding of each data type and how to use them effectively in your Python code.
Goals:
Prerequisites:
To make the most out of this tutorial, you should have a basic understanding of Python syntax and programming concepts.
In Python, numeric data type represent the data which has numeric value. Numeric value can be integer, floating number or even complex numbers. These values are defined as int, float and complex class in Python.
Example:
# Integer number
num1 = 10
# Floating number
num2 = 10.5
# Complex number
num3 = 10 + 5j
Strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1.
Example:
str1 = "Hello, World!"
A list in Python is a collection of items. Lists are mutable, meaning they can be changed. Lists are ordered, meaning that the items have a specific order, and that order will not change. Lists allow duplicate values.
Example:
list1 = [1, 2, 3, 4, 5]
A tuple in Python is similar to a list. The difference between the two is that tuples are immutable, meaning they cannot be changed (unlike lists).
Example:
tuple1 = (1, 2, 3, 4, 5)
A dictionary in Python is an unordered collection of items. Each item in a dictionary has a key/value pair.
Example:
dict1 = {"name": "John", "age": 30, "city": "New York"}
Let's dive into some code examples for each data type.
# Integer declaration
num_int = 10
print(num_int) # Expected output: 10
# Float declaration
num_float = 10.5
print(num_float) # Expected output: 10.5
# Complex number declaration
num_complex = 10 + 5j
print(num_complex) # Expected output: (10+5j)
# String declaration
str1 = "Hello, World!"
print(str1) # Expected output: Hello, World!
# List declaration
list1 = [1, 2, 3, 4, 5]
print(list1) # Expected output: [1, 2, 3, 4, 5]
# Tuple declaration
tuple1 = (1, 2, 3, 4, 5)
print(tuple1) # Expected output: (1, 2, 3, 4, 5)
# Dictionary declaration
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1) # Expected output: {'name': 'John', 'age': 30, 'city': 'New York'}
In this tutorial, we covered the five standard data types in Python: numbers, string, list, tuple, and dictionary. You've learned how to declare and manipulate each of these data types in your Python code.
Next steps:
Additional resources:
Create a list and a tuple with the same elements and compare their sizes using the sys.getsizeof()
function.
Create a string and print each character individually.
Create a dictionary representing a student. The dictionary should include student id, name, and score.
Solutions:
import sys
list1 = [1, 2, 3, 4, 5]
tuple1 = (1, 2, 3, 4, 5)
print(sys.getsizeof(list1)) # Size of list
print(sys.getsizeof(tuple1)) # Size of tuple
str1 = "Hello, World!"
for char in str1:
print(char)
student = {"id": 1, "name": "John", "score": 90}
print(student)
Tips for further practice: