Categories: python

Python Variables

A variable is a name that refers to a memory location. A Python variable, also known as an identifier, is used to store data. Python is not a “statically typed” language. We do not need to declare variables or their types before using them. When we first assign a value to a variable, it is created. A variable in Python is the name given to a memory location. It is the fundamental storage unit in a program.

There are some rules to assign a name to the variable.

Python variables creation rules:
  • A variable name must begin with a letter or an underscore.
  • A variable name must not begin with a number.
  • Only alphanumeric characters and underscores (A-z, 0-9, and _) are permitted in variable names.
  • The case of variable names matters.
  • Reserved words (keywords) cannot be used to name variables.
Declaring Variable and Assigning Values:

Python does not require us to declare variables before using them in our code. It enables us to create a variable at the appropriate time.

In Python, we don’t need to declare variables explicitly. When we assign a value to a variable, it is automatically declared.

To assign a value to a variable, use the equal (=) operator. For instance,

# declaring and assigning the var
Marks = 100

# display
print( Marks)

Output:

100
Assigning different values to multiple variables:

Python allows adding different values in a single line with “,” operators.

name, marks, age = "Rabecca Fatima", 80.2, 19

print(name)
print(marks)
print(age)

Output:

Rabecca Fatima
80.2
19
Use of + operator with different variables:
a = 10
b = 20
c = "Hello"
d = "World"
print(a+b)
print(c+d)
print(c+a)

Output:

30
Hello World
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Global and Local Python Variables:

Local variables are those defined and declared within a function. This variable cannot be accessed outside the function.

# This function uses local variable s
def f():
 s = "Hello World"
 print(s)


f()

Global variables are those that are defined and declared outside a function and must be used within one.

# This function uses global variable s
def f():
 print(s)

#Global 
s = "Hello World"
f()
Delete a variable:

We can delete the variable using the del keyword.

x = 6  
print(x)  
# deleting a variable.   
del x  
print(x) 

Output:

6
Traceback (most recent call last):
File "<string>", line 6, in <module>
NameError: name 'x' is not defined

Note: also read about Python operators

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago