6 of Python's Newest and Best Features (3.7-3.9)

James Briggs · Advanced ·🧠 Large Language Models ·5y ago

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 Stoic Philosophy Text Generation with TensorFlow
Stoic Philosophy Text Generation with TensorFlow
James Briggs
2 How to Build TensorFlow Pipelines with tf.data.Dataset
How to Build TensorFlow Pipelines with tf.data.Dataset
James Briggs
3 Every New Feature in Python 3.10.0a2
Every New Feature in Python 3.10.0a2
James Briggs
4 How-to Build a Transformer for Language Classification in TensorFlow
How-to Build a Transformer for Language Classification in TensorFlow
James Briggs
5 How-to use the Kaggle API in Python
How-to use the Kaggle API in Python
James Briggs
6 Language Generation with OpenAI's GPT-2 in Python
Language Generation with OpenAI's GPT-2 in Python
James Briggs
7 Text Summarization with Google AI's T5 in Python
Text Summarization with Google AI's T5 in Python
James Briggs
8 How-to do Sentiment Analysis with Flair in Python
How-to do Sentiment Analysis with Flair in Python
James Briggs
9 Python Environment Setup for Machine Learning
Python Environment Setup for Machine Learning
James Briggs
10 Sequential Model - TensorFlow Essentials #1
Sequential Model - TensorFlow Essentials #1
James Briggs
11 Functional API - TensorFlow Essentials #2
Functional API - TensorFlow Essentials #2
James Briggs
12 Training Parameters - TensorFlow Essentials #3
Training Parameters - TensorFlow Essentials #3
James Briggs
13 Input Data Pipelines - TensorFlow Essentials #4
Input Data Pipelines - TensorFlow Essentials #4
James Briggs
6 of Python's Newest and Best Features (3.7-3.9)
6 of Python's Newest and Best Features (3.7-3.9)
James Briggs
15 Novice to Advanced RegEx in Less-than 30 Minutes + Python
Novice to Advanced RegEx in Less-than 30 Minutes + Python
James Briggs
16 Building a PlotLy $GME Chart in Python
Building a PlotLy $GME Chart in Python
James Briggs
17 How-to Use The Reddit API in Python
How-to Use The Reddit API in Python
James Briggs
18 How to Build Custom Q&A Transformer Models in Python
How to Build Custom Q&A Transformer Models in Python
James Briggs
19 How to Build Q&A Models in Python (Transformers)
How to Build Q&A Models in Python (Transformers)
James Briggs
20 How-to Decode Outputs From NLP Models (Python)
How-to Decode Outputs From NLP Models (Python)
James Briggs
21 Identify Stocks on Reddit with SpaCy (NER in Python)
Identify Stocks on Reddit with SpaCy (NER in Python)
James Briggs
22 Sentiment Analysis on ANY Length of Text With Transformers (Python)
Sentiment Analysis on ANY Length of Text With Transformers (Python)
James Briggs
23 Unicode Normalization for NLP in Python
Unicode Normalization for NLP in Python
James Briggs
24 The NEW Match-Case Statement in Python 3.10
The NEW Match-Case Statement in Python 3.10
James Briggs
25 Multi-Class Language Classification With BERT in TensorFlow
Multi-Class Language Classification With BERT in TensorFlow
James Briggs
26 How to Build Python Packages for Pip
How to Build Python Packages for Pip
James Briggs
27 How-to Structure a Q&A ML App
How-to Structure a Q&A ML App
James Briggs
28 How to Index Q&A Data With Haystack and Elasticsearch
How to Index Q&A Data With Haystack and Elasticsearch
James Briggs
29 Q&A Document Retrieval With DPR
Q&A Document Retrieval With DPR
James Briggs
30 How to Use Type Annotations in Python
How to Use Type Annotations in Python
James Briggs
31 Extractive Q&A With Haystack and FastAPI in Python
Extractive Q&A With Haystack and FastAPI in Python
James Briggs
32 Sentence Similarity With Sentence-Transformers in Python
Sentence Similarity With Sentence-Transformers in Python
James Briggs
33 Sentence Similarity With Transformers and PyTorch (Python)
Sentence Similarity With Transformers and PyTorch (Python)
James Briggs
34 NER With Transformers and spaCy (Python)
NER With Transformers and spaCy (Python)
James Briggs
35 Training BERT #1 - Masked-Language Modeling (MLM)
Training BERT #1 - Masked-Language Modeling (MLM)
James Briggs
36 Training BERT #2 - Train With Masked-Language Modeling (MLM)
Training BERT #2 - Train With Masked-Language Modeling (MLM)
James Briggs
37 Training BERT #3 - Next Sentence Prediction (NSP)
Training BERT #3 - Next Sentence Prediction (NSP)
James Briggs
38 Training BERT #4 - Train With Next Sentence Prediction (NSP)
Training BERT #4 - Train With Next Sentence Prediction (NSP)
James Briggs
39 FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
FREE 11 Hour NLP Transformers Course (Next 3 Days Only)
James Briggs
40 New Features in Python 3.10
New Features in Python 3.10
James Briggs
41 Training BERT #5 - Training With BertForPretraining
Training BERT #5 - Training With BertForPretraining
James Briggs
42 How-to Use HuggingFace's Datasets - Transformers From Scratch #1
How-to Use HuggingFace's Datasets - Transformers From Scratch #1
James Briggs
43 Build a Custom Transformer Tokenizer - Transformers From Scratch #2
Build a Custom Transformer Tokenizer - Transformers From Scratch #2
James Briggs
44 3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
3 Traditional Methods for Similarity Search (Jaccard, w-shingling, Levenshtein)
James Briggs
45 3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
3 Vector-based Methods for Similarity Search (TF-IDF, BM25, SBERT)
James Briggs
46 Building MLM Training Input Pipeline - Transformers From Scratch #3
Building MLM Training Input Pipeline - Transformers From Scratch #3
James Briggs
47 Training and Testing an Italian BERT - Transformers From Scratch #4
Training and Testing an Italian BERT - Transformers From Scratch #4
James Briggs
48 Faiss - Introduction to Similarity Search
Faiss - Introduction to Similarity Search
James Briggs
49 Angular App Setup With Material - Stoic Q&A #5
Angular App Setup With Material - Stoic Q&A #5
James Briggs
50 Why are there so many Tokenization methods in HF Transformers?
Why are there so many Tokenization methods in HF Transformers?
James Briggs
51 Choosing Indexes for Similarity Search (Faiss in Python)
Choosing Indexes for Similarity Search (Faiss in Python)
James Briggs
52 Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
Locality Sensitive Hashing (LSH) for Search with Shingling + MinHashing (Python)
James Briggs
53 How LSH Random Projection works in search (+Python)
How LSH Random Projection works in search (+Python)
James Briggs
54 IndexLSH for Fast Similarity Search in Faiss
IndexLSH for Fast Similarity Search in Faiss
James Briggs
55 Faiss - Vector Compression with PQ and IVFPQ (in Python)
Faiss - Vector Compression with PQ and IVFPQ (in Python)
James Briggs
56 Product Quantization for Vector Similarity Search (+ Python)
Product Quantization for Vector Similarity Search (+ Python)
James Briggs
57 How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
How to Build a Bert WordPiece Tokenizer in Python and HuggingFace
James Briggs
58 Metadata Filtering for Vector Search + Latest Filter Tech
Metadata Filtering for Vector Search + Latest Filter Tech
James Briggs
59 Build NLP Pipelines with HuggingFace Datasets
Build NLP Pipelines with HuggingFace Datasets
James Briggs
60 Composite Indexes and the Faiss Index Factory
Composite Indexes and the Faiss Index Factory
James Briggs

The video teaches viewers about the newest features in Python versions 3.7-3.9, including debugging tools, compact coding techniques, and type hinting. These features can be used to improve code readability, simplify variable assignments, and restrict function parameter types.

Key Takeaways
  1. Use the breakpoint function to add a breakpoint in code
  2. Use the walrus operator to assign a value to a variable on the fly
  3. Use the walrus operator to write more compact code by including variable assignments within conditional statements
  4. Use the f-string equals sign specifier to print variable names and values
  5. Specify input parameters to functions as positional only using the slash notation
  6. Add type annotations to function parameters to restrict their types
  7. Use the dictionary union operator (merge) to combine two dictionaries
  8. Use the dictionary union operator (update) to update a dictionary in place
💡 The walrus operator can be used to write more compact code by including variable assignments within conditional statements, making code more readable and efficient.

Related Reads

📰
Open WebUI: Installation, Features, Errors & Complete Beginner Guide (2026)
Learn to install and use Open WebUI with Docker for a seamless LLM experience
Medium · LLM
📰
Pre-training vs Fine-Tuning: How AI Learns Before It Learns You — Part 25
Learn the difference between pre-training and fine-tuning in AI and how they enable models like ChatGPT to learn and answer questions effectively
Medium · AI
📰
Pre-training vs Fine-Tuning: How AI Learns Before It Learns You — Part 25
Learn how AI models like GPT and BERT learn through pre-training and fine-tuning, and why this matters for their ability to answer specific questions
Medium · Machine Learning
📰
Washington Blinked: 18 Days That Bent The U.S.-China AI Race
The U.S.-China AI race is influenced by open source and closed models as competing strategic narratives, impacting global AI dominance
Forbes Innovation
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →