Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Integer and int Exercises

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.

Setup

  1. Open your terminal/command prompt
  2. Navigate to this directory
  3. Run the exercises: python integers.py
  4. Or work interactively: python3 -i integers.py

Exercises

Exercise 1: Basic Math Operations

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.

Exercise 2: Number Analysis

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).

Exercise 3: Digit Operations

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.

Exercise 4: Number Classification

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.

Exercise 5: Range Operations

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.

Exercise 6: Safe String Conversion

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.

Exercise 7: Mathematical Sequences

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).

Exercise 8: Number Base Conversion

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.

Exercise 9: List Statistics

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.

Testing Your Code

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

Python Integer Features

  • 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()

Tips

  • 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}"