Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Module 1 - Python Fundamentals Python Python Basics Python Programming: From Zero to Real-World Applications

Numbers and Math Operations in Python

Reviewed & accurate
AI Summary

What You Will Learn

Beginner

  • Integer and float operations
  • All arithmetic operators
  • The math module and common functions

Arithmetic Operators

OperatorOperationExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division7 / 23.5
//Floor division7 // 23
%Modulo (remainder)7 % 21
**Exponent2 ** 38
Terminalpython
# Division always returns float in Python 3
print(10 / 2)     # 5.0 (float, not int)
print(10 // 3)    # 3 (floor division)
print(10 % 3)     # 1 (remainder)
print(2 ** 10)    # 1024 (2 to the power 10)

# Augmented assignment
x = 10
x += 5    # x = x + 5 = 15
x *= 2    # x = x * 2 = 30
print(x)  # 30

The math Module

Terminalpython
import math

print(math.pi)           # 3.141592653589793
print(math.sqrt(16))     # 4.0
print(math.ceil(3.2))    # 4 (round up)
print(math.floor(3.8))   # 3 (round down)
print(math.pow(2, 10))   # 1024.0
print(abs(-5))           # 5
print(round(3.14159, 2)) # 3.14
Integer division gotcha
10 / 3 = 3.333 (float). 10 // 3 = 3 (int, truncated). For rounding use round(): round(10/3, 2) = 3.33.

Practical Exercise

Calculate: 15 * 3 + 7
Calculate: 2 ** 8 (what is 2 to the 8th power?)
Find remainder: 17 % 5
Use math.sqrt(144) to find square root
Round 3.14159 to 2 decimal places: round(3.14159, 2)

Key Takeaways

  • 7 operators: + - * / // % **
  • / always returns float. // returns int (floor).
  • ** is exponent: 2**10 = 1024.
  • % gives remainder: 7 % 2 = 1.
  • math module: sqrt, pi, ceil, floor, pow.
  • Augmented: x += 5 is x = x + 5.
Previously: Lesson 05 covered data types.
Today: You learned: All about numbers and math in Python.
Next: Lesson 07 covers strings.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments