• 17

This preview shows page 1 - 5 out of 17 pages.

The preview shows page 3 - 5 out of 17 pages.
Python operatorslast modified July 6, 2020In this part of the Python programming tutorial, we cover Pythonoperators.Anoperatoris a special symbol which indicates a certain processis carried out. Operators in programming languages are takenfrom mathematics. Applications work with data. The operators areused to process data.In Python, we have several types of operators:Arithmetic operatorsBoolean operatorsRelational operatorsBitwise operatorsAn operator may have one or two operands. Anoperandis one ofthe inputs (arguments) of an operator. Those operators that workwith only one operand are calledunary operators. Those whowork with two operands are called binary operators.The + and - signs can be addition and subtraction operators aswell as unary sign operators. It depends on the situation.>>> 22>>> +22>>>The plus sign can be used to indicate that we have a positivenumber. But it is mostly not used. The minus sign changes thesign of a value.>>> a = 1>>> -a-1>>> -(-a)1
Multiplication and addition operators are examples of binaryoperators. They are used with two operands.>>> 3 * 39>>> 3 + 36Python assignment operatorThe assignment operator=assigns a value to a variable. Inmathematics, the=operator has a different meaning. In anequation, the=operator is an equality operator. The left side ofthe equation is equal to the right one.>>> x = 1>>> x1Here we assign a number to anxvariable.>>> x = x + 1>>> x2The previous expression does not make sense in mathematics. Butit is legal in programming. The expression means that we add 1 tothexvariable. The right side is equal to 2 and 2 is assigned tox.>>> a = b = c = 4>>> print(a, b, c)4 4 4It is possible to assign a value to multiple variables.>>> 3 = yFile "<stdin>", line 1SyntaxError: can't assign to literalThis code example results in syntax error. We cannot assign avalue to a literal.Python arithmetic operatorsThe following is a table of arithmetic operators in Pythonprogramming language.
SymbolName+Addition-Subtraction*Multiplication/Division//Integerdivision%Modulo**PowerThe following example shows arithmetic operations.arithmetic.py#!/usr/bin/env python# arithmetic.pya = 10b = 11c = 12add = a + b + csub = c - amult = a * bdiv = c / 3power = a ** 2print(add, sub, mult, div)print(power)All these are known operators from mathematics.\$ ./arithmetic.py33 2 110 4.0100There are three operators dealing with division.division.py#!/usr/bin/env python
# division.pyprint(9 / 3)print(9 / 4)print(9 // 4)print(9 % 4)The example demonstrates division operators.print(9 / 4)This results in 2.25. In Python 2.x, the / operator was an integerdivision operator. This has changed in Python 3. In Python 3, the /operator returns a decimal number.print(9 // 4)The // operator is an integer operator in Python 3.print(9 % 4)The%operator is called the modulo operator. It finds theremainder of division of one number by another.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 17 pages?

Course Hero member to access this document

Term
Two
Professor
N/A
Tags
Binary numeral system, Bitwise operation, Python Operators
• • • 