Skip to main content

Learning-python:
Data Types

image for Data Types

Data types

Think of data types as different kinds of boxes you can use to store different things.

Integer

A whole number with no decimal point:

age = 25
temperature = -5
count = 0

Float

A number with a decimal point:

price = 9.99
temperature = 36.5
percentage = 0.75

Boolean

Can only be True or False. Used for yes/no questions:

is_raining = True
is_student = False

String

Text enclosed in quotes:

name = "Alice"
city = 'London'
message = """This is
a multi-line
string"""

List

An ordered collection of items in square brackets. Lists can hold different types of data and you can change them:

fruits = ['apple', 'banana', 'cherry']
numbers = [1, 2, 3, 4, 5]
mixed = [1, 'hello', 3.14]

Tuple

Like a list but in parentheses. Once created, you cannot change it:

rgb = (255, 0, 128)
days = ('Monday', 'Tuesday', 'Wednesday')

Dictionary

Stores pairs of labels and values in curly braces. You look up information by its label (called a "key"):

person = {'name': 'Alice', 'age': 25, 'city': 'London'}

To get a value, use its key:

print(person['name'])  # Shows Alice

Set

An unordered collection of unique items. Duplicates are automatically removed:

numbers = {1, 2, 3, 3, 4}  # Becomes {1, 2, 3, 4}

NoneType

Represents "nothing" or "no value". Useful as a placeholder:

result = None

Finding out data types

Use type() to see what kind of data something is:

print(type(25))           # Shows <class 'int'>
print(type("hello"))      # Shows <class 'str'>
print(type([1, 2, 3]))    # Shows <class 'list'>

Casting

Casting means converting data from one type to another:

age = "25"              # This is text
age = int(age)          # Now it's a number