1.3. Numbers

Python treats numbers in several ways, depends on their different uses. Let's first look at the simplest form of number: integer.

Integer

You can add (+), subtract (-), multiply (*), and divide (/) integers as follows.

To express exponents, instead of using ^, Python use **.

You can use parentheses ( ... ) as you do in math.

The spacing around equations has no effect; it simply helps to spot which operations that have priority.

Floats

A float is any number with a decimal point. You can do the same operations as in the integers.

> Sometimes you can get a long decimal numbers in your answer like the last two example. This happened in all programming languages and you don't need to worry. Just ignore them for now.

Integers and Floats

When you divide two integers, the result will always be a float.

Mixing an integer and a float in any operation will also result in a float, even if the output is a whole number.

Multiple Assignment

You can assign several values using just a single line. This can help make your program shorter and easier to read.

Here, we will assign the variables a, b, c, each has value 1, 2, and 3.

Exercise 1.3

1. Perfect Square
Print the following formula
(186)(1812)/2
in Python.

2. Circumference
The formula for circumference of a circle is 2 x pi x r(radius). Write this in python and print the circumference for the given r and pi.

3. Pythagoras
The Pythagoras formula is
a2 + b2 = c2. Print the value of c from the given a and b.
Hint: Use ** 0.5 for square root.

4. Floor Division
Another math operation, the floor division // , rounds the result down to the nearest whole number. Assign the value 15 to variable x and 2 to variable y then print x//y to see the result.