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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago