Python Variables and Data Types Explained with Examples for Beginners
Introduction
Table of contents
What is a Variable?
A variable is a name used to store data in a program. Data can be number, text, or object. You can think of it as a container that holds a value and whenever we need that data we need to call that container by its name.Example
name = "Haarsh"
age = 20
In this example, name stores text, and age stores a number.
Rules for Declaring Variables
Uses of Variable Declaration in Python
- Store Data Variables keep values in memory. age = 21 You can use age later in the program
- Reuse Values Instead of writing the same value repeatedly: pi = 3.14 area = pi * r * r
- Perform Calculations Variables help in mathematical operations. a = 5 b = 3 sum = a + b print(sum)
- Store User Input name = input("Enter your name: ") print(name)
- Update Values Dynamically Variables can change during execution. score = 10 score = score + 5 Now score becomes 15.
Common Mistakes in Python Variables
- Using a Variable Before Declaring It
print(name)
name = "Rahul"
name = "Rahul"
print(name) - Forgetting Quotes for Strings
name = Rahul
Python thinks Rahul is another variable.
name = "Rahul" - Using Python Keywords as Variable Names
class = 10
Explainnation:- class is a Python keyword.
class_name = 10 - Starting Variable Name with a Number
1name = "Ali"
name1 = "Ali" - Case Sensitivity ConfusionPython treats uppercase and lowercase differently.
age = 20
print(Age)
age = 20
print(age) - Using Spaces in Variable Names
student name = "Amit"
student_name = "Amit" - Using Special Characters
❌ We cannot use special characters like &,*,-,=,@ etc.
user-name = "Sam"
✅ We only use underscrore
user_name = "Sam" Choosing Poor Variable Names
❌ This Type of variable is hard to understand by reader.a = 50000
✅ This Type of variable is easy to understand by reader.salary = 50000Overwriting Important Variables
x = 10
x = "hello"
Now x is no longer a number.Confusing in EqualTo "=" and Double EqualTo "=="
= → assignment
= = → comparison
if x = 5:
if x == 5:
What are Data Types?
Data types tell Python what kind of value is stored in a variable. It represents what kind of operation can be done on a particular data. Different data types are used for different kinds of data.Common Python Data Types
Example
x = 10
y = 3.5
message = "Hello, Python"
is_student = True
Here:
x is an integer.y is a float.
message is a string.
is_student is a boolean.
Checking the Data Type
You can check the data type of a variable using the `type()` function. Example
num = 5
print(type(num))
This will show that `num` is of type `int`.
Taking Input from the User
Python allows you to take input from the user using the `input()` function. But remember, input is always taken as text by default.Here is the Example:
name = input("Enter your name: ")
print("Hello", name)
Type Conversion
If you want to perform calculations, you must convert text input into a number using `int()` or `float()`. Example
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
This program takes two numbers from the user and adds them.
Full Example
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("Next year, you will be", age + 1)
This example shows how variables, data types, input, and type conversion work together.