In this post, you will learn what are variables and how to take input from the user in Python with very basic explanation and examples, So let’s start learning both concepts one by one.
Variables in python
A Variable is a container that holds or stores the value of particular datatypes. OR you can say the Name assigned to your datatypes is known as a variable in python.
Variables are used to store data types such as Numeric, Sequence types, Dictionary, Set, Boolean, etc.
As like other programming languages, there is no need to specify data type while assigning variables in python. you just write the name of your variable followed by the equal sign and your data.
Example:
num = 25
print(num)
Output:
25
Variables vary, which means that you can change the value of your variables.
my_data = 34
print(number)
my_data = "allinpython"
print(number)
my_data = 34.7
print(number)
Output:
34
allinpython
34.7
There are some rules to declare variables in python (this rule is compulsory to follow otherwise you get an error).
Rules to declare variables in python
- only letters, numbers, and underscores are allowed to declare variables.
- variables name must start with letters or underscores only.
- variables name can not start with numbers.
- Variables names are case-sensitive means if you can declare a variable with the name ans and declare another variable with the name Ans they both are different.
always remember these 4 rules for declaring variables otherwise you will get an error while declaring variables.
ans = "allinpython"
print(ans)
Output:
allinpython
Other example:
$name = "allinpython"
print($name)
Output:
allinpython\variable.py", line 10
$name = "allinpython"
^
SyntaxError: invalid syntax
According to Rule no-1, only letters or numbers, or underscore are allowed but in the above example we declare variable name as $name
and in this case, we have not followed rules (because any special character is not allowed except underscore) that’s why we get an error.
Similarly if you are not follow any rule you will get error as show in above example.
Type of variables
There are two types of variables in python and they are:
- local variable
- global variable
Local variable
A variable that assigns inside any function is known as a local variable.
Example:
def demo():
a = 34
b = 2
print(a+b)
demo()
Output:
36
here variables a
& b
are local variables we can not use those variables outside of the function otherwise it gives an error.
for your better understanding, let’s re-write the above program but this time we try to print variables a
& b
outside the function and see the output.
def demo():
a = 34
b = 2
print(a+b)
demo()
print(a)
print(b)
Output:
36
Traceback (most recent call last):
File "c:\Users\saurabh\Desktop\saurabh\phython tutorial\lambda_expresion.py", line 25, in <module>
print(a)
NameError: name 'a' is not defined
In the above program, we try to print local variables a
& b
outside the function and it gives an error that means we can not use a local variable outside a function (or you can say that local variables are not visible outside of the function).
Global variable
Variables that are assigned outside a function is known as a global variable.
To use the global variable inside a function we use the global
keyword.
a = 2
def demo():
global a
a = 7
b = 5
print(a+b)
demo()
print(a)
Output:
12
7
In the above program variable a
is assigned outside a function, to use this variable inside a function we use the global
keyword as shown in the above program but when we changed the value of variable a
to 7 its value also changes outside a function means the variable inside function and outside a function are same.
Assign Multiple variables in one line
num1,num2 = 5,3
print(num1)
print(num2)
Output:
5
3
Assign a Single value to many variables
a=b=c= 5
print(a)
print(b)
print(c)
Output:
5
5
5
Now we understood variables in python, it’s time to learn how to take user input in python.
User input in python
input()
function is used to take user input in python.
name = input("enter your name :")
print("Your name is",name)
Output:
enter your name :ibrahim
Your name is ibrahim
NOTE: input()
function always takes input in the string (even if we enter any number as input, it is considered a string ) let’s understand it with the help of an example.
num = input("enter : ")
print(type(num))
Output:
enter : 34
<class 'str'>
here in the above example, we use the type()
function to check the datatype of the num
variable.
To take input as a number you need to typecast the input()
function into an int()
.
num = int(input("enter your number: "))
print(type(num))
Output:
enter your number: 45
<class 'int'>
Similarly, you can also typecast input() function to any other datatypes as per your requirement.
Take multiple input in single line
We use .split()
method to take multiple input in single line.
For Example:
n1,n2 = input("Enter both the number separeted by comma (,): ").split(",")
print(int(n1))
print(int(n2))
Output:
Enter both the number separeted by comma (,): 3,5
3
5
In the .split()
method, you have the option to pass a separator. In the above example, we used a comma (,
) as the separator, but you can choose any character as the separator. If no separator is provided, by default, whitespace is considered as the separator.
That’s all for variables and how to take user input in Python. I hope this post has provided value and will be beneficial to you. Thank you!
1 Comment
Thank you for this post I have to learn python and this blogs are also help me
Keep posting the next blogs