Skip to main content
Practice

Data Types in Python

When you start learning programming, you often encounter the term data type, which refers to the form of the data.

In programming, data types define what kind of values variables and data can have.

For instance, numerical data types handle integers (Integer) and floating-point numbers (float), whereas string data types (String) handle data composed of one or more characters.

In Python, an error occurs when performing operations between variables with incompatible data types.

Example of Data Type Mismatch Error
a = 10
b = "Hello"

# This will cause an error because you cannot add a number to a string
print(a + b)
# TypeError: unsupported operand type(s) for +: 'int' and 'str'

Let's explore the commonly used data types in Python.


Numeric Data Types

Numeric data types include int for integers and float for floating-point numbers.

Example of Numeric Data Types
a = 10  # int data type
b = 3.14 # float data type

Numerical data types support various arithmetic operations.

Example of Numeric Data Type Operations
a = 10
b = 3

print(a + b) # Outputs: 13
print(a - b) # Outputs: 7

String Data Types

String refers to data composed of one or more characters.

In Python, strings must be enclosed in either double quotes " or single quotes '.

Example of String Data Types
name = "Alice"
greeting = 'Hello, World!'

Strings can be concatenated or multiplied.

Example of String Data Type Operations
divider = "="
print(divider * 10) # Outputs: '=========='

Boolean Data Types

Boolean represents values of True and False.

True indicates a true condition, and False indicates a false condition.

Boolean data types are primarily used in conditional statements and logical operations.

Example of Boolean Data Types
is_adult = True

if is_adult:
print("You are an adult.")
else:
print("You are a minor.")

Managing Multiple Data with Lists and Dictionaries

Just as you can place multiple items in a single box in real life, Python allows you to group multiple pieces of data together.

Python provides List, Tuple, Dictionary, and Set data types for managing multiple data.


List

A List is an ordered collection of data. You can store multiple items in a list.

Lists are defined by enclosing the data in square brackets [ ], and each item is separated by a comma ,.

Example of List Data Types
fruits = ["apple", "banana", "cherry"]

print(fruits[0]) # Outputs: 'apple'

Here, the number 0 used to refer to a specific position within the array is called an index.

Indexing starts from 0, so fruits[0] refers to the first item in the fruits list, which is apple.

Similarly, fruits[1] refers to banana, and fruits[2] refers to cherry.


Tuple

A Tuple is similar to a list but is immutable once created.

Tuples are defined by enclosing the data in parentheses ( ), and each item is separated by a comma ,.

Example of Tuple Data Types
fruits = ("apple", "banana", "cherry")

# Once created, tuples cannot be changed
fruits[0] = "orange"
# TypeError: 'tuple' object does not support item assignment

Dictionary

A Dictionary stores data as key-value pairs.

Similar to an English dictionary matching words and their meanings, a Python dictionary retrieves values based on specific keys.

Dictionaries are defined by enclosing the data in curly braces { }, with each item separated by a comma ,.

Example of Dictionary Data Types
person = {"name": "John", "age": 25}
name = person["name"]

print(name) # Outputs: "John"

Set

A Set is a collection of unique, unordered data.

Sets are defined by enclosing the data in curly braces { }, with each item separated by a comma ,.

Example of Set Data Types
unique_numbers = {1, 2, 3, 3, 4}

# Outputs: {1, 2, 3, 4}
print(unique_numbers)

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.