write a python program to read two numbers

Python provides input function calls to read the content from the user interactively. The input function call returns the content that it took from the user into string format. The programmer needs to convert the string to the appropriate type and start using it.

Let's use the input function to read two numbers from the user.

 

 

write a python program to read two numbers

 




Write a Python Program to Read Two Numbers

Let's take two variables first and second, in the first statement use the first variable and input() function call to ask the user to enter the first number. Convert the value of input() function call to integer type.

first = int(input("Enter First Number : "))




In the second statement of the program use again the input() function call, ask the user to enter the second number and convert that to again integer type using int() type conversion.

second = int(input("Enter Second Number : "))




Once we get two variables use the print function to print the two integer numbers.

print(f"The two number are {first} and {second}")




Final Program
================

first = int(input("Enter First Number : "))
second = int(input("Enter Second Number : "))

print(f"The two number are {first} and {second}")




Output
===========

Enter First Number : 23
Enter Second Number : 45
The two number are 23 and 45




Write a Python Program to Read Two Floating Point Numbers

In the above program we use integer type conversion, to convert the input from the user to integer value. Now let's use floating point type conversion to get the two floating point numbers. Use the float() function call to convert the number to float.

Program
==========

first = float(input("Enter First Number : "))
second = float(input("Enter Second Number : "))

print(f"The two number are {first} and {second}")





OUTPUT
===========

Enter First Number : 10.067
Enter Second Number : 93.456
The two number are 10.067 and 93.456




Now let's use complex type conversion to get the complex number and print it. Use the complex() function to convert the input values to complex numbers

Program
===========

first = complex(input("Enter First Number : "))
second = complex(input("Enter Second Number : "))

print(f"The two number are {first} and {second}")




OUTPUT
=============

Enter First Number : 2+1j
Enter Second Number : 3-5j
The two number are (2+1j) and (3-5j)





Write a Python Program to Input Two Integers in a Single Line

Use the input function call to ask the user to enter two numbers separated by space. Split the string by space and use map function to convert it into integers. Store the output of map into two variables first and second.

first, second = map(int, input("Enter Two Numbers (Separated by Space): ").split())

print(f"The two number are {first} and {second}")




OUTPUT
============

Enter x and y values (separated by space): 12 23
The two number are 12 and 23



Conclusion
============

Try running the program by yourself and comment down below if you have any queries or suggestions to improve the above programs.



Post a Comment

0 Comments