Understanding Variables in Python
Variables are one of the very basic concepts when one is getting started with programming. You can basically think of a variable as a container that holds data that you can use and manipulate within your program. Variables in Python are very flexible to work with and really easy, therefore, making them is very good for any beginner.
Basically, a variable in Python is nothing but a name given to a value. This can eventually be utilized within the code snippet to reference that value. Variables help in storing and manipulating data; therefore, they enable your code to perform different types of operations.
For example, if you would like to store the number of toys you have, you may want to create a variable called toys and you might give it a value:
toys = 6
Here, toys is the variable name, and 6 is the value stored in that variable.
Declaring a Variable in Python is very simple: a variable in Python is declared by writing the name
of this variable, using the assignment operator – ‘=’, and then assigning a value.
* variable_name = value Here’s an example:
Example:
# A variable to store a name
name = “Bhuvan Bam”
# A variable to store integer number
age = 30
# A variable to store decimal number
height = 5.8
# A variable to save the current status of the person
is_student = False
In the example above:
‘name’ is a variable that holds the string “Bhuvan Bam”.
‘age’ is a variable that holds the integer 30.
‘height’ is a variable that holds the float 5.8
Each variable name is unique and refers to the data you’ve assigned to it.
You can easily change the value of a variable. It totally depends on you what you want to store in
a variable. Just how you decide which toy to put in which box
company_name = “Meritshot”
print(company_name)
# Assigning a new value to company name
company_name = “Meritshot Zetta”
print(company_name)
Meritshot
Meritshot Zetta
Now if you want to assign values to multiple variables, assigning then again and again seems a tiring job, so we have a shortcut here.
# we declare the variable again and again, note we are
a = 5
b = 6
c = 10
d = 15
# we are assigning all the variables together
a,b,c,d = 5,6,10,15
The task of assigning multiple values becomes so easier now right?
We can also assign one value to multiple variables at once instead of repeatedly assigning them
again and again
a = b = c = “Dyuti”
print(a)
print(b)
print(c)
Dyuti
Dyuti
Dyuti
6.0.1 Variables do not need to be declared with any particular type and can even
change the type after they have been set.
now let’s understand this statement. Suppose you declare a variable ‘a’ and then assign ‘6’ to the variable. Later on you decide to change the value assigned to ‘a’. You can do that easily by reassigning the value to ‘a’. Look at the example below.
a = b = c = “Dyuti”
print(a)
print(b)
print(c)
6
Shweta
a = Shweta
print(a)
————————————————————————— NameError Traceback (most recent call last) Input In [9], in () —-> 1 a = Shweta 2 print(a) NameError: name ‘Shweta’ is not defined
We encounter NameError because Shweta is string and strings should be enclosed in single or double
quotes
a = ‘Shweta’
print(a)
a = “Shweta”
print(a)
Shweta
Shweta
Enclosing strings in Single quote ” or double quote ”” makes no difference. They give you same result
6.0.2 NOTE:
7 Variable Naming Conventions.
Naming conventions are a set of guidelines for choosing names for variables, functions, classes, and other identifiers in your code. Consistent naming is crucial which makes your code more readable, maintainable, and easier to understand for both you and others. Now imagine if you name a variable as “fruits” but you wanted to store a chocolate’s name, For you it is easy to understand that whenever you are referring to “fruits”, it is actually chocolate you’re refering to, but some other person can’t understand that in your code “fruits” refer to chocolate. This will create confusion, That is why all over the world people decided on some rules to abide by while using variables. There are some common styles that you can use while naming the variables. ## Common Styles:
CamelCase: Lowercase first word, then capitalize the first letter of each subsequent word (e.g., firstName, totalAmount).
myVariableName = “Dyuti” print(myVariableName)
Dyuti
PascalCase: Capitalize the first letter of each word (e.g., FirstName, TotalAmount).
MyVariableName = “Dyuti” print(MyVariableName)
Dyuti
Snake Case: All lowercase, words separated by underscores (e.g., first_name, total_amount).
my_variable_name = “Dyuti” print(my_variable_name)
Dyuti
(e.g., first-name, total-price)
Input In [14]
my-variable-name = “Dyuti”
^
SyntaxError: cannot assign to operator
We get syntax error because Python does not allow kebab case
8 Rules for variables:
1man = “superman” print(1man)
Input In [15]
1man = “superman”
^
SyntaxError: invalid syntax
We got SyntaxError because variables cannot start with 1
age = 19
Age = 20
print(age)
print(Age)
19
20
As you can see “age” and “Age” are different
for = “Cat”
print(for)
Input In [17] for = “Cat”
^
SyntaxError: invalid syntax
We get an error because “for” is a reserved keyword in python
wow! = “Interjection
print(wow!)
Input In [18]
wow! = “Interjection”
^
SyntaxError: invalid syntax
9 Best Practices:
10 How to delete variable:
Now since we are declaring and assigning variable, we should also be able to delete the variables
fruit = “pomegranate” print(fruit)
del fruit
# The variable gets deleted. Now if you try to print fruit again you will get␣
an error because it does not exist.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.