Start your career in Software Development and make $80k+ per year! coursecareers.com/a/techwithtim?course=software-dev-fundamentals
@XrayTheMyth23 Жыл бұрын
Your timestamps are mislabeled for if_name and function types.
@animeshmukherjee3676 Жыл бұрын
Hi Tim please make a video about GIL in Python and mulithreading in Python.
@pinnaclemindset8985 Жыл бұрын
what do you think about Mojo programming language
@shashwatpatil4831 Жыл бұрын
please make a video about GIL and why does python not support multithreading
@Singlton Жыл бұрын
Mojo programming language is super set of python and it is 35000x faster than python
@Gaurav-gc2pm Жыл бұрын
Working as a python dev and in my 1 year of practicing python... no one ever explained this well... you're a GEM TIM
@Ness-d2c5 ай бұрын
i find it very informative too
@jeffrey61243 ай бұрын
Agree! You got another GEM from me Tim 🤓
@nordicesАй бұрын
Tim is the best channel for python
@donaldjohnson8522Ай бұрын
I’m a complete beginner at coding. Do you suggest free code camp or something else?
@apmcd47 Жыл бұрын
At around the 4 minute mark you are confusing immutability with references. When you do 'y = x' what you are doing is assigning the reference of the object that x is pointing to, to y. When you assign a new object to x it drops the old reference and now refers to a new object, meanwhile y still refers to the original object. You use tuples in this example, but this is true for lists and dicts. When you change to lists, all you are really demonstrating is that x and y refer to the same object. With your get_largest_numbers() example, if you were to pass a tuple into the function you would get an AttributeError because you were passing an immutable object which doesn't have the sort method.
@itsjaylogan Жыл бұрын
Thank you so much for correcting this section of the video. I hope enough people read this and try it so they can correct their understanding of the concept.
@eugeneo1589 Жыл бұрын
Isn't Python treat immutable types (strings, numbers, tuples) as literals, while lists and dicts are basically objects? Once you assign different value to a string or number or any other immutable type variable, you're actually creating another literal object, but the one you created previously still resides in memory and will be purged later, no?
@CliffordHeindel-ig5hp Жыл бұрын
Yes, thank you. This kind of sloppy presentation should be career ending.
@Elliria_home8 ай бұрын
Actually, Tim was right and was pointing out the possibly-unexpected behavior one can run into with mutable types: If you create x, create y with the value of x, and then REPLACE x by creating x again, then x and y will have different values. Try it yourself: x = [1, 2]; y = x; x = [1, 2, 3]; print(x, y) If you create x, create y with the value of x, and then CHANGE x by reassigning one of its values, then x and y will have the same new value and the original value will be gone. Try it yourself: x = [1, 2]; y = x;x[0] = 9; print(x, y)
@jcwynn40757 ай бұрын
@@CliffordHeindel-ig5hp your type of comment should be career ending 😂
@zecuse Жыл бұрын
Some details skipped about *args and **kwargs: A forward slash "/" can be used to force parameters to be positional only, thereby making them required when calling and not by name. So, def function(a, b, /, c, d, *args, e, f = False, **kwargs) means a and b cannot have default values, are required to be passed when calling function, AND can't be supplied with their parameter names. e must also be supplied with a value when called. Naming the first * is not required. Doing so simply allows the function to take an arbitrary amount of positional parameters. def function(a, b, /, c, d, *, e, f = False) would require at least 5 arguments (no more than 6) passed to it: a and b are required, c and d are also required and optionally passed as keywords, e must be passed as keyword, f is completely optional, and nothing else is allowed. / must always come before *. * must always come before **kwargs. **kwargs must always be last if used.
@timo_b3 Жыл бұрын
thanks
@kmn1794 Жыл бұрын
I didn't know the kwonly args after *args didn't need a default. The posonly arg names can also be used as kwarg keys when the signature accepts kwargs.
@user-sj9xq6hb9p Жыл бұрын
you can also use "*" to force but I the more apt way is to use "/" I guess
@hamzasarwar2656 Жыл бұрын
Your description is accurate and provides a clear understanding of the use of /, *, and **kwargs in function parameter definitions in Python. Let's break down the key points: / (Forward Slash): When you use / in the function parameter list, it indicates that all parameters before it must be specified as positional arguments when calling the function. This means that parameters before the / cannot have default values and must be passed in the order defined in the parameter list. Parameters after the / can still have default values and can be passed either as keyword arguments or positional arguments. * (Asterisk): When you use * in the function parameter list, it marks the end of positional-only arguments and the start of keyword-only arguments. Parameters defined after * must be passed as keyword arguments when calling the function. They can have default values if desired. **kwargs (Double Asterisks): **kwargs allows you to collect any additional keyword arguments that were not explicitly defined as parameters in the function signature. It must always be the last element in the parameter list if used. Here's an example function that demonstrates these concepts: python Copy code def example_function(a, b, /, c, d, *, e, f=False, **kwargs): """ a and b must be passed as positional arguments. c and d can be passed as positional or keyword arguments. e must be passed as a keyword argument. f is optional and has a default value. Any additional keyword arguments are collected in kwargs. """ print(f"a: {a}, b: {b}, c: {c}, d: {d}, e: {e}, f: {f}") print("Additional keyword arguments:", kwargs) # Valid calls to the function: example_function(1, 2, 3, 4, e=5) example_function(1, 2, c=3, d=4, e=5) example_function(1, 2, 3, 4, e=5, f=True, x=10, y=20) # Invalid calls (will raise TypeError): # example_function(a=1, b=2, c=3, d=4, e=5) # a and b must be positional # example_function(1, 2, 3, 4, 5) # e must be passed as a keyword By using /, *, and **kwargs in your function definitions, you can create more structured and expressive APIs and enforce specific calling conventions for your functions.
@jcwynn40757 ай бұрын
He definitely should've included this info in the video. I've learned this before but am not a professional programmer so haven't used it, so seeing it in this video would help non-experts like me. Also, this can be inferred from the explanations above, but maybe still worth stating explicitly: Parameters between / and * can be positional OR named. And the function won't work if * comes before /, since the parameters in between would be required positional and required keyword, which creates a contradiction.
@koflerkohime2981 Жыл бұрын
Great content. Keep it up. However, I believe there is a mistake at 3:34. You mention that we have some sort of automatic "copying" going on with "y = x" when using immutable types. This is actually not correct. The assignment still works exactly like with any other object - the reference x is assigned to y. Identifiers x and y are simply referring to the same 2-tuple object. After that, you change what identifier x is referring to (another 3-tuple) and print out the two individual objects. The identifiers are still references - even if using immutable objects.
@illusionofquality979 Жыл бұрын
I might be dumb but don't you mean "x" instead of "y" here: "After that, you change what identifier y is referring to"
@kungfumachinist Жыл бұрын
Came here to say the same. The point can be illustrated with this code, x and y point to the same thing: >>> x = 1 >>> y = x >>> print(hex(id(x)), hex(id(y))) 0x7f82aa9000f0 0x7f82aa9000f0
@koflerkohime2981 Жыл бұрын
@@illusionofquality979 Yes, indeed you are correct. I have edited my comment.
@TohaBgood2 Жыл бұрын
The GIL can be bypassed by using parallelism which offers about the same capabilities as threads in other languages. This is more of a naming convention issue rather than an actual thing that you can't do in Python. Python threads are still useful for IO and similar async tasks, but they're simply not traditional threads. It's important to highlight these kinds of things even for beginners so that they don't go out into the world thinking that you can't do parallelism in Python. You absolutely can. It's just called something else.
@umutsen2290 Жыл бұрын
Hello dear sir, You mentioned that 'It's just called something else', and what came up to my mind is that another threading library named _thread which is meant for low level threading and also multiprocess library that allows users to run multiple python clients. Am I correct or did you mean something else?
@Joel-pl6lh Жыл бұрын
Thank you, that was a bit misleading. How can you do "multithreading" in python then?
@TohaBgood2 Жыл бұрын
@@Joel-pl6lh The library of choice for actual parallel processing in Python is _multiprocessing_ It has a similar interface, but gives you actual parallel computing on different CPU cores.
@Joel-pl6lh Жыл бұрын
@@TohaBgood2 That's what I found too, thank you because I'd have thought it's not possible. I wonder why he included this in the video?
@ruotolovincenzo94 Жыл бұрын
Agree, in the GIL part of the video there is a lot of confusion since multi-threading is mixed with multi-processing, and not a clear definition has been provided, which contributes to confuse who approaches to these concepts. It simply does not exist a multi-threading code, in all the coding languages, that executes threads at the same time
@TonyHammitt Жыл бұрын
I wanted to mention that the if name is main thing is frequently used for test code for libraries. Your code may have some functions to import elsewhere, then you can do examples in the main of how to use them, or try various failure cases, illustrate how to catch exceptions, etc. Also, to those getting into programming, please do yourself a favor and leave a comment in your code as to what it's for. The most likely person to be reading your code later is you, but if you wrote it 6 months ago, it might as well have been written by someone else, so be kind to yourself.
@yutubl Жыл бұрын
Thanks. Most things I already know, so my takeaway: 1.) immutable types = C#/.NET valuetypes or Java primitive types, plain data types in C/C++, Pascal and mutable types = C#/.NET reference types or Java object types, C++ reference, dereferenced pointer data aka memory location in C/C++/Pascal/Assembler. 2.) List comprehension is reverted looping writing style (like perl?). 3.) Function arguments look similar to other languages here added dynamic argument *args and ** kwargs little bit like C's ... period argument and function. 4.) __name__=="__main__" unique feature? Easy, but unique, as I didn't saw dynamic caller backreference in another language. 5,) I thought GIL is about single threading
@zedascouve2 Жыл бұрын
Absolutely brilliant for beginners. Crystal clear. I had countless errors due to the lack of understanding of mutable vs immutable variables
@andrewcrawford29777 ай бұрын
I'm glad I stuck around; I had no idea about some of those other tips like in the function calls.
@pharrison306 Жыл бұрын
Please do a global interpretor lock, love your explanation style, clear and concise. Keep it up
@adrianoros4083 Жыл бұрын
this is just what ive been searching, please elaborate on python interpretor and how does it differ from C compiler, noting that python is developed in C.
@phinehasuchegbu8068 Жыл бұрын
Please do this man!!!
@xxd1167 Жыл бұрын
@@adrianoros4083 c compiler is very fast than python interpreter due to the defining of type of variable before compiling
@harrydparkes Жыл бұрын
@@xxd1167bro you clearly have no clue what you're talking about
@midtierplayer3890 Жыл бұрын
@@xxd1167 If you don’t know what you’re talking about, please don’t post anything. Stuff like this hurts those who are here to learn.
@narutoxboruto873 Жыл бұрын
There is an error in the time stamps ,names of function arguments and if __ are interchanged
@TechWithTim Жыл бұрын
thanks, just fixed it :)
@mariof.1941 Жыл бұрын
Certainly! In addition to multithreading, Python also provides the multiprocessing module, which allows for true parallel execution across multiple processor cores. Unlike multithreading, multiprocessing bypasses the limitations imposed by the Global Interpreter Lock (GIL) since each process gets its own Python interpreter and memory space. By utilizing multiprocessing, you can take advantage of multiple processor cores and achieve parallelism, which can significantly improve performance in computationally intensive tasks. Each process operates independently, allowing for efficient utilization of available CPU resources. However, it's important to consider that multiprocessing comes with some overhead due to the need for inter-process communication. Data exchange between processes can be more involved and slower compared to sharing data between threads within a single process. As a result, multiprocessing may not always be the best choice for every situation. To determine whether to use multithreading or multiprocessing, it's crucial to evaluate the specific requirements and characteristics of your application. If the task at hand is primarily CPU-bound and can benefit from true parallel execution, multiprocessing can be a suitable option. On the other hand, if the workload consists of I/O-bound operations or requires a high degree of coordination and shared state, multithreading might be more appropriate. In summary, the multiprocessing module in Python offers a way to achieve true parallelism by leveraging multiple processor cores. While it circumvents the limitations of the GIL, it introduces additional overhead for inter-process communication, which may impact performance. Careful consideration of the specific requirements and trade-offs is necessary to determine the most suitable approach for your use case.
@nokken__1031 Жыл бұрын
least obvious chatgpt user
@excessreactant9045 Жыл бұрын
Certainly!
@mariof.1941 Жыл бұрын
@@excessreactant9045 Yes i using ChatGPT to translate from my Native Language in Englisch + I Used it to put more information in it
@flor.7797 Жыл бұрын
😂❤
@Gruuvin1 Жыл бұрын
Because of the GIL, Python multi-threading is not useful for processor-bound computing, but it is still great for I/O bound computing (processor waits for input and output; example: disk read/write or networked data). Multiprocessing is a great way to get around the GIL, when you need to.
@Pumba1287 ай бұрын
At 3:45 with: x = (1, 2) y = x you are not doing a copy, it is still an assignment to an immutable object. You can check it with: print(x is y) This returns True, meaning that both x and y are referencing the same object - a tuple (1, 2). And of course print(x == y) also returns True, as we are comparing an object with itself.
@Eeatch8 ай бұрын
I am currently doing Python courses and i struggle a lot, i like that you distinguished parameters and arguments correctly and basically everything else what you've said is exactly the same things, that i got myself/what i've been told. But it is good to refresh upon those conceprts and methods to proceed with my further studying, because i when i am given a task almost everytime i find it hard to came up with the right solution and fail to get the right approach to it. Thank you for the video. Subscribed!
@AFuller2020 Жыл бұрын
It's starts at 1:45, if you're in a hurry.
@ireonus Жыл бұрын
At around 11 mins another cool thing you could know mention is that if you provide a default variable that is mutable, say a = [], and say you modify the list to look like within the function to say a= [1,2,3], that default varraible is actually now a = [1,2,3] and could create problems if you call that function twice without giving the a argument
@HerrNilssonOmJagFarBe Жыл бұрын
Can you clarify what you mean with a code example? I thought you meant this, but the default value doesn't change in this case (luckily, that would have been disastrous...) >>> def f(x,a=[]): ... print(a) ... a=[3,4,5] ... print(a) ... pass ... >>> f(9) [] [3, 4, 5] >>> f(9) [] [3, 4, 5] How would you make the default value change?
@ireonus Жыл бұрын
@@HerrNilssonOmJagFarBe yes, here you setting the value with the statement, , a=[3, 4, 5],which as far as I know is now stored at a different place in the memory but try instead by having your default value as say a = [1] and then in the function append a value to the list, something like, def add_item(a = [1] ): a.append(2) print(a)
@HerrNilssonOmJagFarBe Жыл бұрын
@@ireonus >>> def f(x,a=[]): ... a.append(2) ... print(a) ... pass ... >>> f(1) [2] >>> f(1) [2, 2] >>> f(1) [2, 2, 2] Oh. Well, that's truly weird...! I also tried a recursing version of f() which made the issue even more spectacular. So 'a' is local to each particular invocation of the function, but the default value itself is the same across calls? What happens to the memory that's claimed by the default value once I've called the function too many times. There is no way to directly reference it outside the function. Can I ever reclaim it (short of redefining the function)?
@kmn1794 Жыл бұрын
f.__kwdefaults__['a'] I use this like f(x, *, _cache={}) but have not tested it across imports. Should probably fully understand the implications with good tests before using these for personal projects.
@HerrNilssonOmJagFarBe Жыл бұрын
@@kmn1794 Clever. It also made me understand the behaviour. Thanks! But such code seems obscure and abusive of that particular language quirk. How many would understand such code? I certainly wouldn't have until I saw this youtube vid.
@carl248810 ай бұрын
The explainer of mutable and immutable is really really clear, concise and useful...
@jennyudai4 ай бұрын
I initially rushed through learning of Python's fundamentals. Whilst these things were mentioned, I didn't grasp the concepts until this video. Thanks.
@messitheking15772 ай бұрын
Thank you, i have been using Python for 2-3 years but didn't know about mutable and immutable types..
@Raven-bi3xn Жыл бұрын
Great video! It might have been worth it to mention multiprocessing in Python as a way to overcome the multithreading limitation that you reviewed towards the end.
@yankluf10 ай бұрын
Fiiiiinally I understand those *args/**kwargs!!! Thank youuuuuuu!! 🎉🎉
@johnnytoobad7785 Жыл бұрын
Threading just takes advantage of GIL "idle time". (aka I/O wait-states) The Python "Multiprocessing" module allows you to run exclusive processes in multiple cores. (ie CPU-bound applications.). And (believe it or not) you CAN use threading inside an M/P function if it is coded properly. (according to the rules of MP functions and threads...)
@LMProduction Жыл бұрын
Yeah I was doing this on one of my projects and I'm surprised Tim didn't mention it in this video. Made it seem like you just can't do it at all.
@frostsmaker8966 Жыл бұрын
Mojo will solve multi-thread problems in Python. Do you need something fast and it is Python? Mojo is the answer for you.
@iphykelvin86983 ай бұрын
You are correct. Recently did that
@kineticraft69772 ай бұрын
Coming back to Python recently and it’s amazing how many of these quirks I have forgotten.
@ricdelmar4961 Жыл бұрын
The statement you made at about 3:30 was not correct. Writing y = x (where x is a tuple) does not create a copy of the object, as you can tell by looking at their ids -- they are identical. So, there is no difference between mutable and immutable objects in this respect. That line only creates a new variable that points to the same object as x did.
@i.a.m2413 Жыл бұрын
Exactly. Additionally, assignment to x just lets x point to another thing and doesn't modify what x pointed to. That whole part was conceptionally wrong.
@taragnor6 ай бұрын
Yeah, it seems like he never used a language with pointers, so he never understood the concept of a reference.
@triforgetech Жыл бұрын
Great refresher been diging into C++ some time your forget the basics concepts great job thanks
@NovaHorizon Жыл бұрын
I'm positive I've used multiprocess pools to get significant performance boosts while trying to do machine learning in the past..
@odarkeq Жыл бұрын
I recently ran across *args, **kwargs in some code I was stealing, uh borrowing, and it might as well have said *abra **kadabra because I didn't really get how it worked. You made me understand. Thanks.
@danuff6 ай бұрын
I am just learning Python and this video is VERY helpful. Thank you!
@ricardogomes95284 ай бұрын
The info in that 3:40 min is gold. Thank you
@acjazz01 Жыл бұрын
I'm not a Python developer, I'm an iOS developer (Swift, SwiftUI), I'd love to have people teaching new features on iOS with the same clear and concise speech as yours.
@MuhammetTaskin11 ай бұрын
Thank you so much. There is a lack of content on the internet about this. In addition to making things clear, it helped me in my programming midterm too.
@nigh_anxiety Жыл бұрын
At 3:30, your description of why changing x does not change y is incorrect and seemingly makes the same mistake many new Python developers make when working with mutable objects such as lists. The assignment `y = x` does NOT create a copy of the original tuple in y. y and x both point to the exact same tuple object in memory. This can be shown with either `y is x` (outputs true) or by printing `id(x)` and `id(y)` which will be identical. When you subsequently assign x to a new tuple, x now points at a new tuple object at different location in memory, which you can see by checking id(x) before and after that assignment. All variables in Python are references to objects. Doing an operation like `y = x`, for any type of object in x, simply makes y a reference to the same object, which is why mutable objects passed as arguments to a function can be mutated by that function. Likewise, anytime you assign a new value to a variable referencing an immutable object, you get a brand new object. if a = 1000, and then a += 1, you also change the id of a to a brand new object of For some more interesting examples, if you do something like `for i in range(-1000, 1001, 50): print(i, id(i))`, you'll see the id value change for each value, but in most implementations it alternate back and forth between a couple of ids as the interpreter reuses the memory block that was just freed by the garbage collector from the value in the previous loop. The exception is for ints in the range of -5 to 255 (at least in CPython) you'll get a different set of ids because those int objects are pre-allocated when interpreter starts and remain in memory to improve performance, as do the None object and True/False.
@aribalmarceljames9908 Жыл бұрын
Your'e True Legend for us as Python Developer! Thankyou
@pedrostrabeli4659 Жыл бұрын
I don't wanna be THAT GUY, but actually in the first part, when you do the tuple x = (1, 2) y = x x = (1, 2, 3) you're actually creating only one (1, 2) set in the memory. x points to that set, y points to the same set, so no hard copy. when you assign a new set to x in the third line, you create a new set in memory and only changes the memory address that x points to. The difference is that a set has no x[0] = n assignment (since it's immutable), then you're always reassigning.
@What_do_I_Think Жыл бұрын
Most important concept in programming: Boolean algebra. Many programmers do not get that right, but that is the basics of all computation.
@farzadshams32605 күн бұрын
This is really useful, thanks. I know a couple of these but having a video going into the details was great!
@sujanshah7442Ай бұрын
I have never understood Python this clearly as Tim teaches 🔥
@himanshu80442 ай бұрын
Really a *DIAMOND MINE* for coders (especially python coders). I scrolled through your channel and instantly knew this is the channel for python coding and subscribed it instantly. This video was very helpful. The way you explained things shows the amount of experience you have on python coding. Thank you very much ❤❤❤
@craigdawkins6943 Жыл бұрын
HI Tim, Just getting into coding: as you know (motivation level throught the roof - then realise html is not a stepping stone but a foundation of things to understand) Well Done on your coding journey! 😅🧐💫💫
@Kiran_Nath10 ай бұрын
Your solution for if __main-- = "__main__" legitimately saved my sanity. I was working on an asynchronous endpoint function and i was having difficulty closing the event loop until using that worked!
@earthslyrics Жыл бұрын
That actually was really good thank you very much I just finished a code where it was "downloading 10 files at a time which is 10 times faster"... Now I understand why it doesn't work so well :')
@yerneroneroipas8668 Жыл бұрын
This is a great video for someone who is learning python as a second, third, or nth language. These are very python specific implementations of universal concepts and I had been wondering about their purpose when seeing python code.
@nicjolas6 ай бұрын
Should I be worried learning these concepts if I'm thoroughly learning Python as my first language? What should I look out for since I plan to move on to C++?
@mrtecho3 ай бұрын
@@nicjolas I would suggest learning English as your first language... 💀
@nicjolas3 ай бұрын
@@mrtecho brUH
@ventures95607 ай бұрын
3:50 It's also an effect of the fact that the file is read from top to bottom. Line 2 get the evaluated before a line you were to swap lines 2 and 4 with 1 anothen X would equal (1, 2, 3).
@raghaventrarajaram Жыл бұрын
Multithreading is highly advantageous for tackling large problems. I suggest creating a video to elaborate on its benefits for our audience.
@AnantaAkash.Podder Жыл бұрын
Your *args, **kwargs explanation was amazing... Positional Argument & Keyword Argument... You made it very very Easy to Understand the Concept❤️❤️
@warloccarlos8522 Жыл бұрын
Please do create a short video(5 mins) on MultiProcessing for folks who are unaware of true parallelism in Python.
@linatroshka Жыл бұрын
Thanks for the video! Very consize and informative. The only thing that I would add about the GIL is that it because of it there are no performance advantages when it comes to so-call CPU-bound operations (like summation that was used as an example in the video). But when we are dealing with input/output-bound operations, such as sending a HTTP-request, then multithreading will improve performance, because instead of waiting for response before continuing executing code, we can use that waiting time to make more HTTP-requests. This can help handling multiple requests that are send to your web-applications, for example.
@shubhamjha5738 Жыл бұрын
Hey Lina, i also have a django function on which request lands, it was giving timeout error when 2users were hitting the same fn using url, then i increased the gunicorn worker and now it's working fine. So my qn is, was that a good idea or there is any other way to handle concurrent request on prod. Fyi that fn involve hitting different tables, and storing bulk data in one of tables using orm. So if you can comment over this about the best way to handle these things. Kindly share.
@sahilkumar-zp7zv Жыл бұрын
@@shubhamjha5738 Gunicorn is actually running your Django application on two different instances.
@alejovillores6373Ай бұрын
Very cool, finally I could understand what GIL was about
@basavarajus2061 Жыл бұрын
Thank you you are right on point, we miss these understandings and start scratching our head when we get errors.
@phpmaven Жыл бұрын
Just to nitpick, it's not pronounced e-mmu·ta·ble with a long E as in see. It's im·mu·ta·ble with an short I as pronounced in "into"
@danield.7359 Жыл бұрын
I didn't know about the "GIL". Your explanation gave me the answer to a question that I had parked for some time: why did concurrency not speed up a specific function that processed a very large list? I hope this will be fixed soon.
@firefly7076 Жыл бұрын
about 4:04 your explanation of changing values in mutable v. immutable types is really weird. You eventually get to saying that variables are actually just pointers to objects, and saying x=y is just reassigning the pointers to be the same (in other words, there is an object that y points towards, and executing x=y is setting the pointers to be the same) but you also say that immutable types do a clone instead which is... wrong. Here's some code that can be used to see that: x = (1, 2) y = x print(y is x) output: > True the two numbers should be the same, but should be different each time you run. The 'y is x' statement is comparing the ids of the numbers, aka where they are in the code. They're the same object. Some more code to show that 'is' is not just a fancy '==': x = (1, 2) y = tuple([1, 2]) print(y is x, y == x) ouput: > False, True Different objects.
@Elliria_home8 ай бұрын
This was simply phenomenal. Brilliantly done.
@colinmaharaj Жыл бұрын
3:50 So and C++, If I want extremely fast applications, I do not allocate memory all the time . I try to allocate my memory up front and any manipulation happens with that existing memory . The idea that you have to create another object to make an assignment is purely useful . Because I have infinite control over the memory and I know exactly what I am doing, I can write extremely fast and extremely stable code
@Imnotsoumyajit Жыл бұрын
Tim bro you never disappointed us ..This is straight up golden content...Really appreciate your work...Can we get more videos of you summarizing concepts in under 30mins once a month maybe ?
@israelanalista2041 Жыл бұрын
Hello, I am learning to program in programming expert, so far a great experience🎉
@성수-o6h Жыл бұрын
Hi, Tim. Learning lots of things from you! Many thanks from South Korea. Please make an entire GIL video!
@praetoriantribune Жыл бұрын
Concept 6 for Tim to get: Using type hints, so at 14:32 his IDE could smack him when he is passing a string argument to a bool parameter.
@Murzik_krot Жыл бұрын
This video has actually closed some gaps in my understanding of Python. It's truly a very cool and useful video, thank you
@birbirbirbir6133 Жыл бұрын
Example at 3:33 is not relative to mutable/immutable. If change to list, result will be the same. Attempt x[n] = m for tuple and list actually will explain all
@whiskerjones9662 Жыл бұрын
Great reminder for everyone Tim! Here's how I keep things sorted out for each of these topics: -Remember, folks, mutable objects in Python are like a box of chocolates, and you never know what you're gonna get! -List comprehensions are like magic spells. They may look strange initially, but they make things happen instantly once you know them! -Mastering function arguments is like learning a secret handshake; you can get in anywhere! -Using if __name__ == "__main__" is like having a secret identity for your Python code. -GIL is Python's bouncer - only one thread on the dance floor at a time!
@MarkButterworth13 Жыл бұрын
Your first example, assigning X to a tuple and then assigning Y to X, does NOT create a copy. It allocates the same tuple to Y. When you reassign X it creates a new tuple and leaves Y assigned to the original.
@upcomingbs Жыл бұрын
Crazy, I just started watching this video and the speech intonation is exactly the same for almost every computer/video game/programming video on KZbin. "We like..to talk..like thiiiiiis." Im going to finish watching it because I need something in the back ground
@machadinhos5 ай бұрын
In the first four minutes of the video, there is already a mistake. At 3:40, the video states that when you do x = (1, 2, 3) and then y = x, you are creating a copy of the object. This is incorrect. Instead, you are making y point to the same reference in memory as x. This applies to all types, whether mutable or immutable. You can prove this by simply doing x is y (which will return True) or checking id(x) and id(y) (both will have the same value). This invalidates the whole narrative of mutable vs immutable in the first 6 minutes of the video. This happens in most programming languages (if not all), not just in python.
@VargasFamTravels4 ай бұрын
Man this video was phenomenal ! Thank you. I'm taking CS50P and its intense lol
@wiwhwiwh12417 ай бұрын
So you seem to be confused about the way immutable objects are stored in memory. 03:37 when you write y=x it doesn't make a copy or x. Instead, both x and y point to the same tuple in memory. When you then assign something else to x, y still points to the same tuple and python's garbage collection will not free the memory. In fact one of the reasons to have immutable object is precisely to avoid having to make physcal copies of it in memory. Hope this helps
@BALASTE Жыл бұрын
thanks for the vid Tim
@user-sj9xq6hb9p Жыл бұрын
Multi threading is beneficial when your python program pauses or waits for the user to input something till then the GIL can be passed to another function and it can run that while its waiting for the user to provide the input
@vlabogd Жыл бұрын
Thanks for clarifying *args and **kwargs!
@FATIMAELAMRANI-k5e2 ай бұрын
great video finally getting to catch some of the basics through a project based tutorial wish is a great way to know how to implement the basics thank you for your effort
@yah5o9 ай бұрын
Now I'm so looking forward to running into my first mutable/immutable issues....
@heco. Жыл бұрын
the only thing i didn't know that you could put * before list or dic as arguments so i guess it was helpful
@raymondgrant20158 ай бұрын
Thank you for this video! Very clear overview of important concepts in Python
@luizh.1886 ай бұрын
🎯 Key Takeaways for quick navigation: 🧱 Understanding mutable vs. immutable types in Python is crucial for avoiding common mistakes. 🔀 Immutable types like strings, integers, floats, booleans, bytes, and tuples cannot be changed once defined. 🔄 Mutable types like lists, sets, and dictionaries can be modified after being defined. 🔄 Assigning a mutable object to another variable creates a reference to the same object, affecting both variables if modified. 📝 List comprehension simplifies code by creating lists in a concise manner using for loops. 📋 Python supports various argument and parameter types, including positional, keyword, optional, and special types like *args and kwargs. ⚙️ The "if __name__ == '__main__':" statement allows code to be executed only when the Python file is run directly. ⚙️ Python's Global Interpreter Lock (GIL) restricts simultaneous execution of multiple threads, affecting multi-threaded performance. Made with HARPA AI
@andrechen84611 ай бұрын
in your example at 5:00 What is the point of putting return numbers [-n:]? Not sure why you would make it negative. Also why not just return numbers? and also why did you put a 2 there? As far as I can see the two isnt actually doing anything. I wrote my own version of your example and it sems to be doing the exact same thing but a lot simpler: def getlargest(nums): nums.sort() return(nums) nums = [1,23,4,5] print(nums) print(getlargest(nums))
@pwinowski Жыл бұрын
I'm not a Pythonist, so maybe I'm messing with some insiders language, however, the first section "Mutable vs Immutable' seems to me like it's dealing rather with "reference type vs value type" problem. At least in other languages, reference types can be either mutable or immutable, which refers to if the object's value can change or not. But the phenomena shown in this example, with assigning a list to another variable, or passing it to a function and then observing both variables change when either of them is modified, this is (as the author stated) because both variables are references to the same object. Wether that object is mutable itself, that is yet another topic.
@ristolahdelma25913 ай бұрын
I don’t think you understand the first concept. Statement at 3:43 is wrong. Assigning immutable value of x to y on lime 2 does not make a copy, but makes y refer to same object as x. But on line 4 a new immutable object is created and x now refers to that while y still refers to the old object.
@thrasosthrasos7353 Жыл бұрын
It is not true that a copy is made when assignment is done to immutable type - moreover copy should not be made (in order to keep memory consumption as low as possible). Next code shows it: a = (1, 2, 3) b = a c = (1, 2, 3) print(f'4: {b is a}') print(f'5: {c is a}') d = [1, 2, 3] e = d f = [1, 2, 3] print(f'6: {e is d}') print(f'7: {f is d}') Output is: 4: True 5: True 6: True 7: False 4 and 6 returns True which means those are the same objects. The difference is when assignment is done to new object which has is equal to existing one (not the same). Output of 5 and 7 is not the same because it is possible to reuse existing immutable object so tuples a i c are equal and the same object, but lists d and f are equal but not the same object.
@andreibaditoiu Жыл бұрын
Great explanation style, thanks for your work!
@LudovicCarceles Жыл бұрын
Thanks, I wasn't sure about the second to last and never heard of the GIL.
@pyMarek Жыл бұрын
3:49 y is not copy of x. After `x=y` , y and x represent name of the same variable, then in line `x=(1,2,3)` you create new variable and named is x. `x=y` never create copy.
@skuzmo Жыл бұрын
Yes. I think Tim misspoke a bit in this section on mutability vs immutability. Setting “y = x” creates a second reference to the tuple which was originally referenced as “x”. This assignment has nothing to do with a “magical” copy of an immutable object. This can be confirmed with “x is y” and “y is x”; both are “True”. Equality checks (with “==“) are also “True”. Subsequent setting of “x = (1, 2, 3)” creates a new 3-element tuple referenced as “x” while “y” becomes the only reference to the original 2-element tuple. I think it might even help to add a 6th must-know concept - “references vs. copies”.
@Shop.smart_953 ай бұрын
Your video is amazing and your explanation is very good some day i am find a bast python programer to teach python so at the end you are bast for my opinion ❤
@jesprotech Жыл бұрын
Thank you for the video! I find it very interesting how you show how to work with Python.
@anitasunildesai Жыл бұрын
Thanks a lot for this tutorial as improved my understanding a lot. Request to kindly upload more of these beneficial vedios. 🙏🏼🙏🏼
@nargileh1 Жыл бұрын
You can avoid sharing a reference for mutable types by assigning like this: y = list(x) This forces python to create a new list instead of sharing a reference to the same object.
@nickstanovic2 ай бұрын
List comprehensions can be abused by one-line superheroes because you can do a lot with that one line but eventually it will be harder to debug because you may have to read it multiple times and mentally break it down but the whole point of Python is that you are trading off performance in favor of quick prototyping and readability and if you cared about performance so much you would just use C because it's a faster language and a sentence is shorter than a couple paragraphs.
@CerealMalt Жыл бұрын
Really helpful video for pythonistas trying to become better
@MariusDiMaio11 ай бұрын
you are a good teacher, but i ask you a question: why you don't use Eclipse, instead of VSCode? Or Pycharm, for example
@debakarr Жыл бұрын
I hope you start adding advanced concepts instead of beginner ones in the future.
@Iain26510 ай бұрын
About 5 minutes in... When you called your function Get largest numbers you passed it into a variable called largest? Then you printed a different variable called nums. I would have assumed you would have printed: print(largest) Could you clarify what the variable largest was for if it was not used. Thanks
@csharpification Жыл бұрын
The GIL (or Global Interpreter Lock) is only taken when running pure Python code, and in many cases is completely released and not even checked. For IO operations it is not taken into account. Also, Numpy library is not limited by GIL.
@kirkwolak6735 Жыл бұрын
This is a great video. The __main__ comment should also be a place for "module tests" so it is exercising the module, but you would NOT want that if you did not run the file directly. (Because IMPORT effectively executes the file, because it interprets it). Did I miss how to use deepcopy? (for the mutable objects) import copy original_list = [1, 2, [3, 4]] cloned_list = copy.deepcopy(original_list)
@chaosopher23 Жыл бұрын
Star Args & friends work as star args, but splat args is what * looks more like: a dead bug. But that's from when computers used to have one 8-bit processor that ran at 4 MHz, if you had a good one. And the screen had one color. And we *LIKED* it!
@SciHeartJourney Жыл бұрын
I think of threading as being like juggling. You only have one set of hands, but you can have as many balls in the air as you can handle.
@ZooDinghy6 ай бұрын
Thanks for the video! Python can be quite confusing.
@velvetjones86348 ай бұрын
I need a remedial version of this. One that defines the various terms that I’m not familiar with.
@vedantkulkarni5437 Жыл бұрын
Pure content....this is so basic but most of us never pay attention to this....and boom interviewer asks this 😵💫
@xaviervillalobos39589 ай бұрын
Thank you, I really appreciate this video. It has been really helpful to me on my Python learning journey. :)
@AndrejPodzimek Жыл бұрын
3:33 That tuple vs. list example is a flawed false analogy. If you take the tuple example and replace () with [] (without modifying anything else in the example), you will get the exact same outcome: y holds a reference to what was formerly referenced by x (until x was set to a new reference). Example: x = [1, 2]; y = x; x = [1, 2, 3]; print(x, y) And nope, the tuple did *not* get (deep) copied. Example: x = (1, 2); y = x; x is y ← This returns True. No deep copies involved.