User Input & Type Casting
User Input​
Python allows for user input. That means we can ask the user for input. input()
To take input from the user.
Example :
name = input("Enter the Student Name :")
print(name)
Output :
Enter the Student Name : Yogita
Yogita
Example Explanation :
In the above example, we are taking input from the user with the help of the input
function and assign to the variable name
and printing name
using the print()
function. That name
value is Yogita.
Showing Output in Formatting way :
name = input("Enter Your Name :")
print("My name is {}".format(name))
Output :
Enter Your Name : Yogita
My name is Yogita.
Example Explanation :
In the above example, we are taking input from the user with the help of the input
function and assign to the variable name
and print a message My name is
{}
, this {}
means format()
function. We assign the name
variable in the format()
function. So output is My name is Yogita.
Type Casting​
Type Casting is the method to convert the variable data type into a certain data type for the operation required to be performed by users.
Example :
val1 = int(input("Enter the Value 1:"))
val2 = int(input("Enter the Value 2:"))
sum = val1 + val2
print(sum)
Output :
Enter the Value 1 : 100
Enter the Value 2 : 10
110
Example Explanation :
In the above example,
In 1st line, we are taking input from the user with the help of the input
function and type casting this input into the int
data type and assign to the variable val1
.
In the 2nd line, we typecast the input into the int
data type and assign it to the variable val2
.
In the 3rd line, we are adding both the variables val1
and val2
and assign them to the variable sum
.
In the 4th line, we print the variable sum
.
Another Example of another way to Type Casting :
val1 = int(input("Enter the Value 1:"))
val2 = int(input("Enter the Value 2:"))
sum = int(val1) + int(val2)
print(sum)
Output :
Enter the Value 1 : 10
Enter the Value 2 : 10
110
Example Explanation :
In the above example, we are taking two inputs from the user with the help of the input
function and assigning them to the variable val1
and val2
respectively. After that, we are adding both the variables val1
and val2
by typecasting them into the int
data type and assigning it to the variable sum
.
Now we print the variable sum
.
Show output in Formatting Way :
val1 = input("Enter value 1 :")
val2 = input("Enter value 2 :")
sum = int(val1) + int(val2)
print("Sum of { } and { } is { }".format(val1,val2,sum))
Output :
Enter value 1 : 25
Enter value 2 :15
Sum of 25 and 15 is 40 .
Example Explanation :
In the above example, we are taking two inputs from the user with the help of the input
function and assigning them to the variable val1
and val2
respectively. After that, we are adding both the variables val1
and val2
by typecasting them into the int
data type and assigning it to the variable sum
. Now we print Sum of {}
and {}
is {}
. That {}
means format()
function.
Swap two numbers
Example :
a = 10
b = 20
print("Before swapping value of a",a)
print("Before swapping value of b",b)
temp = b
b = a
a = temp
print("After swapping value of a",a)
print("After swapping value of b",b)
Output :
Before swapping value of a 10
Before swapping value of b 20
After swapping value of a 20
After swapping value of b 10
Example Explanation :
In the above example, We have to swap two numbers. we have declared two variables a
and b
.The value of the a
variable is 10
and the value of the b
variable is 20
.Now we have to swap these two numbers using the third variable. The third variable we declared as temp
.Now we stored the second variable b
in the temp
variable. Stored the value of variable a
in variable b
.now stored the temp
variable in variable a
.