P
P
Python Classroom
Search…
⌃K

Input

Introduction

Typically, programs take input from a user, process this input and give the user output.

input function

The input() function takes input from your console. It reads the input and converts it to a string.

Variable Roles

Changing the data type of the input

Function
Converts
Example
int()
converts to an integer
num = int(input("Give me an integer: "))
float()
converts to a float
num = float(input("Give me a float: "))
When using the input function, think about about what data type you want.
name = input("What's your name? ")
num = int(input("Give me a number: "))
money = float(input("How much money do you have? "))
A common mistake is not matching the number of open parenthesis and closed parenthesis.

Example

Write a program that prompts for two numbers. Print the sum, difference, product, and quotient of those numbers. In addition, print the result of integer division and the modulus operator of those numbers.
example1.py
num1 = int(input("Number 1: "))
num2 = int(input("Number 2: "))
print("Sum:", num1 + num2)
print("Difference:", num1 - num2)
print("Product:", num1 * num2)
print("Quotient:", num1 / num2)
print("Integer Division:", num1 // num2)
print("The Remainder of num1 divided by num2:", num1 % num2)