2.1 Python Numbers and Operations

Key Points

  • Learn how to use Python
  • Learn how to process numbers in Python

Preparation Before Class

You are recommended to finish a systematic Python course. The course offered here is more streamlined and focuses on knowledge related to API development. After learning this course, you will be able to develop APIs on your own.

If you have already installed another version of Python (Python 3 or later) and can check the installation, there is no need to install it again.

Installing Python

Go to https://www.python.org/downloads/ and download Python that suits your operating system, as shown below:

Download Python version 3.8.0 or later. Choose the appropriate installation package according to your operating system. It is recommended to download the executable installer, which allows the software to be directly installed.

After downloading, double-click the file to install.

Tick the two boxes as shown below. You can choose to install now or customize installation. When installing, you can follow the default settings.

Installing Pip and Configuring Environment Variables

If you have Python version 3.8.0 or later, pip is included by default.

Pip is a package manager for Python packages and is used to search, download, install, or uninstall Python packages. You need to check whether pip has been installed successfully.

Checking Installation

Enter CMD in the search bar to open the command prompt.

Input:

Python -V
pip -V 

If Python 3.11.3 appears, it means that Python has been installed successfully. If pip ... (Python 3.11.3) appears, it means that pip has been installed by default.

Note: If you see the error message "'Python' is not recognized as an internal or external command, operable program or batch file", this is because Pyhton's executable file is not found in environment variables. You can add Python to the environment variables as follows.

Right-click This PC, then go to Properties > Advanced system settings > Environment variables.

In the Path system variable of the environment variables, add the installation directory of Python.

D:\Software\Python311\Scripts\
D:\Software\Python311\ 

Course Content

Software Version

Python 3 and Python 2 are vastly different in use. This course is mainly based on Python 3.8 (Python 3 or later is all right).

Using Python Based on Different Tools

There are two ways to use Python:

  • Command Prompt
    • Python Shell > command-line interpreter mode
  • IDLE
    • Python Shell > command-line interpreter mode
    • Editor > Edit Mode

Command-line interpreter mode:

>> print(123)

Input a command and press Enter to execute it.

Editor mode:

print(123)    #Press F5 to execute it

The command will not be executed immediately after you press Enter.

1. Command Prompt

Enter cmd or Command Prompt to open the command prompt.

The interface (The interface on Mac may be slightly different):

Input the following command and press Enter, then you can view the version of the installed Python.

Python -V

Input the following command and press Enter to open Python (interpreter).

python

Input the following command and press Enter to print out Hello world.

print("Hello world")

Now you have executed the simplest Python command in the command prompt. Next, you can input the following command and press Enter to exit Python.

exit()

Note: All the parentheses here are in English input format. Using Chinese parentheses will result in an error.

2. IDLE

Click to open Pyhton's IDLE (included with Python installation).

The IDLE Python Shell interface:

Input the following command and press Enter to print out Hello world.

print("Hello world")

3. IDLE Editor

In IDLE, press Ctrl + N to open the IDLE editor.

After you input the code in the editor, it will not run immediately, which is suitable for writing long codes. Input the following command and press Ctrl + S to save the Python file. Then press F5 to execute the command.

print("Hello world")

Code will run in the IDLE interface. In the IDLE editor, you can debug the code.

Getting Help

If you have any questions about Python functions, you can use help() function to get the documentation of the function. Input the following command in IDLE:

help(print) #Help on the function print

help(sum) #Help on the function sum

help("if") #Help on the "if" statement

Processing Numbers

1. Numeric Types

Type

Name

Example

Feature

Integers

int

-1, 2, 5, 10, 666

/

Floating Point Numbers

float

-1.1, 2.25, 666.66

/

Booleans

bool

True, False, (None)

int(False) = 0, int(True) = 1

Complex Numbers

complex

1+2j (1+2j)

Complex numbers are written in the form, x + yj, where x is the real part and y is the imaginary part.

Use the type() function to know which class a value belongs to.

2. Arithmetic Operators

Operations between int/float and int/float result in an int/float.

Operator

Description

Example

+

Addition

1 + 1

-

Subtraction

1 - 1

*

Multiplication

1 * 2

/

Division

1 / 2

//

Floor division

11 // 2

%

Modulus

11 % 2

**

Exponentiation

1 ** 2

3. Comparison Operators

Operations between int/float and int/float result in a bool.

Operator

Description

Example

==

Equal

2 == 2

!=

Not equal

2 != 2

>

Greater than

3 > 2

<

Less than

3 < 2

>=

Greater than or equal to

3 >= 2

<=

Less than or equal to

3 <= 2

4. Assignment Operators

Define a variable 'a' and assign a value to it by using the equal operator. Then use the type() function to check its data type, you can see that the data type of 'a' becomes the same as the one you assigned. In the development process, variables are often used to store data. For example:

a = 1
type(a)

a = "123"
type(a)

a = {"name": ["ziv" , "Yunlin" , "Charlie"] }
type(a)

You can use assignment operations to perform arithmetic operations directly on variables.

Operator

Description

Example

=

Assignment

a = 1

+=

Addition assignment

a += 8

-=

Subtraction assignment

a -= 8

*=

Multiplication assignment

a *= 8

/=

Division assignment

a /= 8

//=

Floor division assignment

a //= 8

**=

Exponentiation assignment

a **= 2

%=

Modulus assignment

a %= 2

5. Logical Operators

Operator

Description

Example

and

Return True if both statements are true.

2 == 2 and 3 == 3, returns True.

or

Return True if one of the statements is true.

1 == 3 or 3 == 3, returns True.

not

Reverse the result, return False if the result is true.

not(1 == 2), returns True.

Note: expressions in parentheses are evaluated first.

6. Membership Operators

Operator

Description

Example

in

Return true if a variable is present in the specified sequence.

1 in [1,2]

not in

Return true if a variable is not present in the specified sequence.

1 not in [2,3,4]

It can also be used in strings, for example,

"i" in "i am ziv"

7. Type Conversion

Function

Description

Example

int()

Convert any data type to an integer.

int(2.8), returns 2.

float()

Convert any data type to a floating-point number.

float(1), returns 1.0.

bool()

Convert any data type to a boolean.

bool(1), returns True.

Was this information helpful?
Yes
NoNo
Need more help? Contact support