0 ratings0% found this document useful (0 votes) 52 views11 pagesQ 6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
oart2r2028 10.24 Python Operators
3
w Tutorials Exercisesw Servicesey§ QO O Login
schools
Python Operators
[errs] Loew |
Python Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
print(1@ + 5)
Python divides the operators in the following groups
* Arithmetic operators
* Assignment operators
* Comparison operators
* Logical operators
* Identity operators
+ Membership operators
* Bitwise operators
Python Arithmetic Operators
nitpsilwwav@schools.comipythonipylhon_operators.asp unoart2r2028 10.24 Python Operators
3
w Tutorials» —Exercisesw Servicese§ QO Log in
schools
+ Addition xty
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
od Exponentiation xy
YW Floor division x/ly
Python Assignment Operators
Assignment operators are used to assign values to variables:
Operator Example Same As Try it
= x=5 x
+= xt=3 x=xt3
-= x=x-3
es x*=3 x=x*3
/= x/=3 x=x/3
%= x %= 3 X=x%3
= x//=3 x=x//3
ke x3 xex 3
a= x &=3 x=x83
I= x|=3 x=x|3
nitpsilwwav@schools.comipythonipylhon_operators.asp amoart2r2028 10.24 Python Operators
3
w Tutorials Exercisesw Servicesey§ QO O Login
<<= x<<=3 x=x<<3
ADVERTISEMENT
Python Comparison Operators
Comparison operators are used to compare two values:
Operator Name Example Try it
Equal
= Not equal
> Greater than x>y
< Less than x Greater than or equal to x >=y
Less than or equal to x<=y
Python Logical Operators
Logical operators are used to combine conditional statements:
Operator Description Example Try it
and Returns True if both x> Signed
right shift
Sets each bit to 1 if one
of two bits is 1
Sets each bit to 1 if only
one of two bits is 1
Inverts all the bits
Shift left by pushing
zeros in from the right
and let the leftmost bits
fall off
Shift right by pushing
copies of the leftmost bit
in from the left, and let
the rightmost bits fall off
Operator Precedence
Qo Log in
xly
~x
x<<2
x>>2
Operator precedence describes the order in which operations are performed.
Example
Parentheses has the highest precedence, meaning that expressions inside
parentheses must be evaluated first:
print((6 + 3) - (6 + 3))
Example
Multiplication * has higher precedence than addition +, and therefor multiplications
are evaluated before additions:
nitpsilwwav@schools.comipythonipylhon_operators.asp
snoart2r2028 10.24
w
Tutorials +
schools
Python Operators
Exercises» Servicesy§ QO Log in
The precedence order is described in the table below, starting with the highest
precedence at the top:
Operator
oO
4X OK Ox
*“/ I %&
<<>>
not in not
in
not
and
or
Description Try it
Parentheses
Exponentiation
Unary plus, unary minus, and bitwise NOT
Multiplication, division, floor division, and modulus
Addition and subtraction
Bitwise left and right shifts
Bitwise AND
Bitwise XOR
Bitwise OR
Comparisons, identity, and membership operators
Logical NOT
AND
OR
If two operators have the same precedence, the expression is evaluated from left to
right.
Example
nitpsilwwav@schools.comipythonipylhon_operators.asp entoart2r2028 10.24
3
WV tutoriatse —Exercises* Services
schools
Exercise:
Multiply 10 with 5, and print the result.
print(1@ 5)
Start the Exercise
Python Operators
Q 0
ADVERTISEMENT
nitpsilwwav@schools.comipythonipylhon_operators.asp
Log in
amoart2r2028 10.24 Python Operators
v’ Tutorials» —Exercisesw Servicese§ QO Log in
sQ JAVA PHP.
W3Schools Spaces
Build your own
website or software
Get Premium!
COLOR PICKER
nitpsilwwav@schools.comipythonipylhon_operators.asp antoar1212028 10:22 Python Operators
3
w Tutorials» —Exercisesw Servicese§ QO Log in
schools
ADVERTISEMENT
ADVERTISEMENT
nitpsilwwav@schools.comipythonipylhon_operators.asp antoar1212028 10:22 Python Operators
Tutorials~ Exercisese Serviesey§ QO SignUp Login
css JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] c
ADVERTISEMENT
3
‘SPACES UPGRADE NEWSLETTER:
schools
GET CERTIFIED REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
[Link]/python_operators.asp onoar1212028 10:22 Python Operators
3
w Tutorialsy Exercises» Servicesy§ Q @ SignUp Login
= css JAVASCRIPT SQL PYTHON JAVA PHP. HOW TO. [Link] Cc
HTML Reference
C85 Reference
JavaScript Reference
‘SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
‘Angular Reference
{Query Reference
Top Examples Get Certified
vara examples stm certncate
3s Examples
UaveScript Examples SvaScriptceritcate
How To amples rront Ena certeate
501 Examples SQL certificate
Python Examples Python Cericte
Wa.055 Eramples
Bootstrap Examples JQuery certificate
iP Examples Sava Certieate
Java Examples Gee certifeate
HM Examples Ca cericate
[Query examples XML Cerificate
G@ @ @® © Forum asour
\W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2023 by Refsnes Data. All Rights Reserved, W3Schools is Powered by
[Link].
[Link]/python_operators.asp wnt