7 Key Points about Variables in Python

Rina Mondal
3 min readDec 25, 2023

--

In Python, a variable is a symbolic name or an identifier associated with a value or a memory location. Variables are used to store and manage data just like containers with labels, holding marbles, candies and even secret things.

In this blog, we will explore everything related to variables in Python and I have also posted a youtube link below where I have described variables in details in a lucid way..

Here are some key points about variables in Python:

1. Variable Naming

i. Variable names can contain letters, numbers, and underscores.
ii. Variable names must begin with a letter or an underscore.
iii. Variable names are case-sensitive (`myVar` is different from `myvar`). iv. Avoid using reserved keywords (e.g., if, while, for, else, in etc.) as variable names and also avoid using reserved functions like print, str, etc..

# Valid variable names
my_var = 10
counter = 0
_total = 100
variable_123 = "Hello"

# Invalid variable names
123_var = 5 # Invalid: Starts with a number
total@count = 20 # Invalid: Contains '@' symbol

2. Variable Assignment

Variables are created and assigned values using the assignment operator (=).

i. Integer variable: age = 25
ii. Floating-point variable ( height = 5.9)
iii. String variable ( name = "John")
iv. Boolean variable(is_student = True)

3. Multiple Assignment

Python allows you to assign values to multiple variables in a single line.

x, y, z = 10, 20, 30 # Here x is assigned with 10, y is assigned with 20 and z is assigned with 30.
print(x,y,z)

# O/t- 10,20,30

4. Naming Convention for Constants

While Python doesn’t have true constants, variables with constant-like behavior (values that are not intended to change) are typically named using uppercase letters. Example: PI = 3.14159

5. Variable Reassignment

Values can be re assigned to variables and the variable type can be changed.

x = 5
x = "Hello" # x will be replaced by Hello (which is a string)

6. Deleting Variables:

del statement can be used to delete a variable.

my_var = 10
del my_var
# now if you try to access my_var, it will show as an error.

7. Variable Scope:

Variables can have local or global scope, depending on where they are defined.
i. Function parameters and variables defined inside functions have local scope.
ii. Variables defined outside functions or using the global keyword have global scope.

# Global variable
global_variable = "I am global"

def example_function():
# Local variable
local_variable = "I am local"
print("Inside the function:", local_variable)
print("Inside the function (accessing global variable):", global_variable)

example_function()

# Accessing the global variable outside the function
print("Outside the function (accessing global variable):", global_variable)

# Uncommenting the line below will result in an error since 'local_variable' is local to the function
# print("Outside the function:", local_variable)


#O/t:

# Inside the function: I am local
# Inside the function (accessing global variable): I am global
# Outside the function (accessing global variable): I am global

Now, let’s summarize everything we have studied so far:

# Variable examples
age = 25
height = 5.9
name = "John"
is_student = True

# Multiple assignment
x, y, z = 10, 20, 30

# Constant-like variable
PI = 3.14159

# Variable reassignment
x = "Hello"

# Deleting a variable
del height

# Displaying values
print("Name:", name)
print("Age:", age)
print("Is Student?", is_student)
print("X:", x)
print("Constants - PI:", PI)

# O/t- Name: John
Age: 25
Is Student? True
X: Hello
Constants - PI: 3.14159

Remember that clear and meaningful variable names contribute to code readability.

Practice what you have learned- https://youtu.be/_9yUZiQhVio

Download Code from Github: https://github.com/datascience-1100/Variables-in-Python.git

--

--

Rina Mondal

I have an 8 years of experience and I always enjoyed writing articles. If you appreciate my hard work, please follow me, then only I can continue my passion.