Storage is the basic concept when we deal with any devices, technologies or any programming language. We always looking for storage. In Programing language, you want to store a lot of values like Name, Age, Marks, Address, Contact etc., for this you should know about the concept of “Variables”. So, after read this new term your mind ask you “What is Variables?”. Let’s clarify your all doubts related to Variable in this article. Let’s start…..
Variables :
- Variable represents named location that refers to a value and whose values can be used and processed during program run.
- A variable, as the name suggests, has been derived from the word “vary” which states that a variable may vary during the execution of a program, i.e. the value of a variable keep changing during the program execution.
- Example : if you want to store name and age of a person during a program run, then you some variables to store these values.
- A variable is reference type not value type.
Creating Variables
- Variable is also an identifier, so before moving ahead please revise all rules of identifier in chapter (Character sets).
- Variables are created by assigning value of desired type to them. Simply, you need to create a variable with any name and assign value to it.
- Example :
-
- name=’Mahesh’ => name is variable which stores a string value Mahesh
- age=33 => age is a variable which stores a integer value 33
-
Components of Variables:
A variable has three main components :
Components of a Variable in Python
Value :
Value of a variable is basically a literal. You can easily see the value of a variable by printing it by using print() function.
Type :
As per the literal assigned to a variable, variable can associate with same kind of data type, i.e if you will assign a String literal then type of a variable is string and if you will assign integer value then the type of a variable is integer.
You can check type of variable with the help of type() function.
Memory Address/ID :
- Each variable has some id (memory address) or reference of id which does not change once it has been created.
- You can check address of variable with the help of id() function.
- Example of Value, Type and Address of a variable :
Component of Variables Example
Lvaules and Rvalues
When you create a variable, you’ll notice that the name of the variable is at the left of the assignment operator (=) and the value associated with that variable is at its right. So, basically this expression can be divided into two parts, i.e., Lvalues and Rvalues.
lvalue :
- expression that can come on the lhs (left hand side) of an assignment.
- lvalues are the objects to which you can assign a value.
- Basically it’s a variable name.
- An lvalue refers to an object that persists beyond a single expression.
rvalue :
- expression that can come on the rhs (right hand side) of an assignment.
- rvalues are the literals and expressions that are assigned to lvalues.
- An rvalue is a temporary value that does not persist beyond the expression that uses it.
- Example :
- name=”Mahesh” => Mahesh is a rvalue and name is a lvalue.
- Now, you can change the value of name that’s why we can say that rvalue is temporary.
Assigning Values to Variables :
In python, you can assign values to a variable in different ways :
Assigning single value to single variable :
- In this, you can assign a single value to a variable. The type of the variable depends on the value you will assign.
- Examples :
- a=5
- b=”Mahesh”
Assigning same value to multiple variables :
- In this, you can assign same value to multiple variables in a single statement.
- Example :
- a=b=c=7
- it will assign the values 7 to all the variables.
Assigning multiple values to Multiple variables :
- In this, you can assign multiple values to multiple variables in a single statement.
- Examples: x,y,z=10,20,30
- name,age,subject=’Mahesh’,33,’Python’
- It will assign the value order wise i.e first variable is given the first value, second variable the second value and so on.
Dynamic Typing
- A variable pointing to a value of a certain type, can be made to point to a value/object of different type. This is called dynamic typing.
- Example:
Dynamic Typing in Python
In above example you can see that single variable can not be fixed with any single data type. This feature is called Dynamic Typing.
I hope you learnt the concept of variables from the above information. Now, the next question comes in our mind that how can we accept values from user? From the above information you are able to store value at the time of variable declaration. But what if you want to accept values from user. For this you should learn about Input and Output Statements.
Input and Output
Let’s understand what is input and output. A program is good if you initialize some value to the variable and perform calculation on these value. For example you will take two variables A and B with values 5 and 10 and perform some on these variables. But when you run it, it will give the same output.
To become above program more interactive we need some input from the users.
Python provides built in function input() to accept value form user interactively.
input() :
- input() function is used to get data from the user.
- It enables us to accept an input string from the user without evaluating its value.
- It continues to read input text from the user until it encounters a new line.
- It always returns a value of String type.
- Syntax :
-
-
- variable name=input(<User’s display message>)
-
-
- Example:
-
-
- name=input(“Enter you name : “)
-
-
Reading Numbers Using input() :
- As you know input() function returns only string and string cannot be used for any arithmetic or other numeric operation. For these operation you need to convert this string into numeric values.
- Python offers two function int() and float() to be used with input() to convert the values received by input() function.
- Examples:
User Input in Python
Whenever you enter your name and print it, then Python easily take your name as a string and print it. Although it takes the value(23) as a string but not as a integer. Then in this case you will be unable to do any arithmetic operation (like ans=n+2)
on that number and it will generate the above error. To solve this problem, you can use int() or float() function with input(). Like :
Print function in Python
Output through print() :
- print() function is used to display the output.
- Syntax:
-
- print(*objects, [sep=’ ’ end=’\n’])
-
- Examples:
Print function example in Python
Output of the above program :
Print method in Python