Python Variables with Examples and Types|

In Python, the term “variable” is a very important term. it plays a crucial role not only in Python programming but in all programming languages. in this article, I will explain python variables and python variable types, python variable declaration, python variable initialization and many more. so let’s get started…..

What is a variable in Python?

In simple words, To define variable in Python, variables are the name of memory locations which is used in programming to store the data, information such as numbers, and text that can be used later to perform various tasks.

to understand the variable better, assume there is an empty box that is used to store something like coconut. And also there are many boxes around that box in which some or the other things are kept. so now, if you needed to access that one coconut box then you will have to give a name to that box. now it’s time to understand that the name of that box “coconut” is a variable and the coconut stored in the box is data or information.

so, now you understood what are python variables ? In a variable, you can store different types of data like integer (int), float point numbers, decimals, letters and sentences. you need to use the variable to store the data so that Python can access it very easily with the help of the name.

Working with Python variables

To create variable in Python, we will have to go through some steps that are necessary to understand. so let’s get started…

here is the syntax below for declaring a variable.

<variable_name> = <value>

1). Python variable Declaration

the first step in “working with variables” is the declaration of variables. means if you want to use a variable then you have to first declare that variable. declaration of variable simply refers to “Assigning value to the variable”. for example – name = “Aman” . Here value “Aman” is stored in the “name” variable. you must use the equal sign to assign the value to the variable. you can understand the declaration of a variable by looking at the picture below.

name = "Aman"          // first variable declaration

day = "monday"         // second variable declaration

year = 2023                // third variable declaration

roll_no = 5345           // fourth variable declaration

marks = 83.5             // fifth variable declaration

Ex. – location = India, here “location” is the variable and “India” is the data that is stored in the location variable.

here you can also assign the same value to multiple variables, an example is given below.

first_student = "karan"

second_student = "karan"

in this case. you assigned a single value to a single variable but Python allows you to declare multiple variables at a time. for example of multiple variable declarations, you can see the picture given below.

Multiple Declaration of Variable

location, day = "india", "monday"

in the above example, location and day are two variables that have been declared along with their two values. here “India” is assigned to the “location” variable and “Monday” is assigned to the “day” variable.

here you can also assign the same value to multiple variables. an example is given below.

location = place = "india"

here in the above example, the value “India” is assigned to both variables “location” and “place”.

2) Assigning a new value

After declaring a variable you can also assign a new value to the variable at any point of time in you code. let’s see an example is given below.

location = 'india"

location = "jabalpur"

here in the above example, a value “India” is assigned to the “location” variable and then in next line we have changed the value of location from India to jabalpur.

3) variable Manipulation

after declaring a variable and assigning a value to it, you can perform various operations in variable like add, subtract, multiply, devision and many more. variables can be used to perform operations like mathematical operations, string manipulation and more based on the variable’s type.

here you will see an example of variable manipulation given below. that includes string manipulation and some mathematical operations like addition, substractino, multiplication, devision.

 num1 = 10       // value 1o assigned to variable num1
num2 = 20.       // value 20 assigned to variable num2

// added num1 and num2 and then assigned the result to the add variable
add = num1 + num2            

// substracted num1 and num2
sub = num1 - num2

// multiplied num1 and num2
mul = num1* num2

// devided num1 to num2
dev = num1/num2

name = "aman"

full_name = name+" kumar".               // aman kumar

Python Variable Name Rules

here are some rules for declaring Python variables, if your variable name follows these rules then that can be a variable name.

1). The name of a variable should start with only a letter or underscore.

name = "studypunchx"
_name = "studpunchx"   

2) you can use only alphanumeric characters and underscore in variable names.

Name = "studypunchx"
Name56 = "anuj"
first_Name = "prakash"
ad64lk = "demo"

3) you can’t use a number at the start of the variable name.

6name = "aman"            // not a variable

4) python variables are case-sensitive. it means “name”, “Name”, and “NAME” are totally different.

name = "aman"
Name = "prakash"

5) you can’t use Reserved words as a variable name. if you will do so then it will show an error message.

while = "ramesh"            // can't be variable
if = 10                       // can't be variable
for = 10.5                 // can't be variable

Good Name Variables :

  • studypunchx
  • Study_punchx
  • studyPunchx
  • _studypunchx
  • _study_punch_x
  • studypunchx13
  • Study13_punch

Bad Name Variables :

  • 12studypunch -> (number at first not allowed)
  • while -> (Reserved words not allowed)
  • study punchx -> (spaces not allowed)

you can use multi-word keywords to name the variable.

1). Camel case: camel case means, the first word of the variable name should be in lowercase and then after it all the remaining word’s first letter should be capitalized. In the camel case, space is not allowed between letters or words. For example firstName, youAndMe, camelCase etc.

firstName = "Ajay"

lastName = "kumar"

nameOfSchool = "sarswati school"

2) Pascal case: it is similar to the camel case but in Pascal case even the first letter of the first word should be capitalized. like this: FirstName, NameOfSchool, etc. Also in pascal case, space is not allowed between words.

FirstName = "Ajay"

LastName = "Kumar"

NameOfSchool = "sarswati school"

3) Snake Case : in snake case, each word is separated with an underscore. and also there is no any kind of restriction to use lower and uppercase at any place.

first_name = "Ajay"

Last_name = "kumar"

NaMe_of_SchooL = "sarswati school"

Python Type of Variable

Python type of variable refers to the type of value stored in the variable. it is used to know which type of value stored in the variable. here are various types of variable. let’s explore one by one.

1). Integer

This type of variable is used to store the integer numbers.

Example of Integer Variables :

num1 = 10

num2 = 30

a = 5

roll_no = 3438

Code :

num1 = 10               // first variable storing integer value "10"
num2 = 30               // second variable storing integer value "30"

sum = num1 + num2             // sum of both variables stored in "sum" variable

print("sum = ",sum)                 // using print() function, printing the value of sum

Output :

sum = 40

2) Float

This type of variable is used to store the decimals or floating point values. for ex – 2.45, 343.45, etc.

num1 = 5.34

num2 = 59.63

a = 1.5

Code :

num1 = 5.34          
num2 = 59.63

sum = num1 + num2

print("sum of num1 and num2 is = ",sum)


sum of num1 and num2 is = 64.97

3) Boolean

Boolean variables are used to check if the condition is true or not. Boolean variable stores only two values ‘True” and “False”. if the condition is true, it will return “True” otherwise “False”.

Example of Boolean Variables :

a = True

b = False

Code :

a = True
b = False

print("a = ",a)
print("b = ",b)

Output :

a = True
b = False

4) String

String variable is used to store letter, word or sentences. like name of a person or a paragraph. one more thing to remember that to declare a string you will have to place the value inside the double quotes.

Example of String Variable :

name = “study punchx”

location = “India”

hobby = “I love python”

Code :

name = "study punchx"
location = "india'
hobby = 'i love python"

print("Name = ",name)
print("Location = ",location)
print("Hobby = ",hobby)

Output :

Name = study punchx
Location = india
Hobby = i love python

5) List

This is the type of variable that is used to store more than one value. we can store multiple values in list variable. To declare list variable, write each value inside the square bracket, separated with commas.

Example of List variable :

names = [“ajay”, “ravi”, “meena”, “seeta”, “lakshami”]

numbers = [4,64,23,534,43,32]

Code :

names = ['Ajay", "Ravi", "Meena", "Seeta", "Lakshami"]

print("Name of students = ",names)

Output :

Name of students = ['Ajay', 'Ravi', 'Meena', 'Seeta', 'Lakshami']

6) Dictionary

The Dictionary variable is used to store values in key-value pairs. like – “name” : “study punch”. Dictionary variable is used to store numerous values in one variable.

Example of Dictionary variable :

info = {“name” : “ajay”, “roll_no” : 1345, “marks” : 83.5}

Marks_of_subject = {“Hindi” : 54, “English” : 64, “Mathematics” : 93, “Science” : 63, “Computer” : 85}

Code :

Marks_of_subject = {"Hindi" : 54, "English" : 64, "Mathematics" : 93, "Science" : 63, "Computer" : 85}

print("Marks of Subjects = ",Marks_of_subject)

Output :

Marks of Subject = {'Hindi' : 54, 'English' : 64, 'Mathematics' : 93, 'Science' : 63, 'Computer' : 85}

Hello Guys, Ayush this side. I am founder of studypunchx. I love to share trending and useful informations. keep Growing.....

Leave a Comment