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.

Example:

num = 25
print(num)

Output:

25

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)

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

1. only letters, numbers, and underscores are allowed to declare variables.
4. 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:

1. local variable
2. global variable

## 1. 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 re-write the above program but this time we try to print variables a & b outside the function and let’s 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 as a string ) let’s understand with the help of an example.

name = input("enter : ")
print(type(name))

Output:

enter : 34
<class 'str'>

here in the above example, we use the type() function to check the datatype of the name 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.

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.

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.