Input, print and numbers
1. How to read and write in Python
Every program is eventually a data processor, so we should know how to input and output data within it. There exists a function,
print(), to output data from any Python program.
To use it, pass a comma separated list of arguments
that you want to print to the
Let's see an example. Press "run" and then "next" to see how the program
is being executed line by line:
print(5 + 10) print(3 * 7, (17 - 2) * 8) print(2 ** 16) # two stars are used for exponentiation (2 to the power of 16) print(37 / 3) # single forward slash is a division print(37 // 3) # double forward slash is an integer division # it returns only the quotient of the division (i.e. no remainder) print(37 % 3) # percent sign is a modulus operator # it gives the remainder of the left value divided by the right value
To input data into a program, we use
input(). This function reads a single line of text, as a String.
Here's a program that reads the user's name and greets them:
print('What is your name?') name = input() # read a single line and store it in the variable "name" print('Hi ' + name + '!')
2. Sum of numbers and strings
Let's try to write a program that inputs two numbers and prints their sum. We read the two numbers
and store them in the variables
b using the assignment operator
On the left side of an assignment operator we put the name of the variable. The name could be a string of latin characters (
but must start with a letter in the range
On the right side of an assignment operator we put any expression that Python can evaluate.
The name starts pointing to the result of the evaluation.
Read this example, run it and look at the output:
a = input() b = input() s = a + b print(s)
After running the example we can see that it prints
57. As we were taught in school,
5 + 7 gives
12. So, the program is wrong, and it's important to understand why.
The thing is, in the third line
s = a + b Python has "summed" two strings, rather than two numbers.
The sum of two strings in Python works as follows: they are just glued one after another. It's also sometimes
called "string concatenation".
Do you see in the variable inspector, on the right hand side, that the values bound to variables
are wrapped in quotes? That means that the values there are string, not numbers. Strings and numbers
are represented in Python differently.
All the values in Python are called "objects". Every object has a certain type. The number 2 corresponds to an object "number 2" of type "int"
(i.e., an integer number). The string
'hello' corresponds to an object "string 'hello'" of type "str".
Every floating-point number is represented as an object of type "float". The type
of an object specifies what kind of operations may be applied to it.
For instance, if the two variables
"second" are pointing to the objects of type
int, Python can multiply them. However, if they are pointing to the objects of type
str, Python can't do that:
first = 5 second = 7 print(first * second) # you can use single or double quotes to define a string first = '5' second = "7" print(first * second)
To cast (convert) the string of digits into an integer number, we can use the function
int(). For example,
int('23') gives an int object with value
Given the information above, we can now fix the incorrect output and output the sum of the two numbers correctly:
a = int(input()) b = int(input()) s = a + b print(s)