Skip to content
Cover image for: Understanding Time Complexity in Python
#pythonIntermediate

Understanding Time Complexity in Python

Apr 25, 2026
Updated May 14, 2026
30 min read

AI Insights

Powered by GPT-4o-mini

Verified Context: understanding-time-complexity-in-python

Understand Big O notation and time complexity analysis in Python with clear, practical examples. Learn to measure and compare algorithm performance using timeit, analyze loop execution times, identify O(1), O(n), O(n²), and O(log n) patterns, optimize data structure choices, and write efficient code. Covers common pitfalls like premature optimization, space-time tradeoffs, and profiling techniques for real-world Python applications.

This code measures the execution time of a loop that prints numbers from 1 to 9.

Explanation

  • The time module is imported to track the time taken for code execution.
  • start captures the current time in seconds before the loop begins.
  • A for loop iterates through numbers 1 to 9, printing each number to the console.
  • end captures the current time in seconds after the loop completes.
  • The total execution time is calculated by subtracting start from end and printed to the console.
python

Output

text

Converting an Integer to a String Representation in Python

Explanation

  • The program prompts the user to input an integer and initializes an empty string for the result.
  • It uses a while loop to repeatedly extract the last digit of the number using the modulus operator and appends the corresponding character from the digits string to the result.
  • The number is then divided by 10 using floor division to remove the last digit, continuing until the number becomes zero.
  • Finally, the resulting string representation of the integer is printed.
  • The time complexity of this approach is O(log(n)), where n is the input number, due to the number of iterations being proportional to the number of digits in the integer.
python

Output

text

This code calculates the sum and product of a list of integers.

Explanation

  • Initializes a list L containing integers from 1 to 4.
  • Computes the sum of the elements in the list using a for loop, storing the result in the variable sum.
  • Computes the product of the elements in the list using another for loop, storing the result in the variable product.
  • Outputs both the sum and product values to the console.
  • The time complexity for both operations is O(n), where n is the number of elements in the list.
python

Output

text

Nested loops iterate through two lists to print all combinations of their elements

Explanation

  • The code initializes two lists, A and B, containing integers.
  • It uses a nested loop structure where the outer loop iterates through each element in list A.
  • For each element in A, the inner loop iterates through all elements in list B.
  • The print function outputs each combination of elements from A and B as pairs.
  • The time complexity of this code is O(n^2), indicating that the execution time grows quadratically with the size of the input lists.
python

Output

text

This code snippet demonstrates nested loops to print combinations of elements from two lists.

Explanation

  • The code initializes two lists, A and B, containing integers.
  • It uses three nested loops: the outer loop iterates over elements in list A, the middle loop iterates over elements in list B, and the innermost loop runs a fixed number of times (5).
  • For each combination of elements from A and B, the code prints the current values of i and j.
  • The overall time complexity is O(n^2) due to the nested loops, with the innermost loop being constant and not affecting the growth rate.
python

Output

text

This code snippet reverses a list in place using a loop.

Explanation

  • Initializes a list L with five integer elements.
  • Iterates through the first half of the list using a for loop.
  • Calculates the corresponding index from the end of the list for swapping.
  • Swaps elements at the current index and its corresponding end index using a temporary variable.
  • The final output is the reversed list, and the operation runs in O(n) time complexity.
python

Output

text

This code calculates a value based on nested loops and logarithmic growth.

Explanation

  • Initializes variables n, j, and k with values 10, 10, and 0 respectively.
  • The outer loop iterates from n//2 (5) to n (10), resulting in 5 iterations.
  • The inner loop uses j to control the step size, incrementing by powers of 2, which grows rapidly.
  • In each iteration of the inner loop, k is incremented by n/2 (5), leading to a cumulative sum.
  • The final value of k is printed, which reflects the total accumulated from the nested loops.
python

Output

text

This code performs integer division and conditional printing based on variable values.

Explanation

  • Initializes two variables, a with a value of 10 and b with a value of 3.
  • Checks if b is less than or equal to -1; if true, it prints -1 (this condition is false in this case).
  • Calculates the integer division of a by b and stores the result in div.
  • Prints the result of the expression a - div - b, which evaluates to 10 - (10 // 3) - 3.
  • The overall time complexity of the operations is O(1), indicating constant time execution.
python

Output

text

This code calculates the sum of the digits of a given integer.

Explanation

  • Initializes a variable n with the integer value 345 and a variable sum to store the cumulative sum of its digits.
  • Uses a while loop that continues as long as n is greater than 0.
  • Inside the loop, it adds the last digit of n (obtained using n % 10) to sum.
  • Updates n by removing the last digit (using integer division n // 10).
  • Finally, prints the total sum of the digits, which in this case would be 12 (3 + 4 + 5).
python

Output

text

This code defines a recursive function to compute Fibonacci numbers.

Explanation

  • The function fib(n) calculates the nth Fibonacci number using recursion.
  • It returns 1 if the input n is 0 or 1, which are the base cases of the Fibonacci sequence.
  • For values of n greater than 1, it recursively calls itself to sum the two preceding Fibonacci numbers, fib(n-1) and fib(n-2).
  • The time complexity of this implementation is O(2^n), indicating that it grows exponentially with larger values of n.
python

Next in this series: Python Interview Guide: 25 Must-Know Questions & Answers →

Frequently Asked Questions

The execution time is measured by capturing the current time in seconds before and after the loop using the time module, and then subtracting the start time from the end time.
The time complexity of converting an integer to a string representation is O(log n), where n is the input number, due to the number of iterations being proportional to the number of digits in the integer.
The time complexities for calculating both the sum and the product of a list of integers are O(n), where n is the number of elements in the list.
The time complexity of the nested loops is O(n^2), indicating that the execution time grows quadratically with the size of the input lists.

How was this tutorial?

Understanding Time Complexity in Python | Madhu Dadi