Lesson 1 ❮ Lesson List ❮ Top Page
❯ 1.3 Numbers
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 10m 47s
☷ Interactive readings 5m
You can add (+), subtract (-), multiply (*), and divide (/) as you do normally in math.
To express exponents, instead of using ^, Python use **.
Many times, we only want to show 2 or 3 decimal numbers or add commas as thousands separators (like 10,000).
This is possible by using string format:
{variable_name:.2f}
will show only 2 decimal places.
{variable_name:,d}
will show the thousand separators.
Adding string format will not change the value of the variables.
To check the data types, you can use type(). Converting a number (or string) into int, float, or even str can be done using int(), float(), str(), respectively.
You can assign several values using just a single line. This can help make your program shorter and easier to read.
Also, in general, spaces should be included between mathematical and logical operators and other elements in an expression.
Limit space delimiters to 1 space as well.
In math, we used to simplify multiplication like c = b (2a) instead of c = b x (2 x a). Python cannot handle this and it will give TypeError or SyntaxError if you do that.
Division by 0, just like in any calculator, will also give an error: ZeroDivisionError.
Try to put the appropriate * symbols and change the equation in Line 3 to evade those errors.