Print Statements and Types

Print statements

The print() function displays values. Put the value you want to print inside the parentheses. Here is an example that prints "Hello world".

Try it yourself by typing print("Your Name").

A print statement can print more than one value. Separate the values with commas. By default, Python inserts a space between them. Try this and see:

The two values in that print statement are different types. The first one is a string, the second one is a floating-point number (a number with a decimal point).

We can produce the exact same output like this (give it a try!):

Types

Python distinguishes between different types of values. Here we will talk about four basic Python types: integers, floating-point numbers, strings and Booleans. We will encounter some other types later in the course.

Integers

Integers are numeric values that do not contain a decimal point. In Python 3 integers can be as long as you want (within the limits of your computer's memory).

Floating-point numbers

Floating-point numbers are numeric values that do contain a decimal point. In mathematics, these are called real numbers. Integers are a subset of the real numbers. The value 4 is an integer, but the value 4.0 is a floating-point number.

Floats can also be written using scientific notation, using either e or E followed by a positive or negative integer (the exponent).

The maximum value of a float is 1.7976931348623157e+308. Floats are stored as binary fractions. Unfortunately most fractional values cannot be represented exactly in binary. Just as 1/3 cannot be represented exactly in decimal notation with a finite number of digits (0.333333333333333...), 1/10 cannot be represented exactly in binary notation with a finite number of digits (0.000110011001100...). Because of this, floats are approximate values, but the approximation is accurate to several decimal places.

Strings

Strings are zero or more characters between a pair of quotation marks. You can use either single quotes or double quotes. The length of a string is only limited by your computer's memory.

There are two ways to put a quotation mark inside a string. One way: if the string contains a double quote, use single quotes to mark the ends of the string (or vice versa).

Another way is to put a backslash character in front of the quotation marks that are part of the string.

The backslash is an "escape character", because it removes the special meaning of the character after it. To put a backslash itself in a string, write two in a row (the first removes the special meaning of the second).

Normal strings cannot be split across multiple lines. Later we will see a special kind of string (called a docstring) that can be.

Booleans

There are only two Boolean values, True and False. The first letter of those values must be capitalized.

Determining type

We can use the type() function to tell us what the type of a particular value is. Try these examples to find out the type of the values in the parentheses.

Now check what you have learned.

Check your understanding. Predict the result. Name the type of each value.

first = 5
second = 5.0
third = "5"
fourth = 5 > 3

Set each variable below to the name of the type, written as a string between quotes, and give it a type hint. The names are "int", "float", "str" and "bool". The hint is str because you are writing the name of a type as text. The comparison 5 > 3 is evaluated before it is stored.

Hint 1

A decimal point makes a float. Quotation marks make a string. A comparison gives True or False.

Quizzes

  1. Write a statement that prints out the type <class 'float'>. Pick a value of that type and use the type() function on it.
Hint 1

A float is a number with a decimal point. Pick any number that has one.

  1. Write a statement that prints this line exactly as shown, double quotes included:
"Knock knock, "Who's There?", "Your Mom."
Hint 1

A string can be surrounded by triple quotes '''...''', which lets you include both single and double quotes inside it.

  1. Write a statement that prints out the type <class 'bool'>. Pick a value of that type and use the type() function on it.
Hint 1

A boolean has only two possible values, and they are written as words without quotes. With quotes they would be strings.

  1. Write a statement that prints out the type <class 'int'>. Pick a value of that type and use the type() function on it.
Hint 1

An int is a whole number with no decimal point. Pick any whole number.

  1. Write a statement that prints out the type <class 'str'>. Pick a value of that type and use the type() function on it.
Hint 1

A string is any text between quotes. Pick any words you like, with the same kind of quote at each end.