Understanding Python Data Types

Tutorial 3 of 5

1. Introduction

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:

  • Understanding Python's standard data types
  • How to declare and manipulate these data types
  • Best practices for using Python data types

Prerequisites:

To make the most out of this tutorial, you should have a basic understanding of Python syntax and programming concepts.

2. Step-by-Step Guide

Numbers

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

String

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!"

List

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]

Tuple

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)

Dictionary

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"}

3. Code Examples

Let's dive into some code examples for each data type.

Numbers

# 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

# String declaration
str1 = "Hello, World!"
print(str1)  # Expected output: Hello, World!

List

# List declaration
list1 = [1, 2, 3, 4, 5]
print(list1)  # Expected output: [1, 2, 3, 4, 5]

Tuple

# Tuple declaration
tuple1 = (1, 2, 3, 4, 5)
print(tuple1)  # Expected output: (1, 2, 3, 4, 5)

Dictionary

# Dictionary declaration
dict1 = {"name": "John", "age": 30, "city": "New York"}
print(dict1)  # Expected output: {'name': 'John', 'age': 30, 'city': 'New York'}

4. Summary

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:

  • Practice using these data types with different values
  • Learn how to combine different data types

Additional resources:

5. Practice Exercises

  1. Create a list and a tuple with the same elements and compare their sizes using the sys.getsizeof() function.

  2. Create a string and print each character individually.

  3. 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:

  • Try to create nested lists and dictionaries
  • Practice manipulating these data types (change, add, remove values)