[go: up one dir, main page]

0% found this document useful (0 votes)
88 views2 pages

Python As A Calculator: Arithmetic Operators

Python can be used as a calculator to perform basic arithmetic operations like addition, subtraction, multiplication, division and exponentiation using operators like +, -, *, / and **. It supports two main numeric types - integers (int) for whole numbers and floating point numbers (float) for decimal numbers. When multiple arithmetic operators are combined, operations are evaluated based on precedence rules with exponents having the highest and addition/subtraction the lowest precedence. Syntax errors occur when invalid Python syntax is used while semantic errors happen when valid syntax produces invalid results.

Uploaded by

Julian Moreno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views2 pages

Python As A Calculator: Arithmetic Operators

Python can be used as a calculator to perform basic arithmetic operations like addition, subtraction, multiplication, division and exponentiation using operators like +, -, *, / and **. It supports two main numeric types - integers (int) for whole numbers and floating point numbers (float) for decimal numbers. When multiple arithmetic operators are combined, operations are evaluated based on precedence rules with exponents having the highest and addition/subtraction the lowest precedence. Syntax errors occur when invalid Python syntax is used while semantic errors happen when valid syntax produces invalid results.

Uploaded by

Julian Moreno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python as a Calculator https://d3c33hcgiwev3.cloudfront.net/_b57eab3bf52075f38d322bb0e4b43ee1_calc.html?Expir...

Python as a Calculator Chapter 2.2.


Expressions and
Arithmetic Operators Values: Arithmetic in
Python
Operator Operation Expression English description Result Chapter 2.3. What Is
a Type?
+ addition 11 + 56 11 plus 56 67 Chapter 2.6. How
- subtraction 23 - 52 23 minus 52 -29 Python Tells You
* multiplication 4 * 5 4 multiplied by 5 20 Something Went Wrong
Optional reading
** exponentiation 2 ** 5 2 to the power of 5 32
/ division 9 / 2 9 divided by 2 4.5
// integer division 9 // 2 9 divided by 2 4
% modulo (remainder) 9 % 2 9 mod 2 1

Types int and float

A type is a set of values and operations that can be performed on those values.

Two of Python's numeric types:

int: integer
For example: 3, 4, 894, 0, -3, -18

float: floating point number (an approximation to a real number)


For example: 5.6, 7.342, 53452.0, 0.0, -89.34, -9.5

Arithmetic Operator Precedence

When multiple operators are combined in a single expression, the operations are evaluated in order of precedence.

Operator Precedence
** highest
- (negation)

1 of 2 9/13/2020, 11:38 PM
Python as a Calculator https://d3c33hcgiwev3.cloudfront.net/_b57eab3bf52075f38d322bb0e4b43ee1_calc.html?Expir...

*, /, //, %
+ (addition), - (subtraction) lowest

Syntax and Semantics

Syntax: the rules that describe valid combinations of Python symbols

Semantics: the meaning of a combination of Python symbols is the meaning of an instruction — what a particular combination of symbols does
when you execute it.

Errors

A syntax error occurs when we an instruction with invalid syntax is executed. For example:
>>> 3) + 2 * 4
SyntaxError: invalid syntax

A semantic error occurs when an instruction with invalid semantics is executed. For example:
>>> 89.4 / 0
Traceback (most recent call last):
File "", line 1, in
89.4 / 0
ZeroDivisionError: float division by zero

Jennifer Campbell • Paul Gries


University of Toronto

2 of 2 9/13/2020, 11:38 PM

You might also like