SlideShare a Scribd company logo
The Big O with Python
Learn how to analyze code for efficiency
1
Presented by: Dennis Wainaina
Position: Senior Software Engineer
What’s Big O?
2
What’s Big O?
- How your code performs as you process more data
3
What’s Big O?
- How your code performs as you process more data
- It’s NOT running time
4
What’s Big O?
- How your code performs as you process more data
- It’s NOT running time
- General trend of performance over time
5
What’s Big O?
- How your code performs as you process more data
- It’s NOT running time
- General trend of performance over time
- A way of ranking the efficiency of an algorithm
6
Why Care?
7
Why Care?
- Provides a formal way to talk about how your code
performs in comparison to your data
8
Why Care?
- Provides a formal way to talk about how your code
performs in comparison to your data
- Discussing trade-offs between different approaches
9
Why Care?
- Provides a formal way to talk about how your code
performs in comparison to your data
- Discussing trade-offs between different approaches
- When you notice your code is slowing down and want
to find the bottlenecks
10
Why Care?
- Provides a formal way to talk about how your code
performs in comparison to your data
- Discussing trade-offs between different approaches
- When you notice your code is slowing down and want
to find the bottlenecks
- INTERVIEWS 😂
11
Terminology
- O stands for “Order of”
12
Terminology
- O stands for “Order of”
- N represents how much data you got
13
Some Examples
14
Some Examples
Write a function that calculates the sum of integers from 1
to n
15
Some Examples
def add_one_to_n(n):
total = 0
i = 1
while i <= n:
total += i
i += 1
return total
16
Some Examples
def add_one_to_n_mathematical(n):
return n * (n + 1) / 2
17
So, which one is better?
18
Instead of timing, we can count the number of operations
performed by each piece of code
Let’s count the steps
def add_one_to_n_mathematical(n):
return n * (n + 1) / 2
19
1 multiplication
1 addition
1 division
Let’s count the steps
def add_one_to_n_mathematical(n):
return n * (n + 1) / 2
20
1 multiplication
1 addition
1 division
3 steps regardless of the
size of n
Let’s count the steps
def add_one_to_n(n):
total = 0
i = 1
while i <= n:
total += i
i += 1
return total
21
n additions
1 assignment
1 assignment
n comparisons
Let’s count the steps
- 2n ?
- 3n + 2 ?
- 5n + 2 ?
😩 22
Big O!
- 2n -> O(n)
- 3n + 2 -> O(n)
- 5n + 2 -> O(n)
- O(3) -> O(1)
- O(n + n2
) -> O(n2
)
- O(7n4
+ 2n2
+ 5) -> ???
23
Big O!
So, add_one_to_n_mathematical is O(1) and
add_one_to_n is O(n)
24
A O(n2
) Example
def pairs(n):
for i in range(n):
for j in range(n):
print(i, j)
25
A O(n2
) Example
26
def pairs(n):
for i in range(n): O(n)
for j in range(n):
O(n)
print(i, j)
A O(n2
) Example
def pairs(n):
for i in range(n): O(n)
for j in range(n):
O(n)
print(i, j)
27
Nested O(n)
operations!!!
O(n * n) = O(n2
)
A O(log n) Example
def binary_search(arr, elem):
start = 0
end = len(arr) - 1
middle = math.floor((start + end) / 2)
while arr[middle] != elem:
if elem < arr[middle]:
end = middle - 1
else:
start = middle + 1
if start >= end:
return -1
middle = math.floor((start + end) / 2)
return middle
28
Common Python Operations
29
Lists
my_list.appen
d(a)
O(1)
my_list[i] O(1)
val in my_list O(1)
for val in
my_list
O(n)
my_list.sort() O(nlogn)
Sets
my_set.add(va
l)
O(1)
For val in
my_set
O(n)
val in my_set O(1)
Common Python Operations
30
Dicts
my_dict[key] = val O(1)
my_dict[key] O(1)
key in my_dict O(1)
for key in my_dict O(n)
Visualize Big O Complexity
31
<Thanks>
32

More Related Content

PPTX
Lecture02
PPTX
Lecture 3 time complexity
PPTX
CS 151 Date time lecture
PPTX
L1-Introduction for Computer Science.pptx
DOCX
Lecture1
PPTX
Class[1][23ed may] [algorithms]
PPT
Data Structures and Algorithm Analysis
PPT
Lecture 1 and 2 of Data Structures & Algorithms
Lecture02
Lecture 3 time complexity
CS 151 Date time lecture
L1-Introduction for Computer Science.pptx
Lecture1
Class[1][23ed may] [algorithms]
Data Structures and Algorithm Analysis
Lecture 1 and 2 of Data Structures & Algorithms

Similar to The Big O with Python (20)

PPT
Csc 130 class 2 problem analysis and flow charts(2)
PDF
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
PPT
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
PPTX
Functional pogramming hl overview
PDF
Data Structure - Lecture 1 - Introduction.pdf
PDF
Open Day July 2019
PDF
Parallel and Distributed computing: why parallellismpdf
PDF
Visual Approach to Essbase Calcs: 2018
PDF
Introduction to programming : flowchart, algorithm
PPT
Cs 1114 - lecture-2
PDF
Refactor your specs! Øredev 2013
PPTX
C PROGRAMMING document for beginners....
PDF
Pengenalan algoritma dasar dalam pemrograman
PPTX
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
PPTX
Introduction to cp
PPT
UNIT-2-PPTS-DAA.ppt
PPTX
2-Algorithms and Complexity analysis.pptx
PPTX
Pruning your code
PPTX
uw cse correct style and speed autumn 2020
PDF
Lecture 1
Csc 130 class 2 problem analysis and flow charts(2)
CSC 101-CSC 111 - Introduction to Computer Science - Lecture 4.pdf
UNIT-1-PPT-DESIGN AND ANALYSIS OF ALGORITHMS
Functional pogramming hl overview
Data Structure - Lecture 1 - Introduction.pdf
Open Day July 2019
Parallel and Distributed computing: why parallellismpdf
Visual Approach to Essbase Calcs: 2018
Introduction to programming : flowchart, algorithm
Cs 1114 - lecture-2
Refactor your specs! Øredev 2013
C PROGRAMMING document for beginners....
Pengenalan algoritma dasar dalam pemrograman
Challenges-and-Consideration-in-Programming-Logic-and-Design...pptx
Introduction to cp
UNIT-2-PPTS-DAA.ppt
2-Algorithms and Complexity analysis.pptx
Pruning your code
uw cse correct style and speed autumn 2020
Lecture 1
Ad

Recently uploaded (20)

PDF
Complete Guide to Website Development in Malaysia for SMEs
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
assetexplorer- product-overview - presentation
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PPTX
Transform Your Business with a Software ERP System
Complete Guide to Website Development in Malaysia for SMEs
How to Choose the Right IT Partner for Your Business in Malaysia
CCleaner Pro 6.38.11537 Crack Final Latest Version 2025
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Computer Software and OS of computer science of grade 11.pptx
Design an Analysis of Algorithms I-SECS-1021-03
Oracle Fusion HCM Cloud Demo for Beginners
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
Tally Prime Crack Download New Version 5.1 [2025] (License Key Free
Reimagine Home Health with the Power of Agentic AI​
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Operating system designcfffgfgggggggvggggggggg
assetexplorer- product-overview - presentation
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
Digital Systems & Binary Numbers (comprehensive )
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
17 Powerful Integrations Your Next-Gen MLM Software Needs
Transform Your Business with a Software ERP System
Ad

The Big O with Python

  • 1. The Big O with Python Learn how to analyze code for efficiency 1 Presented by: Dennis Wainaina Position: Senior Software Engineer
  • 3. What’s Big O? - How your code performs as you process more data 3
  • 4. What’s Big O? - How your code performs as you process more data - It’s NOT running time 4
  • 5. What’s Big O? - How your code performs as you process more data - It’s NOT running time - General trend of performance over time 5
  • 6. What’s Big O? - How your code performs as you process more data - It’s NOT running time - General trend of performance over time - A way of ranking the efficiency of an algorithm 6
  • 8. Why Care? - Provides a formal way to talk about how your code performs in comparison to your data 8
  • 9. Why Care? - Provides a formal way to talk about how your code performs in comparison to your data - Discussing trade-offs between different approaches 9
  • 10. Why Care? - Provides a formal way to talk about how your code performs in comparison to your data - Discussing trade-offs between different approaches - When you notice your code is slowing down and want to find the bottlenecks 10
  • 11. Why Care? - Provides a formal way to talk about how your code performs in comparison to your data - Discussing trade-offs between different approaches - When you notice your code is slowing down and want to find the bottlenecks - INTERVIEWS 😂 11
  • 12. Terminology - O stands for “Order of” 12
  • 13. Terminology - O stands for “Order of” - N represents how much data you got 13
  • 15. Some Examples Write a function that calculates the sum of integers from 1 to n 15
  • 16. Some Examples def add_one_to_n(n): total = 0 i = 1 while i <= n: total += i i += 1 return total 16
  • 18. So, which one is better? 18 Instead of timing, we can count the number of operations performed by each piece of code
  • 19. Let’s count the steps def add_one_to_n_mathematical(n): return n * (n + 1) / 2 19 1 multiplication 1 addition 1 division
  • 20. Let’s count the steps def add_one_to_n_mathematical(n): return n * (n + 1) / 2 20 1 multiplication 1 addition 1 division 3 steps regardless of the size of n
  • 21. Let’s count the steps def add_one_to_n(n): total = 0 i = 1 while i <= n: total += i i += 1 return total 21 n additions 1 assignment 1 assignment n comparisons
  • 22. Let’s count the steps - 2n ? - 3n + 2 ? - 5n + 2 ? 😩 22
  • 23. Big O! - 2n -> O(n) - 3n + 2 -> O(n) - 5n + 2 -> O(n) - O(3) -> O(1) - O(n + n2 ) -> O(n2 ) - O(7n4 + 2n2 + 5) -> ??? 23
  • 24. Big O! So, add_one_to_n_mathematical is O(1) and add_one_to_n is O(n) 24
  • 25. A O(n2 ) Example def pairs(n): for i in range(n): for j in range(n): print(i, j) 25
  • 26. A O(n2 ) Example 26 def pairs(n): for i in range(n): O(n) for j in range(n): O(n) print(i, j)
  • 27. A O(n2 ) Example def pairs(n): for i in range(n): O(n) for j in range(n): O(n) print(i, j) 27 Nested O(n) operations!!! O(n * n) = O(n2 )
  • 28. A O(log n) Example def binary_search(arr, elem): start = 0 end = len(arr) - 1 middle = math.floor((start + end) / 2) while arr[middle] != elem: if elem < arr[middle]: end = middle - 1 else: start = middle + 1 if start >= end: return -1 middle = math.floor((start + end) / 2) return middle 28
  • 29. Common Python Operations 29 Lists my_list.appen d(a) O(1) my_list[i] O(1) val in my_list O(1) for val in my_list O(n) my_list.sort() O(nlogn) Sets my_set.add(va l) O(1) For val in my_set O(n) val in my_set O(1)
  • 30. Common Python Operations 30 Dicts my_dict[key] = val O(1) my_dict[key] O(1) key in my_dict O(1) for key in my_dict O(n)
  • 31. Visualize Big O Complexity 31