Python / Python Basics

Understanding Python Data Types

Python has five standard data types: numbers, string, list, tuple, and dictionary. This tutorial will guide you in understanding each of these data types and how to use them.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Introduces fundamental Python concepts, including syntax, variables, and data types.

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)

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help