Variables in Python

Variables in Python

3 mins read587 Views 2 Comments
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.

Recommended online courses

Best-suited Python for data science courses for you

Learn Python for data science with these high-rated online courses

Free
4 weeks
12 K
8 hours
4.24 K
6 weeks
40 K
100 hours
4.99 K
– / –
– / –
– / –
– / –
60 hours
– / –
90 hours
1.27 L
12 hours

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
Understanding Tuples in Python
Understanding Python Sets (With 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
Functions in Python
Tutorial – for Loop in Python

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
Array Manipulation Using NumPy
Introduction To Decorators In Python

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
qna

Comments

(2)