In this chapter, we are going to learn and practice variables in Python. Variables are one of the most important concepts in programming because they allow us to store, access, and manipulate data while a program is running. Whether you want to perform calculations, process user input, or build real-world applications, variables play a crucial role in almost every Python program.
Before learning Python Variables, make sure you understand the basics of Python, including working modes, writing and executing programs, and the basic structure of a Python program. These concepts will help you understand variables more easily.
If you haven’t studied them yet, we recommend going through the following chapters first:
- Working Modes in Python
- First Python Program in Python
- Structure of a Python Program
Variables
A variable is a named memory location used to store data in a program. It acts like a container that holds a value, which can be changed later if needed.
Example:
x = 42
y = 35
In the above example:
- x and y are variable names.
- 42 and 35 are the values stored in the variables.
- Python reserves memory to store these values.
- The values can be used or changed later in the program.
Note: The assignment operator (=) is used to assign a value to a variable.
Example:
x = 42
Here, the value 42 is assigned to the variable x.
How to change value of a variable?
name = “techtipnow”
print(name)
name = “best for python”
print(name)
Output:
techtipnow
best for python
How to Assign Same value to Different variables in python
x = y = z = 10
print(x,y,z)
Output:
10 10 10
In the above example:
The statement x = y = z = 10 assigns the value 10 to all three variables: a, b, and c.
All variables refer to the same value (10) in memory.
How to Assigning Different Values to Different Variables in Python
x, y, z = 10, 20, 30
print(x,y,z)
Output:
10 20 30
In the above example:
The statement x,y,z = 10,20,30 assigns x = 10, y = 20, z = 30 (values are assigned in order)
Constants
- A constant is a value that is not intended to change during the execution of a program.
- In Python, constants are created by writing variable names in UPPERCASE letters.
Example
PI = 3.14
GRAVITY = 9.8
print(PI)
print(GRAVITY)
Output:
3.14
9.8
Note:
Although Python allows the value of a constant to be changed, programmers follow the convention of writing constants in uppercase to indicate that they should remain unchanged.
Rules and Naming convention for variables and constants:
- Composition: Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.
- Starting Character: An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
- Keywords: Keywords cannot be used as identifiers.
- Special Symbols: We cannot use special symbols like !, @, #, $, % etc. in our identifier.
- Length: Identifier can be of any length.
- Case Sensitivity: Python is a case-sensitive language. This means Variable and variable are not treated the same.
- Meaningful Naming: Always name identifiers so that they make sense. While a statement like c = 10 is valid, writing count = 10 makes much more sense and makes it easier to figure out what the code does, even when you look back at it after a long gap.
- Multi-word Separation: Multiple words within a name can be separated using an underscore—for example, this_is_a_long_variable.
Input and Output in Python

How to Input a value in a variable in Python
- In Python, the input() function is used to take input from the user through devices like a keyboard.
- By default, all input values are treated as strings (text).
- If required, we can convert the input into other data types like int, float, etc., using type conversion functions.
Examples
name = input(“Enter your full name: “)
marks = input(“Enter your marks: “) # stored as string
marks = int(input(“Enter your marks: “)) # converted to int
Output in Python
- Python uses the print() function to display output on the screen.
- It can display text, variables, or the result of expressions.
- The expression is evaluated first, and then the result is shown as output.
Examples of Input and Output
Write a program to accept length and breadth of rectangle. Calculate and display its Area and Perimeter.
L = int(input(“Enter Length: “))
B = int(input(“Enter Breadth: “))
Area = L * B
Perimeter = 2 * (L + B)
print(“Area:”, Area)
|print(“Perimeter:”, Perimeter)


