numpy tutorial - introduction | numpy array vs python list
Key Takeaways
Introduces numpy and its features
Full Transcript
dear friends we are going to look into numpy module today it is extremely popular in Python community and it is heavily used for scientific Computing we will see why it is so popular so let's first start by installing it I will install it using pip you can run pip install numpy command to download and install it uh while it is installing uh numpy's main object is end dimensional array okay so we will look into that array so here uh I have my python idle shall open and I'm going to import numpy module here so num I imported it as NP and to create an array object you will use np. array and in this bracket you just pass down your list so this is my uh numpy array if you look into it now this is sort of similar to a list it is actually very similar to a list you can access the elements by index looks like a list so the question is I already have a list why do I need this numpy array well there are several benefits of of using numpy uh array uh there are three main benefit benefits uh first is it requires less memory it is fast and convenient and we'll go over all these three benefits one by one okay so essentially what we are doing is we are comparing numpy with numpy array with a python list here okay so let's first create a python list I'm just going to say range th000 so I create created a list which has th000 elements here and I'm going to print the size of the list now to print the size uh you need to get a size of one one element so one element here is one number you can give any number here and then length of your uh list second I'm going to create a numpy array so this is how you create a numi array this function a range is similar to range is going to create an array with element 0 to 999 okay so the size of this numpy array would be 1,000 and to print the size of this array you need to call array do size so array do size will give you, okay and and the size of every element will be it will be item size so just remember that array do item size is size of one element versus ar. size is the total length of an array when you run this program you'll notice that the python list is taking taking 14,000 bytes versus nump is taking only 4,000 bytes now this is because your python uh the size of one python object is like 14 okay H versus size of one numpy array object is here in this case since we are storing integer numbers it is only four bytes okay so if you look at this diagram it kind of show this shows the memory presentation of uh list and numpy array on the left hand side we have numpy array which uh points to a continuous or continuous uh location of memories where you have all your elements stored 1 2 3 4 5 and so on and each of these element con is occupying four by of memory versus in case of python list you all know that uh in Python everything is an object so the list will contain a list of pointers first as you can see in the diagram and each point pointer will then point to another location in the memory which will be your object and the size of that one object will be 14 bytes so this is the reason why uh numpy array the memory usage is very less compared to a plain python list now when your array size or List size is very small you won't notice much difference but let's say you are dealing with heavy amount of data where you have millions and billions of numbers to process in that case it will make sense to use numpy all right the second advantage of numpy is that it is fast and convenient so let me prove that point as well so I'm going to what I'm going to do here is um all right let me just close this guy here and I'm going to now create here two list first call it L1 and L2 okay so range uh size so size is th000 here and L2 is also similar so I created two list here and then I'm going to create two array so np. AR range again to create a numpy array and then second array will be called A2 okay now I want to measure the time uh between list processing and nump array processing so for this you use start is equal to time. time and now what I'm going to do is see the thing the operation that I'm doing here is I'm adding these two list I'm adding uh these two list and producing a new list call call it result okay so to order these two list you have to do something like this x + y for XY IN Zip L1 L2 what this will do is it will take first element from L1 first element from L2 add them together and put it in this result as a first element so it will just uh add individual elements from these two list and put it in a result okay pretty straightforward uh and I'm just going to say python list took this much time so right now the time is this I'm going to subtract start from here and I'm going to multiply this guy with th because by default it is in second I want to print milliseconds here all right now again at this point I will capture time one more time and then I will store result here so now so this was your python list okay so uh let me just say python list and here now I'm processing numpy array so that's the difference between these two code blocks uh now numpy is also convenient because if you want to add two arrays you just say A1 + A2 right so it is not like you have to write a list comprehension okay you can just add them together and you can just say print my numpy to uh this is just a way to measure the time that this code block took okay all right so let's run this program here okay says python list took 0.0 numi took 0.0 because this size is pretty small so I'm going to increase the size and do a processing for a about a million elements here now you can see that in order to add million uh elements from two list python list took 116 millisecond versus numpy array took 11.5 seconds second this is crazy fast so you can see that again when you're processing millions and millions of numbers it just makes sense to use numpy it it is also convenient because you can see that if you want to add two list together you just do A1 and A2 a1+ A2 I I will just demonstrate that using idle because it's uh it's little easier to demo that in idle uh so I'm going to to create let's say two numpy array okay A1 A1 is np. array it has 1 2 3 and A2 is again np. array it has let's say element 4 56 okay so you can do A1 + A2 so it will give you that see 4 and 1 5 2 and 5 7 and so on you can also do A2 - A1 so you can see you subtracted this guy from here you can also do multiplication so A1 cross A2 you can do division and so on you can do all these operations okay so that was all about numpy introduction you saw that why numpy is so better than so much better than the plain python uh list uh we will cover more about numpy in our next tutorial thank you for watching
Original Description
This tutorial covers an introduction to numpy python module. We'll see why numpy is very popular and talk about its main feature "n dimensional array". It is memory efficient, fast and convenient compared to python native list.
Topics that are covered in this Python Video:
0:00 what is numpy?
1:24 benefits of numpy array over python list
3:48 numpy using less memory (Memory presentation diagram of numpy and python array )
5:08 why numpy is fast and convenient
Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses.
Next Video:
numpy tutorial - basic array operations: https://www.youtube.com/watch?v=a8aDcLk4vRc&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=40
Website: https://codebasics.io/
📸 Codebasics Instagram: https://www.instagram.com/codebasicshub/
🔊 Codebasics Facebook: https://www.facebook.com/codebasicshub
📝 Codebasics LinkedIn: https://www.linkedin.com/company/codebasics/
📱 Codebasics X handle: https://twitter.com/codebasicshub
📸 Dhaval's Instagram: https://www.instagram.com/dhavalsays/
📝 Dhaval's LinkedIn: https://www.linkedin.com/in/dhavalsays/
📱 Dhaval's X handle: https://x.com/dpcodebasics
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from codebasics · codebasics · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related AI Lessons
Chapters (4)
what is numpy?
1:24
benefits of numpy array over python list
3:48
numpy using less memory (Memory presentation diagram of numpy and python array )
5:08
why numpy is fast and convenient
🎓
Tutor Explanation
DeepCamp AI