Variables and Data Types

Variables are like containers that hold different types of information in a program. Think of them as labeled boxes where you can store and manipulate data. In Python, creating a variable is as simple as choosing a name for it and assigning a value to it using the equal sign (=).


# Integer

age = 25

print(age)  # Output: 25


greeting = "Hello, World!"

print(greeting)  # Output: Hello, World!

Python supports several data types that you can store in variables. 

Let's explore some of the most common ones: 

Numeric Data Types:

Integer (int): This data type represents whole numbers without decimal points, such as 1, 10, or -5.

Float: Floating-point numbers are numbers with decimal points, such as 3.14 or -2.5. They allow for more precision in calculations.

# Integer

age = 25

print(age)  # Output: 25

# Float

pi = 3.14

print(pi)  # Output: 3.14


A string is a sequence of characters enclosed in single ('') or double ("") quotes. It can contain letters, numbers, symbols, or even spaces. For example, 'Hello, World!' or "Python is fun!" are strings. Strings are used to represent text in a program.


greeting = "Hello, World!"

print(greeting)  # Output: Hello, World!

name = 'Alice'

print("My name is " + name)  # Output: My name is Alice


Boolean data type has only two possible values: True or False. Booleans are often used in conditional statements and logical operations to control the flow of a program.


is_true = True

is_false = False

print(is_true)  # Output: True

print(is_false)  # Output: False


In Python, the language is dynamically typed, which means that you don't need to explicitly declare the data type of a variable. Python automatically recognizes the data type based on the value assigned to the variable. 

Here's an example that demonstrates this feature: 

# Dynamically typed variables

x = 10

y = 3.14

z = "Hello, World!"

# Checking the types

print(type(x))  # Output: <class 'int'>

print(type(y))  # Output: <class 'float'>

print(type(z))  # Output: <class 'str'>

# Modifying the value and type of a variable

x = 5.0

print(type(x))  # Output: <class 'float'>

As you can see in the example, the variables x, y, and z are assigned different values of different types without explicitly specifying their data types. Python recognizes the types as int, float, and str, respectively, based on the values assigned to them.

Dynamically typed languages, like Python, provide flexibility as variables can change their type during runtime. However, it's important to be mindful of the data type when performing operations to ensure correct results and prevent potential errors.

Understanding these data types is crucial because they determine how you store and manipulate different kinds of information in Python. Depending on the situation, you'll choose the appropriate data type to represent your data accurately.

As you progress in your Python journey, you'll learn more about how to use and manipulate variables of different data types to perform various operations and solve problems in your programs.