How to read two numbers in python

Last update on August 19 2022 21:50:48 [UTC/GMT +8 hours]

Python Basic: Exercise-134 with Solution

Write a Python program to input two integers in a single line.

Sample Solution-1:

Python Code:

print["Input the value of x & y"]
x, y = map[int, input[].split[]]
print["The value of x & y are: ",x,y]

Sample Output:

Input the value of x & y
 2 4
The value of x & y are:  2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Sample Solution-2:

Python Code:

a, b = [int[a] for a in input["Input the value of a & b: "].split[]]
print["The value of a & b are:",a,b]

Sample Output:

Input the value of a & b:  2 4
The value of a & b are: 2 4

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code [and comments] through Disqus.

Previous: Write a Python program to calculate the time runs [difference between start and current time]of a program.
Next: Write a Python program to print a variable without spaces between values.

Python: Tips of the Day

Convert a dict to XML:

from xml.etree.ElementTree import Elementdef dict_to_xml[tag, d]:
    '''
    Turn a simple dict of key/value pairs into XML
    '''
    elem = Element[tag]
    for key, val in d.items[]:
        child = Element[key]
        child.text = str[val]
        elem.append[child]
    return elem

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.

Below is a demonstration of the same −

Example

 Live Demo

first_num = int[input["Enter the first number..."]]
second_num = int[input["Enter the second number..."]]
print["The first number is "]
print[first_num]
print["The second number is "]
print[second_num]
quotient_val = first_num//second_num
remainder_val = first_num%second_num

print["The quotient is :"]
print[quotient_val]
print["The remainder is :"]
print[remainder_val]

Output

Enter the first number...44
Enter the second number...56
The first number is
44
The second number is
56
The quotient is :
0
The remainder is :
44

Explanation

  • The first and second numbers are taken as input from the user.

  • They are displayed on the console.

  • To find the quotient, the ‘//’ operator is used.

  • To find the remainder, the ‘%’ operator is used.

  • The operations output is assigned to two variables respectively.

  • This is displayed as output on the console.

Updated on 16-Apr-2021 12:05:07

  • Related Questions & Answers
  • Golang Program to Read Two Numbers and Print their Quotient and Remainder
  • Java program to compute Remainder and Quotient
  • C++ Program to Find Quotient and Remainder
  • C Program to Compute Quotient and Remainder?
  • Java Program to Compute Quotient and Remainder
  • Program to find Quotient and Remainder in Java
  • C++ Program for quotient and remainder of big number
  • Return element-wise quotient and remainder simultaneously in Python Numpy
  • Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
  • C# program to accept two integers and return the remainder
  • Golang Program to Read a Number [n] and Print the Natural Numbers Summation Pattern
  • Python program to print all Happy numbers between 1 and 100
  • Java program to print the Armstrong numbers between two numbers
  • C program to print array of pointers to strings and their address
  • Divide a given scalar element with masked array elements and return arrays with Quotient and Remainder in NumPy

I am new at this stuff as well. Did a bit of research from the python.org website and a bit of hacking to get this to work. The raw_input function is back again, changed from input. This is what I came up with:

i,j = raw_input["Enter two values:  "].split[]
i = int[i]
j = int[j]

Granted, the code is not as elegant as the one-liners using C's scanf or C++'s cin. The Python code looks closer to Java [which employs an entirely different mechanism from C, C++ or Python] such that each variable needs to be dealt with separately.

In Python, the raw_input function gets characters off the console and concatenates them into a single str as its output. When just one variable is found on the left-hand-side of the assignment operator, the split function breaks this str into a list of str values .

In our case, one where we expect two variables, we can get values into them using a comma-separated list for their identifiers. str values then get assigned into the variables listed. If we want to do arithmetic with these values, we need to convert them into the numeric int [or float] data type using Python's built-in int or float function.

I know this posting is a reply to a very old posting and probably the knowledge has been out there as "common knowledge" for some time. However, I would have appreciated a posting such as this one rather than my having to spend a few hours of searching and hacking until I came up with what I felt was the most elegant solution that can be presented in a CS1 classroom.

How do I read 2 numbers at a time in Python?

Example -.
# taking two inputs at a time..
a, b, c = input["Enter three values: "].split[].
print["Enter Your First Name: ", a].
print["Enter Your Last Name: ", b].
print["Enter Your Class: ", c].
print[].
# taking three inputs at a time..
x, y, z = input["Enter three values: "].split[].

How do you get 2 numbers in Python?

How to Add Two Numbers in Python.
❮ Previous Next ❯.
Example. x = 5. y = 10. print[x + y] Try it Yourself ».
Example. x = input["Type a number: "] y = input["Type another number: "] sum = int[x] + int[y] print["The sum is: ", sum] Try it Yourself ».
❮ Previous Next ❯.

How do you read numbers in Python?

Example 1: Read Number/Integer from Console.
Python Program #read integer from user n1 = int[input['Enter a number: ']] n2 = int[input['Enter another number: ']] print['The sum of two numbers is:', n1+n2].
Output. ... .
Python Program #read integer from user n1 = int[input['Enter a number: ']] print[type[n1]].
Output..

How do you read two integers on the same line in Python?

One solution is to use raw_input[] two times. Note that we don't have to explicitly specify split[' '] because split[] uses any whitespace characters as a delimiter as default.

Chủ Đề