Variables in PythonFebruary 16, 2021 2021-10-04 7:41
Variables in Python
Variables in Python
Variables are the building blocks of a Python program and we will be using them a lot. Therefore, let us discuss them in-depth in this lesson.
What is a Python Variable?
A variable in Python is a symbolic name that points to the location where data is stored in memory. Variables are created or declared whenever a value is assigned to a character or sequence of characters in Python.
In the example below, the value 10 is being pointed by the variable called
# Declaring the variable called val_num with the value 10 val_num = 10 # Printing the value pointed by the variable called val_num print(val_num)
Since the variable val_num is just a container pointing to where the data is stored in memory, the variable can be assigned to another variable as well. This passes the reference of the value to the assigned variable as well.
# Assigning the variable val_num with the value 10 val_num = 10 # Assigning the variable sec_val_num with the value 10 sec_val_num = val_num # Printing the value pointed by the variable sec_val_num print(sec_val_num)
We can also change which value a variable points to as show in the example below:
# Assigning the variable val_num with the value 10 val_num = 10 # Assigning the variable val_num with the value 20 val_num = 20 # Printing the value pointed by the variable val_num print(val_num)
Best practices and rules for naming a variable in Python
In order to properly name a variable in Python, we must follow a set of rules like in any other programming language. The rules and best practices are as follows:
- Variables names should only consist of a combination of lowercase letters (a-z) or uppercase letters (A-Z) or digits (0-9) or an underscore (_).
- A meaningful word should be used as a variable name. For example,
ageis a meaningful variable name that explains that the variable holds a reference to an age value.
- Variable names should not start with a digit.
- Variable names consisting of two or more words should have an underscore (_) in between them. For example,
date_of_birthis a variable name that holds a reference to a date of birth value.
- Variable names should not use special symbols other than an underscore (_).
Keywords in Python
Python keywords are case-sensitive words in Python that have a Python-specific meaning. Such keywords cannot be used as variable names and are reserved by the Python programming language.
Since keywords define the syntax and structure of Python, they are large in number and remembering all of them is very hard when starting out. The correct way to memorize such keywords is by actually using them in Python and we will be doing so throughout the course. But, for now, here are some example Python keywords along with their meaning:
|def||Used for creating a function in Python|
|global||Used for declaring a global variable in Python|
|class||Used for creating a class in Python|
|import||Used for importing libraries in Python|
|True||Used for denoting a boolean|
Again, you do not have to worry what the above keywords mean right now. We will cover them throughout this course.
What is a Constant in Python?
A constant in Python is a variable whose value is not meant to be changed. The general practice for declaring a constant is by using uppercase letters for the variable name.
In the example below, PI is a constant whose value is not meant to be changed.
# Declaring a constant called PI with the value 3.14 PI = 3.14 # Printing the value pointed by the constant called PI print(PI)
Now you know the most basic concepts related to variables in Python. We will build on these concepts starting from the next lesson of this course.