0% found this document useful (0 votes)
2 views17 pages

Expressions and Arithmetic

The document explains common arithmetic operators in Python, including unary and binary operators, as well as additional operators like integer division and modulo. It also discusses operator overloading, operator precedence, associativity, and the difference between statements and expressions. Augmented assignment operators are introduced for convenience in performing operations.

Uploaded by

baixiaohan365
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)
2 views17 pages

Expressions and Arithmetic

The document explains common arithmetic operators in Python, including unary and binary operators, as well as additional operators like integer division and modulo. It also discusses operator overloading, operator precedence, associativity, and the difference between statements and expressions. Augmented assignment operators are introduced for convenience in performing operations.

Uploaded by

baixiaohan365
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/ 17

Expressions and Arithmetic

Operators
The following are common operators you can use to form an
expression in Python:

Operator operation example

Unary - Negation. -y

+ Addition x+y

- Subtraction x-y
• Multiplication x*y

/ Division x/y

X and y in the examples are called the left and right operands
respectively.

The first operator is a unary operator, which operates on just one


operand. (+ can also be used as a unary operator, but that is not
useful)

All other operators are binary operators, which operate on two


operands.

Python also supports some more operators such as the


followings:

Operator operation example

// integer division x//y

% modulo x%y
** exponentiation x**y

Integer division returns the integer quotient of the two


operators (the fractional part is discarded)

The modulo operator returns the remainder.

Operator overloading

The same operator behaves differently with different types. For


example, the addition operator and multiplication operator can
also be applied on string data types.

This feature in Python that allows the same operator to have


different meaning according to the text is called operator
overloading.

Operator Precedence and associativity


An expression can consist of a sequence of operations performed
In a row

Rules: 1. Grouping by parentheses: inner grouping first

2. operator precedence : higher precedence first

3. operator associativity:
Left associativity: left operand first

Right associativity: right operand first

Augmented Assignment operators

For convenience, Python defines the augmented assignment


operators such as +=, where x+=1 means x=x+1

Statement vs Expression

A statement is an instruction that the python interpreter can


execute. We have only seen the assignment statement so far.

An expression is a combination of values, variables, operators


and call to functions. Expressions need to be evaluated. If you
ask Python to print an expression, the interpreter evaluates the
expression and displays the result.

You might also like