Variables in Python

Variables in Python

3 mins read587 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Nov 22, 2022 19:43 IST

In this article, we will discuss variables in python, how to name them and how to work with the variables.

2022_05_feature-images_python-variables.jpg

Introduction

This article, we will discuss variables in Python.

In any programming language, you need to manage a lot of values and to store these values we need variables. 

In simple terms, variables are containers(or labels) for storing the data.

Now, let’s deep dive to get a better understanding of variables in Python and what are the rules for naming variables in Python.

Table of Content

What is a variable in Python?

Similar to the other programming languages, variables in python are reserved memory locations to store the values and every value in python has a specific data type(Data types in Python).

In simple terms, variables give data to the computer for processing. Variables in Python are grouped of both the letters and digits.

Example:

name = "Shiksha Online" # here name is a vraible
print(name)
2022_05_image-116.jpg

In the above example, “name” is a variable. It holds the string “Shiksha Online” and the print function shows the message “Shiksha Online” as an output.

Must Check: What is Python?

AUC-ROC in Machine Learning
AUC-ROC in Machine Learning
In machine learning, AUC-ROC is a key metric for assessing model accuracy, especially in binary tasks. It gauges a model’s ability to differentiate between classes. Explore to understand its importance.
Understanding Tuples in Python
Understanding Tuples in Python
In this guide, you will learn all about Tuples in Python: creating tuples, accessing tuple items, looping through a tuple, and other tuple operations with the help of examples.
Understanding Python Sets (With Examples)
Understanding Python Sets (With Examples)
In this article, you will discuss about Python Sets: creating sets, accessing, adding, and removing set items, looping through a set, and other set operations with the help of examples.

Rules for Naming Variables

  • Syntax to declare variables in python
    • variable_name = value

Example

#Syntax to declare variables in python
# = is an assignment operator
 
variable_name = variable
  • In value, it can be any data type(number, string, Boolean, complex etc.)
  • A variable name can store number(0 – 9), underscore(_) or letters(A – Z, a – z)
    • But it will only start with a letter or underscore
    • A variable name can be of any length
    • It can consist of Uppercase or lowercase letter
    • The Variable’s name can’t have spaces
      • Use underscore, to separate the words
    • Variable name can’t be any python keyword or built-in functions

Example:

#Rules for Naming Variables
 
#variable name in lowercase
name = "Shiksha Online" # value : string data type
 
#variable name in UPPERCASE
AGE = 3.5  # value : float data type
 
#variable name can be of any length and words are seprated by underscore(_) 
number_of_Employers = 3000 # value : integer data type
 
#variable name start with underscore(_)
_is_true = True # value : boolean data type
 
#display
print(name, AGE, number_of_Employers,_is_true)

Output

2022_05_image-117.jpg

Must Check: Python Online Course & Certifications

Python Dictionary Practice Programs For Beginners
Python Dictionary Practice Programs For Beginners
Working with Python dictionary types can seem slightly difficult if you are not familiar with its features. The best way to master it is by solving problem statements. It is...read more
Functions in Python
Functions in Python
By Varun Dwarki
Tutorial – for Loop in Python
Tutorial – for Loop in Python
Let’s read about for Loop in Python in the below tutorial.

Best way to name variable

  • Should be clear, concise and written in English
  • No Special Character and Python Keywords
  • Avoid spacing in between the words

Working with Variables

Re-declaring Variable

We can re-declare the variable after declaring them.

Example

#Declare the variable
name = "Naukri"
print(name)
 
#Re-declare the variable
name = "Shiksha Online"
print(name)

Output

2022_05_python-variable_redeclaring-the-variable.jpg

In the above example, first, we assign a variable name to “Naukri” and then assign the same variable name to “Shiksha Online”.

Assigning a single value to a different variables

Python allows assigning the same value to different variable names at a time.

Example

#Assigning a single value to multiple variable
 
name = NAME = _NAme = "Shiksha Online"
print(name, NAME, _NAme)

Output

2022_05_python-variable_Assigning-a-single-value-to-multiple-variable.jpg

Assigning different values to different variables

Python allows assigning different values to different variables in a single line.

Example

#Assigning different values to different variables
 
name, AGE, number_of_Employers, _is_true = "Shiksha Online", 3.5, 3000, True
print(name, AGE, number_of_Employers, _is_true)

Output

2022_05_python-variable_Assigning-different-values-to-different-variables.jpg

Concatenate Variable

In python, we can concatenate two variables.

  • In python, we can’t concatenate variables of two different type

Example

#cancatenating variables(of same data types)
 
#string data type
name1 = "Naukri"
name2 = "Learning"
 
print(name1 + name2)
 
 
#number data type
 
age1 = 1
age2 = 2
print(age1 + age2)

Output

2022_05_cancatenating-variablesof-same-data-types.jpg

Example

#cancatenating variables(of different data types)
 
#string - number data types
 
name = "Shiksha Online"
age = 3
 
print(name + age)

Output

2022_05_concatenating-variablesof-different-data-types.jpg

Deleting Variables

We can delete the variables using del command.

Example

#deleting variables
 
name = "Shiksha Online"
del name
name

Output

2022_05_deleting-variables.jpg
String Formatting in Python
String Formatting in Python
In this article we discussed different techniques of string formatting in python like modulo operator, built-in format and f-string.
Array Manipulation Using NumPy
Array Manipulation Using NumPy
NumPy is a foundation library for scientific computations in Python, literally standing for Numerical Python. It contains sophisticated functions and tools for integrating with other programming languages as well. During...read more
Introduction To Decorators In Python
Introduction To Decorators In Python
In this article, we will discuss in detail about what are python decorator, why we need them and finally how to create python decorators.

Conclusion

In this article, we have discussed variables in python, how to name them and how to work with the variables.

Hope this article will help you in learning python and in your data science journey.

Top Trending Articles:

Data Analyst Interview Questions | Data Science Interview Questions | Machine Learning Applications | Big Data vs Machine Learning | Data Scientist vs Data Analyst | How to Become a Data Analyst | Data Science vs. Big Data vs. Data Analytics | What is Data Science | What is a Data Scientist | What is Data Analyst

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio