Python Variables

Python variable is a container that will store value. Python is not referred to as statically typed. There is no need to declare the variables before using them or declare their type.

Therefore, a variable is formed after we provide a value to it. A Python variable is a name that has been given to a memory location. It is considered the fundamental unit of storage in the program. Let us check out this article to define a variable in Python.

Example Of Variable In Python

After an object is given a variable. It will known by that name. According to Layman’s terms, variables in Python have store values.

In the example given below, Skill Vertex is stored as the variable var. Thus, the stored information will be printed.

Var = "Skillvertex"
print(Var)

Output

Skillvertex

Keep in mind, that the stored value can be altered during the program execution. The Variables in Python are just a name that is assigned to a memory location. Hence, every operation that is being done on a variable will affect its memory location.

What are the Rules For Python Variables?

  • A Python Variables name will start with a letter or an underscore character.
  • A Python variable cannot begin with a number.
  • It has alpha-numeric characters and underscores such as (A-z, 0-9, and _ ).
  • It’s case-sensitive (name, Name, and Name are the three different variables.
  • The reserved words in Python cannot be included as the variable name in Python.
# valid variable name
skill = 1
Skill= 2
Sk_k_il = 5
_skill = 6
skill_ = 7
_SKILL_ = 8
 
print(skill, skill, Sk_k_il )
print(_skill, skill_, _SKILL_)

Output

1 2 5
6 7 8

What is the Variable Assignment In Python?

Defining variables in Python by providing a number, floating-point number, and a string to the variable like age, salary, and a name.

# An integer assignment
age = 45
 
# A floating point
salary = 1456.8
 
# A string
name = "John"
 
print(age)
print(salary)
print(name)

Output

45
1456.8
John

What is the Declaration and Initialization Of Variable?

Refer to the example below to declare the variable, define the variable, and print the variable.

# declaring the var
Number = 100
 
# display
print( Number)

Output

100

What is Redeclaring Variables In Python?

Re-declare the variable even after declaring the variable and then define it in Python already.

# declaring the var
Number = 100
 
# display
print("Before declare: ", Number)
 
# re-declare the var
Number = 120.3
   
print("After re-declare:", Number)

Output

Before declare:  100
After re-declare: 120.3

Python Assigns Values to Multiple Variables 

Python will provide a value to several variables simultaneously with the  “=” operators

For example:

a = b = c = 10
 
print(a)
print(b)
print(c)

Output

10
10
10

Assigning different values to multiple variables

Python will add different values in a single line with the help of the “,” operator.

a, b, c = 1, 20.2, "SkillVertex"
 
print(a)
print(b)
print(c)

Output

 1,
 20.2
"Skillvertex"
 

Using the Same Name For Different Types

Python program will allow to use of the same name and can refer to the new value and type.

a = 10
a = "Skillvertex"
 
print(a)

Output

Skillvertex

How does +operator work with variables?

Python Plus will give a way to add a value if it’s a number and concatenate if it’s a string. If the variable is already formed and has a new value in the same variable.

a = 10
b = 20
print(a+b)
 
a = "Skill"
b = "Vertex"
print(a+b)

Output

30
Skillvertex

Using + Operator for Different Datatypes

a = 10
b = "Geeks"
print(a+b)

Output

TypeError: unsupported operand type(s) for +: 'int' and 'str'

What is Global and Local Python Variables?

Local Variables in Python can be defined and declared inside the function. This variable cannot be defined outside the function.’

# This function uses global variable s
def f():
    s = "Welcome skillvertex"
    print(s)
 
 
f()

Output

Welcome skillvertex

Global variables in Python are the ones that will defined and declared outside the function and will be used inside the function.

# This function has a variable with
# name same as s
def f():
    print(s)
 
# Global scope
s = "I love Skillvertex"
f()

Output

I love Skillvertex

What is Global Keyword in Python?

Python global is a keyword that will allow the user to change the variable outside of the scope. Hence, it is used to make a global variable from the non-global scope.

However, the global variable will be used inside the function only when it is required to do an assignment or to change the variable.

Rules of global keyword

  1. Local and Global Variables:
    • When you create a variable inside a function, it’s like a secret code that only that function understands. We call this a “local” variable.
    • If you create a variable outside of any function, it’s like a universal code that everyone can understand. We call this a “global” variable.
  2. Implicitly Global Variables:
    • If you only talk about (reference) a global variable inside a function, Python understands that you’re talking about the big universal code. You don’t need to declare it as global unless you want to change it.
  3. Using global Keyword:
    • If you want to change the big universal code (global variable) inside a function, you need to tell Python explicitly by using the word “global.”

Example

x = 15
 
def change():
 
    # using a global keyword
    global x
 
    # increment value of a by 5
    x = x + 5
    print("Value of x inside a function :", x)
 
 
change()
print("Value of x outside a function :", x)

Output

Value of x inside a function : 20
Value of x outside a function : 20

What is Variable Type in Python?

Data types have classification or categorization of data items. Data types will show the value that tells what operations can be done on a particular data.

Since everything is an object in Python programming, data types are classes and variables are instances (objects) of these classes.

Build-in Python Data types:

a.Numeric

b.Text Type

c. Sequence Type

d. Boolean

d. Set

e. Dictionary

Example

# numberic
var = 123
print("Numeric data : ", var)
 
# Sequence Type
String1 = 'Welcome to the Skillvertex'
print("String with the use of Single Quotes: ")
print(String1)
 
# Boolean
print(type(True))
print(type(False))
 
# Creating a Set with
# the use of a String
set1 = set("Skillvertex")
print("\nSet with the use of String: ")
print(set1)
 
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Skill', 2: 'For', 3: 'Vertex'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)

Output

Numeric data :  123
String with the use of Single Quotes: 
Welcome to the Skill vertex
<class 'bool'>
<class 'bool'>
Set with the use of String: 
{'v', 'S', 'K', 'I', 'E', 'L', 'R'}
Dictionary with the use of Integer Keys: 
{1: skill', 2: 'For', 3: 'skill'}

Creating Objects

class CSStudent:
    # Class Variable
    stream = 'cse'
    # The init method or constructor
    def __init__(self, roll):
        # Instance Variable
        self.roll = roll
 
# Objects of CSStudent class
a = CSStudent(101)
b = CSStudent(102)
 
print(a.stream)  # prints "cse"
print(b.stream)  # prints "cse"
print(a.roll)    # prints 101
 
# Class variables can be accessed using class
# name also
print(CSStudent.stream)  # prints "cse"

Output

cse
cse
101
cse

Conclusion

To conclude, this article is about Python variables, assigning values to multiple and different variables. A global and local variable in Python is also added to this. Students can understand Python variables easily through this.

Python Variables -FAQs

Q1. What are variables in Python?

Ans. A Python variable is a reserved memory location to store values.

Q2. What are the 4 variables in Python?

Ans. Integer, float, String, and Boolean

Q3. What are called variables?

Ans. A variable can be either a characteristic, number, or quantity that can be measured or counted. 

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment