Lessons → Python Fundamentals → Lesson 04
BEGINNER

Variables & Data Types

Learn how to store data in variables and understand the most commonly used Python data types.

📖 20 min   •   📝 10 Questions

What is a Variable?

A variable is a name used to store a value in a Python program.

You can think of a variable as a container that holds some information.

Python
name = "Alex"
age = 18

In this example, name stores a string and age stores an integer.

Creating Variables

Python does not require a separate command to declare a variable.

Python
student = "Rahul"
marks = 85
price = 99.50

Python automatically determines the type of value stored in the variable.

Variable Naming Rules

Follow these basic rules when creating variable names:

01. Start with a letter or underscore

Example: name, _value

02. Do not start with a number

1name is not a valid variable name.

03. Avoid spaces

Use student_name instead of student name.

04. Python is case-sensitive

age and Age are different.

Python Data Types

A data type tells Python what kind of value is stored.

🔢

int

Integer numbers.

age = 18
🔢

float

Decimal numbers.

price = 99.5
📝

str

Text or characters.

name = "Alex"
✅

bool

True or False values.

is_student = True

Integer — int

The int type represents whole numbers without a decimal point.

Python
age = 18
marks = 95

print(age)
print(marks)

Float — float

The float type represents numbers containing a decimal point.

Python
price = 99.50
height = 5.8

print(price)
print(height)

String — str

A string is a sequence of characters enclosed inside quotes.

Python
name = "Sudhanshu"
city = "Patna"

print(name)
print(city)

Boolean — bool

A Boolean value can be either True or False.

Python
is_student = True
is_teacher = False

print(is_student)
print(is_teacher)

Checking Data Type

Python provides the type() function to find the data type of a value.

Python
age = 18
name = "Alex"

print(type(age))
print(type(name))

Output

<class 'int'>
<class 'str'>

Type Conversion

Type conversion means changing a value from one data type to another.

Python
number = "25"

number = int(number)

print(number)

Common conversion functions include:

int()

Converts a value to an integer when possible.

float()

Converts a value to a floating-point number.

str()

Converts a value to a string representation.

Quick Quiz

Test your understanding of variables and data types.

1. Which data type is used for decimal numbers?

Finished this lesson?