6 of Python's Newest and Best Features (3.7-3.9)
Skills:
LLM Foundations85%
Key Takeaways
The video covers Python's newest features from versions 3.7 to 3.9, including the breakpoint function, walrus operator, f-string equals sign specifier, positional only parameters, type hinting, and dictionary union operators.
Full Transcript
hi and welcome to this video we're going to go through and take a look at some of the coolest more interesting features i've been adding to python in the past few years so we'll just go straight into it and code a few examples as well okay so the first one and the only example we're going to look at from python 3.7 is the addition of break points so obviously a breakpoint is where we typically click on the left side of our editor to add in the little red circle which means we want the code to break at this point what the breakpoint function allows us to do is actually use python debuggers within our code so the function uses the bdb debugger by default but we can also use other alternatives as well so for example if we had a list of functions here a list of print statements okay so they just print out hello if we for whatever reason wants to put in a breakpoint to try and figure out what was going wrong safest code was probably a little more complicated we could add in this okay and then we go straight into the python bdb debugger at the bottom here and this is essentially like a interactive shell where we can just write python and figure out what is going on so obviously there's not really much for us to figure out in this case but when you are actually writing more complex functions or classes or modules in your code this can be really really useful so here we can print or do anything it's simply an interactive python debugger window so the second of our new features actually comes from python 3.8 now this is the walrus operator which was the go-to for the majority of 2019 for anyone covering what is new in python 3.8 so it's a new operator added to python it's genuinely very useful it essentially allows us to assign a value to a variable on the fly so it looks like this which looks like a walrus hence the name and we can use it to write more compact code so say we have list here and we want to assign the length of this list to a variable which we'll call t len and then we want to write an if statement checking if t len is greater than three and if so we want to print out the length tlen and a little statement saying that this is greater than three that's great with the wars operator we can actually make this more compact and include our variable assignment within the if statement so we put this within a bracket and then we change our equal sign here to a walrus operator and we can run again and now we have four is greater than three so then we've shortened our code made it more compact obviously you know whether this is useful and more readable is completely up to who is writing the code nonetheless it's a very useful operator to understand and use so a more realistic use case for this could be if we are writing a regit statement so i'll just write out a sentence here quickly okay so we want this sentence to go through and use regex to find a currency followed by a few numbers so i'll just switch this to dollars here so we're going to go through each of these sentences and only print out the ones that contain a currency you also need to import re for rejects now for those of you that use regex a fair bit before you know that when we use a regis search it will return either none or return a set of groups which contain our matches and typically what we'd have to do is we'd have to write amount equals re.search and then we would have our regex in here so here i'm just going to do a lookup so this is just checking for a dollar symbol before a set of digits and this will return just digits not the dollar symbol okay and usually we would have to do this and then we would have to write if amount or just if amount is true and then print out the amount and the group which would be zero and then we would get our results this would be sentences okay so we get 100 but what we can do instead is use our walrus operator right if amount and now shortened our code so that's a more probably a more useful example of using the waris operator another really cool feature that was added in 3.8 is the f string equals sign specifier so i'll give a quick example so we have these two variables it's just two numbers and maybe we are trying to figure out maybe we're doing something wrong and we want to see where our values are going and what variables are being assigned to and what we might typically do is this we might write x equals x and y equals y okay and this is probably what a lot of us would do but rather than doing this we can actually just write x equals like this and this will print out the same thing so we showed the variable name and the value within it so it's a smaller but i think very useful addition to python okay so the final feature from python 3.8 is the inclusion of syntax to specify the input parameters to functions that cannot be called by name but instead by the position so what i mean by that is if we have this function here we can either write this which is using the position of the parameters in order to fill in those variables or we can also do this and we'll get the exact same output now this is great but when you're writing code that other people are using and relying on and maybe that code isn't completely finished yet you may find that you i'm not entirely sure if you'll keep this variable or input parameter as op or this is x or this is y you might change these around and and if people are using your code like this that will cause problems when you do change your code to use different variable names so maybe at a later point you decide you would change your code to use method instead of op if we took that and then tried to use this function again we would get this type error obviously we don't want that so what we can do instead is define method as a positional only parameter and we do that by adding this slash so now if we try to run this but with operator again we see okay we have a type error count got some positional only arguments passes keyword arguments so though we haven't changed operator to method yet we are preventing the user from using up here so that forces the user to use this instead now the user can still define these using their names that's completely fine but they cannot use anything that comes before this slash so if we do rewrite our function to use method instead of op and the user runs that code that they've been using before we won't get any errors because we prevented the user from being able to use this keyword parameter okay so that's everything for python 3.8 and we're going to move on to python 3.9 now the main one of those is the addition of more type hinting so type hinting is something that has been around for a while and it seems to be a recurring theme with every python update that we will get some new type hinting functionality for python 3.9 they've essentially merged all the different type hinting features that they've been adding over time and brought them into the core python functionality so there's no more importing typing modules or annotation modules just works as it is for the most part so for those of you that don't know what typing is in python say we have this function here it's quite clear that this function add-ins shouldn't be used with strings okay but it can be used because we're just using a plus here so it's design essentially telling python to concatenate these two strings but our intention is actually to only allow integers so what we can do is add a type annotation or type hint and that's what this is here so we add this column followed by the data type that we intend this value to be then we return plus vowel now when you are writing this code out in your editor and you have your python linter so pylint for example it will come up with an error if you write something like this later on and this isn't an error that will stop your code we can still run it it is just a hint it is a warning okay you can't sit here because we're in jupiter but in a normal lecture with pylin sold you will see this bit here get highlighted with a note saying we expected type int got string instead okay so that's the warning that will pop up now this is just very useful for complex code bases where you're not entirely sure what values are supposed to be going in and out of different functions and classes just makes things a lot easier to follow another syntax edition in terms of the type hinting that was added is the ability to define the type that should be coming out of a function not just into a function say here we want to sum up all the values within a dictionary we want to input a dictionary and what we would expect to get out of this which is defined by this little arrow syntax here is an integer and then we just return the sum of the values within our dictionary and then we define our dictionary here and we can also add the type here as well and then we call our function and we should get a integer out of that as expected now if we change this and these were the other way around and we had strings instead of numbers here we would obviously get a full blown error but before we even run that we would see a type in come up saying okay we are expecting a integer here not a string so it can be very useful in just in terms of readability it can depending on how you do this be very very useful if you look into complex libraries like you have the tensorflow library or transformers library those libraries with type pins can make things a lot easier in fact some of the more recently written code in the transformers library has been using type hinting and it makes it so much easier to read so it's a very good addition to python in my opinion now something that is very useful if we take this function again we can actually use more complex types so we know that we want a dictionary but maybe we want to define what is within that dictionary as well so we can actually do that like this so now we know that our dictionary should be built of string keys and integer values and this is just an extra layer of detail so we can be more specific there we go now the final feature in python 3.9 that we'll go over is dictionary unions operators there's quite an interesting addition to the syntax and it gives us two new operators one is called the merge operator and the other is the update operator so the merge operator looks like this and we use it to merge two dictionaries so let's write out two dictionaries quickly okay so we have these two dictionaries and we can merge them using our merge operator okay and now we can see that those two dictionaries are now one within our new variable c the update operator is slightly different in that it allows us to do the merge in place so what that means is rather than adding this to a new variable c we actually assign it to the variable on the left of the statement and this here is the update operator now if we print a we see that we get the same function okay so that's it for this video covering some of the biggest changes and upgrades to python that we actually see and use on a daily basis but that's it for this video i hope you enjoyed and as always thank you for watching bye
Original Description
A rundown of the six most recent, and coolest features added to Python in the past few years!
2018 brought us a plethora of new features with the release of Python 3.7, followed by 3.8 in 2019, and 3.9 in 2020.
Many of those changes were behind the scenes. Optimizations and upgrades that the vast majority of us will never notice, despite their benefits.
Others are more obvious, additions to syntax or functionality that can change how we write our code. But even these visible changes can be hard to keep up with.
In this video, we will run through the more apparent upgrades to provide a brief but hopefully invaluable refresher on everything new to Python from the past few years.
- Python 3.7
- Breakpoints
- Python 3.8
- Walrus Operator
- F-string '=' Specifier
- Positional-only Parameters
- Python 3.9
- More Type Hinting
- Dictionary Unions
Medium Article:
https://towardsdatascience.com/amazing-features-added-to-python-from-3-7-to-now-4f35f0bb1ea6
(Free access link):
https://towardsdatascience.com/amazing-features-added-to-python-from-3-7-to-now-4f35f0bb1ea6?sk=bda3cb7717caa969b81619f85191f241
Thumbnail background by Martin Sanchez on Unsplash:
https://unsplash.com/photos/4PDPLw1flgE
🤖 70% Discount on the NLP With Transformers in Python course:
https://bit.ly/3DFvvY5
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from James Briggs · James Briggs · 14 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
▶
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
Stoic Philosophy Text Generation with TensorFlow
James Briggs
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
Every New Feature in Python 3.10.0a2
James Briggs
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
How-to use the Kaggle API in Python
James Briggs
Language Generation with OpenAI's GPT-2 in Python
James Briggs
Text Summarization with Google AI's T5 in Python
James Briggs
How-to do Sentiment Analysis with Flair in Python
James Briggs
Python Environment Setup for Machine Learning
James Briggs
Sequential Model - TensorFlow Essentials #1
James Briggs
Functional API - TensorFlow Essentials #2
James Briggs
Training Parameters - TensorFlow Essentials #3
James Briggs
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
Building a PlotLy $GME Chart in Python
James Briggs
How-to Use The Reddit API in Python
James Briggs
How to Build Custom Q&A Transformer Models in Python
James Briggs
How to Build Q&A Models in Python (Transformers)
James Briggs
How-to Decode Outputs From NLP Models (Python)
James Briggs
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
Unicode Normalization for NLP in Python
James Briggs
The NEW Match-Case Statement in Python 3.10
James Briggs
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
How to Build Python Packages for Pip
James Briggs
How-to Structure a Q&A ML App
James Briggs
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
Q&A Document Retrieval With DPR
James Briggs
How to Use Type Annotations in Python
James Briggs
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
Sentence Similarity With Sentence-Transformers in Python
James Briggs
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
NER With Transformers and spaCy (Python)
James Briggs
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
New Features in Python 3.10
James Briggs
Training BERT #5 - Training With BertForPretraining
James Briggs
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
Faiss - Introduction to Similarity Search
James Briggs
Angular App Setup With Material - Stoic Q&A #5
James Briggs
Why are there so many Tokenization methods in HF Transformers?
James Briggs
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
How LSH Random Projection works in search (+Python)
James Briggs
IndexLSH for Fast Similarity Search in Faiss
James Briggs
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
Build NLP Pipelines with HuggingFace Datasets
James Briggs
Composite Indexes and the Faiss Index Factory
James Briggs
More on: LLM Foundations
View skill →
🎓
Tutor Explanation
DeepCamp AI