“Stop wishing, start doing”
Now, you must be commendable in the basic data types of python.
Moving further, we will be briefing about variables in detail.
Variables are containers for storing data.
In python we do not have a command to declare variables, they are created at the time a value is assigned to them.
We no need to explicitly specify a variable data type. It is automatically assigned when creating them according to the value and we can also change the value and its data type whenever needed.
Rules for naming a variable
- Variable name should start with a letter or underscore(_) character.
- A variable name cannot start with a number but can contain numbers in between.
- No special characters are allowed inside a variable name except underscore(_).
- Space is not allowed in between the variable name.
- Variable names are case-sensitive.
- It is always advisable to create a meaningful variable name.
Multi-word variable name
While writing a variable name, there can be a need to write a variable name with many words, but since space is not allowed in between variable names
It can become tough to read and understand the meaning of the variable name, so we can follow any one of the following patterns while selecting a variable name.
Each word except the first starts with a capital letter.
Each word starts with a capital letter.
Each word is separated by an underscore character.
I always suggest you can start using any type of case, which is appealing to you. However, variables are not the only ones where we use names, so the python style guide also known as PEP8 gives us the following suggestions,
- Snake Case should be in use for functions and variable names.
- Pascal Case should be in use for class names.
So, moving further we can follow the same as requested by PEP8 guidelines.
Equal to(=) is called an assignment operator in python.
Here the value 10 is assigned to the variable my_favorite_number.
So, moving further we can use my_favorite_number anywhere you need.
We can also change its value whenever needed and the changed value is used for future use.
Python also allows us to assign multiple values to multiple variables at the same time.
Which will assign fruit1 to apple, fruit2 to orange, and fruit3 to grape respectively.
Make sure, the number of variables should be equal to the number of values provided else you will get an error.
You can also assign the same value to multiple variables on a single line.
Here, all the three variables x,y, and z are assigned to the same value 10.
If you need to print a variable’s value,
If you need to print more than one variable in a single print statement,
print(variable1 + variable2)
Here, both the variables are concatenated and the output is shown, the only restriction is + operator works best only if you need to concatenate two strings, If you do the same with two integers it will throw an error.
So, the best solution to join many variables of different data types is by using a comma(,) in between them.
Example: Let us create two variables for name, age, and hobby and try to print them as a sentence.
name="Pythonista" age=20 hobby="coding" print("My name is",name,"of age",age,".\nMy favorite hobby is ",hobby,".")
My name is Pythonista of age 20. My favorite hobby is coding.
- Create a variable to store your favorite color and print the same.
- Assign your name and age to two different variables and print it in the following pattern.
My name is <name> My age is <age>
- Find the correct variable names from the below list,
My_Favorite_color=”red” 1stNumber=10 Number2=20 My lucky number=30 my-pet-name=”xx”
- Find the output for the following code,
x=30 print(x) x=40 X=50 print(x) x=70 print(X)
- Write a program to swap two numbers.
Hoping you have got a clear idea regarding variables in python. Don’t forget to work on the Practice yourself. If any doubts you can feel free to ask in the comments section.