Make Your Python Code More Professional

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

Key Takeaways

The video covers three ways to make Python code more professional, including type hinting, docstrings, and declarative programming, as demonstrated by NeuralNine.

Full Transcript

it's not a game it's a [Music] red what is going on guys welcome back and today's video we're going to learn how to make our python code more professional so let us get right into it all right so we're going to cover three points that are going to make your python code more professional and the first one is going to be type hinting now let's say we have a basic function which we call some calculation and we have some values X and Y and then a return value which is x + y^ 2 for example so this is a simple python function and of course python is dynamically typed so technically speaking I can pass whatever I want I can say 10 and 20 I can pass uh I don't know 10.78 and 8.91 and of course since we have Dynamic typing I can also pass some strings so I can pass hello and World which doesn't mean that I'm going to get a uh meaningful result I'm going to get an error in fact uh if I run this because you're going to see unsupported operant types for string and int um the previous calls however work we just need to print the result um so this this is dynamic typing in Python I don't get any errors before I actually get to the line of code that say Hey you cannot do this here because it's not allowed because python does does not do any static uh typee checking I don't say okay this is an integer so I cannot pass a string here this is something that works in Java or is done in Java in C in C++ but it's not done in Python now typ hinting is not going to change the fact that python is dynamically typed but typ hinting is a little bit more uh for documentational purposes so let's say I'm a developer and this is the code of someone else and I look at this function now of course in this case I would immediately know that this is meant to be applied on numerical values but maybe I don't know that maybe the function is more complicated uh or maybe they are there are some Alternatives and maybe this function is not for all numerical values but only for integers so uh if I want to specify for other developers that the parameters for example have to be integers what I do is I use a colon not here I use a colon after the parameter and say int and I do it here as well int and if I want to specify that the return value of the function is also going to be an INT then I go after the function definition function signature and I add this Arrow here and say int as well so this basically says X is supposed to be an integer Y is supposed to be an integer and the result of this function is supposed to be an integer now I can still go ahead and pass a string hello world or two strings this is still going going to work I'm not going to get the error that I cannot run this script it's just going to say okay it doesn't work because it goes to that line executes it and says it doesn't work I think python is going to notice that as you can see here p uh py charm not python py charm is going to notice that the expected type was int but we got a string instead uh as you can see but this is not an error this is just a message that py charm tells me uh I can still run that code so if I for example go with not a string but with 19.28% and with um I don't know 77.2 three I'm still going to get that error right because you can see okay expected int but got float instead however this would work since those are numerical values so I would get if I print it of course I would get the result it's not a problem uh but still pie charm is going to tell me that this is not what was expected here okay so type hinting is mainly used for documentational purposes so when I use a library I want to know okay what parameter is expected here it's just better for predictability and there are also tools like my piie written like that my piie that allow you to check for the Integrity or for the consistency of the typing so for example if I have something like some calculation hello world this works from a python perspective yes pyrum is going to tell me that but it still works from a python perspective if I say mypy this file it's going to say no we have an error here because we have strings where we would expect integers so with mypi we can check for consistency I have a video on this channel already about typ hinting where I go a little bit into more detail so you can check that out it's uh part of the advanced Python tutorial series uh but one more thing that I want to show you here is uh what to do if you want to allow for multiple types so let's say we don't want to allow for integers or floats but we want to allow for integers or float so one of the two we want to allow for or maybe for integers and float so basically we allow both of them and if we want to do that we need to import something from the typing module so we're going to say from typing import Union this is the thing that we need to use here and if I want to say okay X is an integer or a floating Point number I can say Okay X is just a union of int and Float like that and this is also a union of int and float and the return value is also a union of int and float and I'm sure my camera is blocking that right now right so let me just put this down here as you can see so uh we have this type hinting here now this is going to be replaced in Python 3.10 you can watch my video what's new in Python 3.10 because in Python 3.10 we're going to have uh the union operator so in Python 3.10 this is not python 3.10 yet we're working with 3.9 so it's not going to work here but in Python 3.10 we will be able to say x int and then this Union operator uh float and it's going to have the same effect and we don't need to import anything but typ hinting is something that you might consider adding to your code to just make it more readable more predictable uh and uh just better overall all right so the second thing that you can do in order to make your python code more professional is to use doc string so again about documentation here uh but this time about real documentation you want to document your file you want to document your classes your functions and so on now of course there are different ways to do that people use different um I don't know patterns or structures different keywords sometimes uh but in general you might want to start by just documenting the the file itself for example if you're publishing a package or uh a a significant sized project significantly sized project on GitHub for example you might want to start with a basic doc string uh which is is uh started with three three quotation marks here so a multi-line string essentially and you start with something like this is my new library for example and then you can specify some information like uh author equals I don't know neurom 9 and then YouTube if you have one and then email if you have one and so on some information about you if you want to have some contact information up there uh and then you can also do something uh which is quite interesting you can set some uh hidden variables here which are author for example so underscore uncore author underscore uncore and you can set this to a string neural 9 for example and you can do the same thing with email um and you know mail at mailmail for example and then you can do the same thing with uh the status of the project so you can say for example it is uh currently still in the planning phase or whatever um and then you can start to document the individual parts of your code for example classes functions and so on so let's say we have a class person for example and this class person has uh let's say the NIT method here the init Constructor we have name age weight as always and then we have self. name equals come on name and self. H equals H and self. weight equals weight there you go um and now what we can do here if we don't have or let's add one method here let's say we have get older for example and we pass years and then we say self. h equals uh H+ or actually self. h plus equals years and then maybe return that H as well just so we get get the information um and here we can now do something like a multi-line comment and we say okay this is the person class for the project whatever uh and then we can do something like attributes and underline this like that and then we can add some private attributes here and we don't have any private attributes here we only have public attributes but for example if something starts with underscore underscore we would do something like name equals or not equals we we would say something like name then the data type for example uh Str Str and then we would describe what it is so this is a name and so on we will do the same thing for public here so I don't want to go into too much detail here you can take a look at vidstream I'm going to show you in a second how I did it there uh we can do the same thing with methods of course so we say methods underscore uncore underscore uh and or actually you can go with D- Das D Dash uh and then private public again the same thing and the function uh the functions itself are probably the more uh the most important thing that you want to document because there you have some keywords also pycharm has some keywords I think it's also going to autocomplete them so if I start with three quotation marks here it already autocompletes with uh param so we have par for parameter of course I can say a Constructor for person and then I have param name and I can say okay name the name of the person right so I can do the same thing here for age the age of the person of the person and so on um but usually you do this for functions where it's not so clear what we're doing obviously if we have a Constructor and name H weight we know what that is uh but here for example you know we could start with uh param years the or we can say how many years the person is getting older I don't even know if that's grammatically correct to be honest um get older function and so on and then we also have the keyword return which basically says okay what is this uh return value what are we going to get as a result the uh new H okay so that is how it works um and that is just good to have that in your code especially for functions now you don't need to necessarily do all of this if you don't like to uh but especially for the functions it's useful to have a basic description uh to have basic parameter information and return values and all that uh because I also think that if I go and create a person now um a person object let's say I add let me just add nonone none and none I'm not sure if I'm not going to be able to see some Doc strings here there you go so you can see I can I can see the doc strings in the preview in py charm I can see params name the age of the person age the age of the person Wait nothing because we didn't specify anything uh so this is quite useful also if I go with p1. get older I should be able [Music] to if I pass 12 for example I should be able to get some basic information by hovering here you can see params return value so it's also formatted by py charm if you use the proper format and in general it's just more professional to do it like that so you can go to neuron not neural not neuron 9.com to github.com neural 9 uh vstream and you can check out the streaming. py file there you can see that I have uh the same structure I have some basic comments I have um uh class class documentation here for each function I have the parameters listed and all that in a different format maybe I should change that to the pie charm format here uh but you can see what it does basically um and yeah this is this is making your code a little bit more professional especially if you have a large code base it's good to have it documented so people that want to contribute to it or use it know what you're doing in your code all right and last but not least we're going to talk about something that is also going to influence the performance not only the readability and the documentation of the code uh and this is one of those things that I actually do in my codes so the first two things typ hinting and documentation they make your code more professional but my code on GitHub as of right now is not really professional maybe I'm going to change that but this third thing is actually something that I really do and it is using list comprehensions or in general these functions like filter reduce and so on uh instead of doing everything manually with loops so for example uh what would be a bad example um of or a bad practice example let's say we have some numbers here and we say this is just list range uh 100 so we have 100 values here uh and now what we want to do is we want to create a list that has all these numbers squared so what could we do here we could say squared equals empty list and we could say 4X or for number in numbers squared do append uh number squared and we can also do something like that we can also say if uh number modulo 5 equals z so if the number is divisible by five then add it to the list uh and you can see right away first of all that if I hover with a mouse here it says convert for Loop into list comprehension because pyr says okay this is not intelligent to do it that way pyr already recognizes that this is not a good way uh to fill up that list with uh with squares so what we want to do instead this is bad practice what we want to do instead is we want to use a list comprehension that has the same effect but it's going to be more efficient uh and it's a more declarative functional approach to programming so uh the same thing could be done as squared equals and then number for number or actually sorry number squared for number in numbers if number modulo 5 equals z now for someone who does not understand list comprehensions this might be a little bit uh less readable but most people that or actually all people that program and python on a regular basis and contribute meaningfully to projects will understand this uh as well so they will understand this probably even better than this uh so this is just a bad practice and this is a good practice this is what you should be doing um you should not be using full loops and empty lists and appen statements you should use list comprehensions for for Stuff like that uh and now another example for example which is not list comprehension but it's a filtering process let's say you want to have the numbers but you want to filter out all the numbers that are uh that are uh divisible by five so you want to have all the numbers that are divisible by five what can you do you can say Okay um filtered list is an empty list and then we can say for this time I'm going to choose X as a name for X in numbers we're going to say say if x modulo 5 equals 0 and let's also say and x modulo 3 equals z so we want to have both of them if that is the case then uh we're going to say filtered list. append X so uh this in a sense can also be turned into a list comprehension as you can see if I hover here turn into list comprehension but it can also be done with the filter method so we can also or function uh we can also go ahead and say filtered list equals and then we can say filter or actually we need to say list filter because filter returns a filter object and list turns it into a list so list filter uh and first we specify the function what is the function going to be it's going to be Lambda X um and we're going to say x modulo 5 = 0 and x modulo 3al 0 and The Collection is going to be numbers so I can delete that and if we run this if we print that we're going to see that this produces the same result we can get all the numbers that are divisible by five and three and this is more efficient now you should always use something like that you should either use filter reduce map or you should use a list comprehension but you should not uh use these Loops where you start with with an empty list and you fill up the list with appen statements uh based on certain conditions this is inefficient and it's not the pythonic way of doing things nowadays all right so that's it for today's video those were the three major points that I wanted to talk about uh I think they're pretty important but of course being a professional python coder is not limited to those three things those are three things that I think are important type hinting documentation and uh using list comprehensions and efficient functions instead of doing it in a slow and old way uh if you want to have more Inspirations for design principles while coding uh you can open up your idol or any python interpreter that can execute your statements and you can type import this and you're going to get the Zen of python um which is part of python itself so it's not some library that you install and then you can see some principles like beautiful is better than ugly explicit is better than implicit this is another principle you want to write explicit python code not implicit python code simple is better than complex and so on you can read that and get an idea of how python code is written and of course you can just Google how to make your python code more professional there are a lot of resources out there uh but this is it for today's video I hope you enjoyed and hope you learned something if so let me know by hitting a like button leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you very much for watching see you in the next video and bye [Music]

Original Description

Today we will cover three ways in which you can make your Python code more professional. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎙 Discord: https://discord.gg/JU4xr8U3dm 🎵 Outro Music From: https://www.bensound.com/ Timestamps: (0:00) Intro (0:16) 1 - Type Hinting (6:20) 2 - Docstrings (13:15) 3 - Declarative Programming (18:08) Outro
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches viewers how to make their Python code more professional by implementing type hinting, docstrings, and declarative programming, making it easier to read, understand, and maintain.

Key Takeaways
  1. Implement type hinting in Python functions
  2. Write effective docstrings for code documentation
  3. Apply declarative programming principles for improved readability
  4. Use tools like Python Bible and Algorithm Bible for reference
  5. Join communities like NeuralNine's Discord for support
💡 Using type hinting, docstrings, and declarative programming can significantly improve the professionalism and readability of Python code.

Related Reads

📰
AI CLI Tools Are Eating Each Other's Lunch
AI CLI tools are becoming increasingly similar, making it difficult to choose between them, and their failure patterns are identical, highlighting the need for more transparency and accountability in AI development
Dev.to · Tracepilot
📰
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Learn how loop engineering can automate AI workflows, making traditional prompting methods obsolete
Medium · Machine Learning
📰
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Learn how to build a real-time Pokémon TCG AR overlay using a webcam, gaming PC, and open-source AI
Medium · Deep Learning
📰
I tested the new Claude Desktop on Linux - here's how it compares to rival apps
Learn how Claude Desktop on Linux compares to rival apps and its limitations with local AI
ZDNet
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →