In this post, you will learn about variables and how to take user input in python with very basic explanations and examples, 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 int, float, boolean, string, long, double, 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.
num = 25 print(num)
Variables vary, which means that you can change the value of your variables.
number = 34 print(number) number = "allinpython" print(number) number = 34.7 print(number)
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)
$name = "allinpython" print($name)
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
1. Local variable
A variable that assigns inside any function is known as a local variable.
def demo(): a = 34 b = 2 print(a+b) demo()
b are local variables we can not use those variables outside of the function otherwise it gives an error.
for your better understanding re-write the above program but this time we try to print variables
b outside the function and let’s see the output.
def demo(): a = 34 b = 2 print(a+b) demo() print(a) print(b)
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
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).
Variables that are assigned outside a function is known as a global variable.
To use the global variable inside a function we use the
a = 2 def demo(): global a a = 7 b = 5 print(a+b) demo() print(a)
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)
Assign a Single value to many variables
a=b=c= 5 print(a) print(b) print(c)
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)
enter your name :ibrahim Your name is ibrahim
input() function always takes input in the string (even if we enter any number as input it is considered as a string ) let’s understand with the help of an example.
name = input("enter : ") print(type(name))
enter : 34 <class 'str'>
here in the above example, we use the
type() function to check the datatype of the
To take input as a number you need to typecast the
input() function into an
num = int(input("enter your number: ")) print(type(num))
enter your number: 45 <class 'int'>
Similarly, you can also typecast input() function to any other datatypes as per your requirement.
that’s all for variables and how to take user input in python.
hope so this post adds some value to your life – thank you.
Thank you for this post I have to learn python and this blogs are also help me
Keep posting the next blogs
Pingback: How to Reverse a Number in Python – allinpython.com
Pingback: Python Program to Calculate Area of Rectangle – allinpython.com