Variables in Python

A variable in Python is a name used to store a value in a program. The value can be a number, text, or other type of data. Python automatically determines the data type when a value is assigned to a variable, making it a dynamic language that allows for flexibility in coding.

Developers can create multiple variables to hold different types of data. Additionally, they can change a variable's value throughout the program as needed, which enhances the versatility of Python in handling various data manipulations and algorithms.

Syntax

Explanation

name = "Amit"

age = 25

height = 5.8

Output

  • name stores text.

  • age stores a whole number.

  • height stores a decimal number.

Amit

25

5.8

1. What is a Variable?

A variable is a name that refers to a value stored in a program.

Syntax

variable_name = value

Input

Explanation

name = "Rahul"

age = 20

print(name)

print(age)

Output

  • name is a variable.

  • "Rahul" is its value.

  • age is another variable.

  • 20 is its value.

The = symbol is called the assignment operator.

It assigns a value to a variable.

Rahul

20

2. Creating Variables

Python variables are dynamically created when you assign a value to them, allowing you to store different types of data such as numbers, strings, lists, and more. This flexibility makes Python a powerful and efficient programming language for various applications.

Input

Explanation

student_name = "Ajay"

marks = 95

print(student_name)

print(marks)

Output

x = 10

city = "Gurgaon"

price = 99.50

Python doesn't require you to specify whether x is an integer or whether city is a string. Python identifies the type automatically from the assigned value.

Ajay

95

3. Multiple Variables

Python allows you to assign values to multiple variables in one statement, enabling you to efficiently handle multiple pieces of data simultaneously and reducing the amount of code you need to write. This feature is particularly useful when you're dealing with large datasets or when you want to initialize several variables with closely related values in a clear and concise way.

Input

name, age, city = "Amit", 20, "Gurgaon"

print(name)

print(age)

print(city)

You can also assign the same value to multiple variables:

Amit

20

Gurgaon

Output

x = y = z = 10

print(x)

print(y)

print(z)

10

10

10

Output

Input

4. Changing a Variable's Value

A variable can be assigned a new value during program execution, allowing for dynamic changes to the data being processed as the program runs. This capability is essential in algorithms that require the manipulation of information in response to user input or other events that occur during runtime.

Input

age = 20

print(age)

age = 21

print(age)

Python variables can also refer to values of different types at different times, allowing for great flexibility in programming. This dynamic typing feature makes Python an excellent choice for developers looking to build complex applications without being constrained by strict type definitions.

20

21

Output

value = 10

print(value)

value = "Hello"

print(value)

10

Hello

Output

Input

5. Using Variables in Calculations

Variables can be used in mathematical operations to represent different values that can change. This flexibility allows for more dynamic calculations and enables a wide range of mathematical functions to be performed seamlessly, enhancing the overall understanding of mathematical concepts and their applications.

Input

number1 = 10

number2 = 20

total = number1 + number2

print(total)

Another example:

30

Output

price = 500

quantity = 3

total = price * quantity

print(total)

1500

Output

Input

Contact

Reach out for AI courses and support

Email

Phone

careers@ahkacademy.in

Mo:- +91-7769920076

}

© 2025. All rights reserved.