Lesson 3 - Numbers
Numbers in Python
+ addition operator, e.g. 2 + 2 = 4- subtraction operator, e.g. 10 - 6 = 4* multiplication operator, e.g. 5 * 6 = 30/ division operator, e.g. 8 / 4 = 2% modulo operator, returns the remainder after
dividing one number by another, e.g. 10 % 7 = 3// floor division, first number or number at the
left is divided by the second number or number at the right
and returns the quotient, e.g. 47 // 10 = 4
Some functions
int() used to convert a given value into an
integer.abs() returns the absolute value of a
numbermin() used to find the smallest itemmax() used to find the largest item
The
input() function in Python is a built-in
function used to obtain user input from the console.
Odd or Even
num = 11
if num % 2 == 0:
print("Even")
else:
print("Odd")
if num % 2 == 0:
print("Even")
else:
print("Odd")
Last Digit Finder
num = 128
last_digit = num % 10
print(last_digit)
last_digit = num % 10
print(last_digit)
First Digit of a 2-digit Number
num = 93
first_digit = num // 10
print(first_digit)
first_digit = num // 10
print(first_digit)
Sum of Digits (2-digit)
num = 54
first_digit = num // 10
last_digit = num % 10
sum_digits = first_digit + last_digit
print(sum_digits)
first_digit = num // 10
last_digit = num % 10
sum_digits = first_digit + last_digit
print(sum_digits)
Average of Three Numbers
a, b, c = 4, 8, 12
average = (a + b + c) / 3
print(average)
average = (a + b + c) / 3
print(average)
Double Then Add
x = 7
result = x * 2 + 5
print(result)
result = x * 2 + 5
print(result)
Guess the Remainder
num = 25
remainder = num % 4
print(remainder)
remainder = num % 4
print(remainder)
Reverse a Two-Digit Number
num = 47
tens = num // 10
ones = num % 10
reverse = ones * 10 + tens
print(reverse)
tens = num // 10
ones = num % 10
reverse = ones * 10 + tens
print(reverse)
Smallest and Largest of Three Numbers
a, b, c = 12, 7, 19
smallest = min(a, b, c)
largest = max(a, b, c)
print("Smallest:", smallest)
print("Largest:", largest)
smallest = min(a, b, c)
largest = max(a, b, c)
print("Smallest:", smallest)
print("Largest:", largest)
Guess the Number (Math Puzzle)
secret = 15
guess = int(input("Guess the number: "))
if guess == secret:
print("Correct!")
elif guess < secret:
print("Too low!")
else:
print("Too high!")
guess = int(input("Guess the number: "))
if guess == secret:
print("Correct!")
elif guess < secret:
print("Too low!")
else:
print("Too high!")