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:
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:
"Hello, World!"is a string value, which is shown as
strin Python. A string is any sequence of numbers, letters, and punctuation; we will learn more about them in lesson 7A.
35is an integer value, which is shown as
intin Python. An integer just means a whole number; for example, 42, -12, and 0 are integers.
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.
(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
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
1.5in the above program, what is first line of output?
zwas printed as
x * yis mixing an
float, which Python treats as two
floats, giving back
- The mathematical value of
zis 1.5 times 2, which is 3, but stored in inexact decimal form, of type
float. When Python prints any
float, even if its value is a whole number, it is printed ending with
to clarify that it is an inexact value.
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 |
Various typecasts behave differently:
- converting a
intloses the information after the decimal point, e.g.
- converting a
intcauses an error if the string is not formatted exactly like an integer, e.g.
int("1.234")causes an error.
- converting a
floatcauses an error if the string is not a number, e.g.
float("sandwich")causes an error.
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.
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...|
Once you are done, head over to the next lesson.