Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 1    ❮    Lesson List    ❮    Top Page

1.1 Variables

1.2 Strings

❯  1.3  Numbers

1.4 Lists

1.5 List Methods

1.6 Slice Operator   

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   10m 47s
☷  Interactive readings   5m

Applying Basic Math Operator

You can add (+), subtract (-), multiply (*), and divide (/) as you do normally in math. 

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

Using String Format on Numbers

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.

Checking and Casting Data Types

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.

WRITE BETTER CODE
Space between Operators

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.

LEARN FROM ERRORS
Error from Hidden Multiplication

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.

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse