Lesson 3 of 15

Numbers

Numbers

Python has three built-in number types: int, float, and complex.

Integer Operations

10 + 3   # 13
10 - 3   # 7
10 * 3   # 30
10 / 3   # 3.3333...  (float division)
10 // 3  # 3          (floor division)
10 % 3   # 1          (modulo / remainder)
2 ** 10  # 1024       (exponentiation)

abs(), round(), min(), max()

abs(-5)        # 5
round(3.7)     # 4
round(3.14159, 2)  # 3.14
min(3, 1, 4)   # 1
max(3, 1, 4)   # 4

Type Conversion

int("42")     # 42
float("3.14") # 3.14
str(100)      # "100"

The math Module

import math
math.sqrt(16)  # 4.0
math.floor(3.9)  # 3
math.ceil(3.1)   # 4
math.pi          # 3.14159...

Your Task

Implement is_prime(n) that returns True if n is a prime number, False otherwise.

A prime is a number greater than 1 with no divisors other than 1 and itself. A fast check: test divisors only up to √n.

Pyodide loading...
Loading...
Click "Run" to execute your code.