This directory contains exercises to help you practice working with Python integers. Unlike Java, Python has a unified int type that can handle both small and arbitrarily large numbers seamlessly.
- Open your terminal/command prompt
- Navigate to this directory
- Run the exercises:
python integers.py - Or work interactively:
python3 -i integers.py
Write a function calculate_sum(a, b) that returns the sum of two integers.
Write a function calculate_product(a, b) that returns the product of two integers.
Write a function find_larger(a, b) that returns the larger of two integers.
Write a function find_absolute_value(number) that returns the absolute value (always positive).
Write a function count_digits(number) that counts how many digits are in a positive integer.
Write a function reverse_digits(number) that reverses the digits of a positive integer.
Write a function is_prime(number) that returns True if the number is prime (only divisible by 1 and itself).
Write a function is_perfect_square(number) that returns True if the number is a perfect square.
Write a function sum_range(start, end) that calculates the sum of all integers from start to end (inclusive).
Write a function count_multiples(number, limit) that counts how many multiples of number exist up to limit.
Write a function parse_int_safely(text) that tries to parse a string to int, returning None if it fails.
Write a function int_to_binary(number) that converts an integer to its binary representation as a string.
Write a function fibonacci(n) that returns the nth Fibonacci number (0, 1, 1, 2, 3, 5, 8, 13...).
Write a function factorial(n) that calculates n! (n factorial).
Write a function to_binary_string(number) that converts an integer to its binary representation.
Write a function from_binary_string(binary_str) that converts a binary string back to an integer.
Write a function find_max(numbers) that finds the maximum value in a list.
Write a function calculate_average(numbers) that calculates the average as a float.
After completing each exercise, test your functions with different inputs:
# Example tests
print(calculate_sum(5, 3)) # Should return 8
print(find_larger(10, 7)) # Should return 10
print(count_digits(12345)) # Should return 5
print(is_prime(17)) # Should return True
print(sum_range(1, 5)) # Should return 15 (1+2+3+4+5)
print(parse_int_safely("123")) # Should return 123
print(parse_int_safely("abc")) # Should return None
print(fibonacci(6)) # Should return 8
print(to_binary_string(10)) # Should return "1010"
print(find_max([1, 5, 3, 9, 2])) # Should return 9- Arbitrary precision: Python integers can be as large as memory allows
- No separate long type: Unlike older languages, Python 3 has unified int
- Automatic type promotion: Operations between int and float return float
- Boolean context: 0 is falsy, all other integers are truthy
- Bitwise operations: &, |, ^, ~, <<, >> for binary manipulation
- Built-in functions: abs(), min(), max(), sum(), divmod()
- Python integers have unlimited precision - no overflow concerns
- Use
isinstance(x, int)to check if something is an integer //for integer division,/for float division**for exponentiation (powers)- Use
range()for generating integer sequences - f-strings are great for formatting: f"The answer is {number}"