Algorithms and Programming Lab Work 2
Arithmetic Operations
Arithmetic operations are a part of number processing in a computer to perform calculations. In addition to performing calculations, arithmetic operations can also be used for logical operations. The foundation of performing calculations in computer arithmetic is addition, also known as an adder. Below are the arithmetic operators in the Python programming language. Here is a simple example of an arithmetic operations program:
Operator | Symbol |
---|---|
Addition | + |
Reduction | - |
Multiplication | * |
Distribution | / |
Remainder of Share | % |
Promotion | ** |
This is was example of the Arithmatics Operation:
Terminal:1
2
3
4
5
6
7
8
9
10
11
12
13
143 + 2
5
18 % 5
3
abs(-7)
7
float(9)
9.0
int(5.3)
5
complex(1, 2)
(1+2j)
2 ** 8
256
Mathematical operators function normally in Python, just like in other programming languages. There are several notes to keep in mind:
- Multiple variables can be assigned the same value in one statement.
- Parentheses () are used to group operations that should be performed first.
- Division of an integer by an integer will round down.
- An integer will be converted to a floating-point number in operations involving both integers and floating-point numbers.
- We cannot convert a complex number to a real number (floating-point or integer); only its absolute value can be obtained.
Assignment Operators
As the name suggests, this operator is used to assign values to variables. For example: age = 18 This means the variable age has been assigned the value of 18. In addition to storing or assigning values, you can also perform addition, subtraction, multiplication, division, etc. More details can be found in the following table.
Operator | Symbol |
---|---|
Filling | = |
Addition | += |
Reduction | -= |
Multiplication | *= |
Distribution | /= |
Remainder of Share | %= |
Promotion | **= |
Because in Python there are no increment & decrement operators, these operators are replaced by assignment operators by entering like this i += 1.
Python Packages
A package is a bundle or grouping of many functions and classes (source code) into a single unit within a library that can be used and called in the source code you are developing, allowing you to access functions without repeatedly typing the source code. Python provides packages for standard operations. For more specialized operations, you need to use functions from other packages. In this practical session, we will learn about arithmetic operations and how to use packages to call trigonometric operation functions provided by Python in the Math package.