Metadata
Title
4: Types
Category
general
UUID
1b3942dd668346068a5916cd26e3ec8a
Source URL
https://cscircles.cemc.uwaterloo.ca/4-types/
Parent URL
https://cscircles.cemc.uwaterloo.ca/
Crawl Time
2026-03-18T05:13:22+00:00
Rendered Raw Markdown

4: Types

Source: https://cscircles.cemc.uwaterloo.ca/4-types/ Parent: https://cscircles.cemc.uwaterloo.ca/

From this lesson onwards, examples and code input boxes have buttons labelled Open in console and Visualize. Use them to help debug and explore the code.

In the Hello, World! program we saw that Python was able to repeat a sentence back to us. We have also seen several examples of arithmetic with numbers. Numbers and sentences are fundamentally different objects, and it causes a Python error when you try to mix them in the wrong way:

Example

Trying to compare a string and a number.

x = "Hello, World!" y = 35 z = max(x, y)

As you can see, we get an error saying that the two arguments to max are of different types. The error is a good introduction to the rest of the lesson:

Using an object of a bad type is a very common cause of errors in programs. It is like trying to drink a sandwich: you can't do it because you can only drink things of liquid type, and a sandwich is of solid type.

You can determine the type of an object by calling the type function on it.

Example

Some examples of types.

print(type("Hello, World!")) print(type(34)) print(type(1.234))

(The meaning of class is similar to type.) The above example demonstrates that numbers are further divided into two different types, int which we mentioned above, and float, which is used for storing decimal numbers. You should think of floats as inexact or approximate values (we will explain more in lesson 7B). You can usually mix float values with int values, and the result will be another float.

Example

Mixing an int and a float.

x = 1.2 y = 2 z = x * y print(x, y, z) print(type(x), type(y), type(z))

In fact, what Python really does when you mix a float with an int is that it converts the int to a float, and then works with the two floats.

Multiple Choice Exercise: Floating

If we change 1.2 to 1.5 in the above program, what is first line of output?

Your choice: Select oneAn error occurs1.5 2 3.01.5 2 31.5 2.0 3.0

Correct! We have to explain two facts here, to see why z was printed as 3.0.

Also, note that the value and type of y never changed.

It is often necessary to change data from one type to another type. Just as you can convert a sandwich from solid to liquid form by using a blender, you can change data from one type to another type using a typecast function. You write the name of the desired new type in the same way as a function call, for example

x = float("3.4")
print(x-1)

changes the string "3.4" to the float 3.4, and then prints out 2.4. Without the typecast, the program would crash, since it cannot subtract a number from a string.

Sometimes, Python does let you combine strings and numbers using arithmetic operators. The statement print("hots" * 2)prints hotshots. Python's rule is that multiplying a string s by an integer n means to put n copies of the string one after another. We'll see later that "addition of two strings" is also well-defined in Python.

Various typecasts behave differently:

A common use of typecasting that we will see soon is to convert user input, which is always a string, to numerical form. Here is a quick illustration.

Example

Example of typecasting.

inputStr = "12" # a piece of user input print("The input type is", type(inputStr)) x = int(inputStr) print(x, "is of type", type(x), "and its square is", x*x) print(inputStr * inputStr) # this line should cause an error

Here is one more exercise to finish the lesson.

Because there are now lots of editor commands, some have been moved into the menu labelled More actions...

Coding Exercise: Tasty Typecasting

Write a program to help you feed your friends at a party by doing some math about square pizzas. Assume the grader defines a string inputStr for you, which is a decimal string meaning the side length L of the pizza in cm. The area of the pizza should be computed using the formula A = L*L. Then, assuming that each person needs to eat 100 cm2 of pizza, compute the number of people it can feed, rounded down to the nearest integer. Hint

Example: if inputStr is "17.5", the area will be 306.25 cm2, so 3 is the correct output.

You need to create an account and log in to ask a question.

delete this comment and enter your code here

More actions... Help

Once you are done, head over to the next lesson.