[ Web Proxy ]
URL:
Viewing: https://python-course.eu/python-tutorial/operators.php [Back]  [Original]

8. Operators | Python Tutorial | python-course.eu
python-course.eu
Search site:

Live Python classes by highly experienced instructors:

instructor-led training course [instructor-led training course]
Instructor-led training courses by Bernd Klein

  1. Intro to Python Tutorial
  2. History and Philosophy of Python
  3. The Interpreter, an Interactive Shell
  4. Execute a Script
  5. Structuring with Indentation
  6. Assignment Expressions
  7. Data Types and Variables
  8. Type Annotations
  9. Operators
  10. Sequential Data Types
  11. List Manipulation
  12. Shallow and Deep Copy
  13. Dictionaries
  14. Sets and Frozen Sets
  15. Sets Examples
  16. Keyboard Input
  17. Conditional Statements
  18. Structural Pattern Matching
  19. Adventure Game with Structural Pattern Matching
  20. While Loops
  21. For Loops
  22. Output with Print
  23. Formatted Output
  24. Working with Dictionaries and while Loops
  25. Functions
  26. Passing Arguments
  27. Parameters And Arguments
  28. Namespaces
  29. Global vs. Local Variables and Namespaces
  30. File Management
  31. Modular Programming and Modules
  32. Packages
  33. Errors and Exception Handling

containers [containers]

This website contains a free and extensive online tutorial by Bernd Klein, using material from his classroom Python training courses.

If you are interested in an instructor-led classroom training course, have a look at these Python classes:

instructor-led training course [instructor-led training course]
Instructor-led training course by Bernd Klein at Bodenseo

Image kabliczech - Fotolia.com

This page was written by Bernd Klein.

Bernd is an experienced computer scientist with a history of working in the education management industry and is skilled in Python, Perl, Computer Science, and C++. He has a Dipl.-Informatiker / Master Degree focused in Computer Science from Saarland University.

PDF versions of this site [PDF versions of this site]

PDF logo [PDF logo] PDF version of this site

This website is free of annoying ads. We want to keep it like this. You can help with your donation:

[Go]

The need for donations

8. Operators

By Bernd Klein. Last modified: 29 Jun 2022.

Introduction

This chapter covers the various built-in operators, which Python has to offer.

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

See our Python training courses

See our Machine Learning with Python training courses

Operators

These operations (operators) can be applied to all numeric types:

Operator Description Example
+, - Addition, Subtraction 10 -3
*, % Multiplication, Modulo 27 % 7
Result: 6
/ Division
This operation brings about different results for Python 2.x (like floor division) and Python 3.x
Python3:
10  / 3
3.3333333333333335
and in Python 2.x:
10  / 3
3
// Truncation Division (also known as floordivision or floor division)
The result of this division is the integral part of the result, i.e. the fractional part is truncated, if there is any.
It works both for integers and floating-point numbers, but there is a difference between the type of the results: If both the dividend and the divisor are integers, the result will also be an integer. If either the divident or the divisor is a float, the result will be the truncated result as a float.
10 // 3
3
If at least one of the operands is a float value, we get a truncated float value as the result.
10.0 // 3
3.0
>>> 
A note about efficiency:
The results of int(10 / 3) and 10 // 3 are equal. But the "//" division is more than two times as fast! You can see this here:
In [9]: %%timeit
for x in range(1, 100):
    y = int(100 / x)
      : 
100000 loops, best of 3: 11.1 μs per loop
In [10]: %%timeit
for x in range(1, 100):
    y = 100 // x
       : 
100000 loops, best of 3: 4.48 μs per loop
+x, -x Unary minus and Unary plus (Algebraic signs) -3
~x Bitwise negation ~3 - 4
Result: -8
** Exponentiation 10 ** 3
Result: 1000
or, and, not Boolean Or, Boolean And, Boolean Not (a or b) and c
in "Element of" 1 in [3, 2, 1]
<, ≤, >, ≥, !=, == The usual comparison operators 2 ≤ 3
|, &, ^ Bitwise Or, Bitwise And, Bitwise XOR 6 ^ 3
<, > Shift Operators 6 < 3

Live Python training

instructor-led training course [instructor-led training course]

Enjoying this page? We offer live Python training courses covering the content of this site.

Upcoming online Courses

See our Python training courses

See our Machine Learning with Python training courses

top


Web Proxy Viewer  |  New URL  |  Original Page