Python: Printing, Numbers, and Strings

Question Click to View Answer

What does the following code print to the console?

print("hi there")

The string "hi there" is printed to the console.

The Python print function is used to print strings to the console.

What does the following code print to the console?

print("I am hungry")
# print("give me food!")

"I am hungry" is printed to the console, but "give me food!" is not printed. The pound sign (#) is used to make comments. Comments are added to code for human readers and aren't interpreted by Python.

What does the following code print to the console?

print(2 + 3)

5 is printed to the console.

The + operator is used to calculate the sum of two numbers. In this example, + is referred to as the operator. 2 and 3 are referred to as operands.

What does the following code print to the console?

print(5 / 3)

1.6666666666666667 is printed to the console.

Irrational numbers are automatically rounded.

In this example, / is referred to as the operator. 5 and 3 are referred to as the operands.

What does the following code print to the console?

print(5 > 2)

True is printed to the console because the number 5 is greater than the number 2.

True and False are referred to as boolean values.

In this example, > is referred to as the operator. 5 and 2 are referred to as the operands.

What does the following code print to the console?

print(5 + 6 / 3)

7.0 is printed to the console.

Python follows the order of operations, so division takes place before addition. Six is divided by three and the resulting quotient is added to five.

What does the following code print to the console?

print("sub" + "way")

"subway" is printed to the console.

The + operator performs string concatenation when the operands are strings. In this example, "sub" and "way" are operands and + is the operator.

What does the following code print to the console?

print("fantastic" + 4)

This code raises a TypeError because a string cannot be concatenated with a number. For the + operator to work, both operands must be strings or both must be integers. We can't mix the operand types.

What does the following code print to the console?

print(3 + 4.5)

7.5 is printed to the console.

Python allows for the addition of integers and floating point numbers.