Variables in Python
In this article, we will discuss variables in python, how to name them and how to work with the variables.
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?
- Rules for Naming Variables
- Best way to name variable
- Working with Variables
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)
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?
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
Must Check: Python Online Course & Certifications
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
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
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
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
Example
#cancatenating variables(of different data types) #string - number data types name = "Shiksha Online" age = 3 print(name + age)
Output
Deleting Variables
We can delete the variables using del command.
Example
#deleting variables name = "Shiksha Online" del name name
Output
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
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
Comments
(2)
M
3 months ago
Report Abuse
Reply to MOHAMMAD RAJAMMAD
M
3 months ago
Report Abuse
Reply to MOHAMMAD RAJAMMAD