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.

Quizzes

  1. Write a statement that prints out the float value 188.67.
  1. Write a statement that prints out the string value "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 boolean value True (not the string value "True").
  1. Write a statement that prints out the result of calling the type() function on the value 77.
  1. Write a statement that prints out the type of the value "You talkin' to me?"