How to Create Variables in Python?

The first concept of Python is variables. You can imagine a variable as a box where you store some value, like a number, a name, or any data. Whenever you need that value you can use the box’s name to access the data.

Python Variable Featured Image

What is a Variable?

A variable is a name given to a memory location where a value is stored. You can use or change the value by writing the variable’s name.

Example:

name = "Ram"
age = 25

Here, name and age are two variables.

  • name stores “Ram” (a string type).
  • age stores 25 (an integer type).
Academy Pro

Python Programming Course

In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.

11.5 Hrs
51 Coding Exercises
Learn Python Programming

How to Declare a Variable?

To create a variable in Python, simply write:

variable_name = value

Example:

city = "Delhi"
temperature = 38.5
is_raining = False

Rules for Naming Variables

Variable names must follow specific rules:

  • Start with a letter or an underscore (_):
    2name = "Amit" (❌ Invalid)
    name2 = "Amit" (✅ Valid)
  • Can contain only letters, numbers, and underscores:
    student_name, rollNo, marks_1 (✅ Valid)

  • No spaces are allowed:
    student name = "Ravi" (❌ Invalid)
  • Cannot be Python keywords: Keywords are reserved words like if, while, class.
  • Python is case-sensitive: Name and name are considered different variables.
    Name = "Rahul"
    name = "Rohit"

    These are two separate variables.

Python is Dynamically Typed

You do not need to define the type of a variable. Python automatically understands the data type based on the value you assign.

x = 10       # x is an integer
x = "hello"  # Now x becomes a string

Read: Features of Python

Types of Variables

Here are some common types of data you can store in variables:

TypeExampleDescription
intage = 18Whole numbers
floatpi = 3.14Numbers with decimal points
strname = "Mohan"Text, enclosed in quotes
boolis_coding = TrueBoolean values: True or False
listfruits = ["apple", "banana"]Ordered, changeable collection of items
dictperson = {"name": "Rita", "age": 22}Unordered collection of key-value pairs

Suggested Read: Data Types in Python with Examples

Changing the Value of a Variable (Reassignment)

You can change a variable’s value after it has been created.

x = 5
x = x + 2
print(x)   # Output: 7

Multiple Variables in One Line

You can assign values to multiple variables on a single line.

Different values:

a, b, c = 1, 2, 3
print(a, b, c)  # Output: 1 2 3

Same value:

x = y = z = 100
print(x, y, z)  # Output: 100 100 100

Checking Variable Type

Use the type() function to find out a variable’s data type.

name = "Radha"
print(type(name))  # Output: <class 'str'>

marks = 90.5
print(type(marks))  # Output: <class 'float'>

Constants in Python

Python does not have a special syntax for constants. However, there’s a common convention:

  • Constant variables are named in ALL CAPS.
  • We assume their values will not change during program execution.

Example:

PI = 3.14159
GRAVITY = 9.8

Best Practices for Variables

Follow these guidelines for writing clear and readable code:

  • Use meaningful names: Names like total_marks or user_name are clear.
  • Avoid short and unclear names: x, y, a1 can make code hard to understand.
  • Use snake_case: student_name and phone_number are examples of this common Python style.

Quick Practice (Guess Output)

What will this code print?

name = "Amit"
age = 30
age = age + 5
print(name, age)

Output: Amit 35

Summary

ConceptDescription
VariableA box that stores a value
Syntaxvariable_name = value
No Type Decl.Python automatically understands the type
ReassignmentYou can change the value
ConstantsUse ALL CAPS by convention

Homework for You:

Try creating these variables:

  • A variable to store your best friend’s name.
  • A variable for your birth year.
  • A variable to check if you’re a student (True or False).
  • A constant for your SCHOOL_NAME (e.g., “ABC School”).
Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Academy Pro Subscription

Grab 50% off
on Top Courses - Free Trial Available

×
Scroll to Top