Input, print and numbers - Learn Python 3 - Snakify

Lesson 1. 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 data into it and how to output them. There's a function print() to output data from Python program. To use it, fill in its parentheses with a comma-separated list of the values that you want to print. 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
print(37 / 3)  # single forward slash is a division
print(37 // 3)  # double forward slash is an integer division
print(37 % 3)  # percent sign is a modulus operator
        # it gives the remainder of the left value divided by the right value

Use the function input() to input data into the program. This function reads a single text line as a string.

Here's a program that reads the user's name and greets him:

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 a and 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. 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 its 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 right side that the values bound to variables a and b are wrapped in quotes? That means that the values there are string, not numbers. Strings and values 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 "first" and "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 the string of digits into an integer number, we can use the function int(). Eg., int('23') gives an int object 23.

Given the information above, we can now fix our wrong program to sum the two numbers:

a = int(input())
b = int(input())
s = a + b
print(s)