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.
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.
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:
Example: name,
_value
1name is not a valid
variable name.
Use student_name
instead of student name.
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.
age = 18
marks = 95
print(age)
print(marks)
Float — float
The float type represents numbers
containing a decimal point.
price = 99.50
height = 5.8
print(price)
print(height)
String — str
A string is a sequence of characters enclosed inside quotes.
name = "Sudhanshu"
city = "Patna"
print(name)
print(city)
Boolean — bool
A Boolean value can be either
True or False.
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.
age = 18
name = "Alex"
print(type(age))
print(type(name))
Output
<class 'str'>
Type Conversion
Type conversion means changing a value from one data type to another.
number = "25"
number = int(number)
print(number)
Common conversion functions include:
Converts a value to an integer when possible.
Converts a value to a floating-point number.
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?