10 Python Tips and Tricks to Improve Code Quality | Beginner tips and tricks for efficiency
Become a more efficient developer by using these tips and tricks in Python! These tips and tricks are something I've learned in my 4 years of experience with python. These always come in handy and make me more efficient. Let's see what the 10 tips and tricks are:
1. Unpacking
Value unpacking or variable unpacking in python is when you assign multiple values from an iterable in python such as lists or dictionaries or tuples into individual variables. This makes your code look more elegant and improves the code readability. This is a must know and it helps in competitive programming as well.
2. Ternary operator:
Similar to ternary operator in C, Dart or Java, python has its own ternary operator which means it is a short way of writing a conditional if-else statement. The syntax for the ternary operator in python is as follows, "true_statement if condition else false_statement". This really works when returning some value based on a condition from a function or when assigning a value to a variable based on a condition.
3. for/while...else loop
The for-else or while-else loop is really useful in competitive programming. It saves you the time of writing and maintaining a flag variable. It's a normal for loop with an else block at the end of the for loop. The else block gets executed only when the loop gets executed fully without encountering a break or a return statement.
4. List comprehension
List comprehension is a way to quickly build lists in python with default or dummy values. It makes the code more readable.
5. enumerate
The enumerate function in python is most commonly used to keep track of the index when looping through a list in python. This saves you from maintaining a separate index variables. It comes in handy when you want to make sure the index matches the values in a list.
6. arr = list(map(int, input().split()))
This single line of code is something you MUST KNOW if you're a beginner in competitive programming or trying to get an array input from a single of integers separated by space. This also demonstrates the power of the map function. The reason we have to wrap the map function with a list function is because the map function returns a map object which is an iterable, but it isn't a list which is why we type cast it to a list.
7. fstrings
fstrings are really useful when you want to write strings that need more code readability. This feature was introduced in python 3.6+. In python, use fstrings by adding the letter f before starting the quotes of the actual string. Inside the string if you want to print the value of a variables, surround it with curly braces and python will automatically substitute the value of the variable present inside the fstring.
8. sort
Sort is a powerful method. It's important because it has a "key" argument which a lot of people skip over. In the video I show you how we can sort by frequency using the in-built sort method and the key argument.
9. zip
Zip, as the name mentions, combines two or more lists/iterables into a single iterable. For examples, if you want to combine two lists in python, using the zip function the resultant list would be a list of tuples where each tuple has the corresponding value in the corresponding index in each of the two separate lists.
10. Python code formatter using black formatter
This is a vscode/python tip which makes your code that improves your code readability by so much. I wish I knew this when I had some basic experience with python. A code formatter formats your code to make it look good and follow python PEP rules. There are different python code formatters but I found black code formatter to be the best python code formatter out there. This can be installed using pip or directly using vscode.
TIMESTAMPS:
00:00 Intro
00:15 Unpacking
03:48 Ternary Operator
04:50 for/while...else loop
06:28 List comprehension
08:15 enumerate
10:00 getting input in competitive programming in one line
11:12 fstrings in python
13:10 sort (frequency sorting using in-built function)
15:13 zip
16:25 Python code formatter using black formatter
Find me on:
Twitter: https://twitter.com/1Lemon1Lemonade
Видео 10 Python Tips and Tricks to Improve Code Quality | Beginner tips and tricks for efficiency автора Квантовая Логика
Видео 10 Python Tips and Tricks to Improve Code Quality | Beginner tips and tricks for efficiency автора Квантовая Логика
Информация
2 декабря 2023 г. 6:57:45
00:17:24
Похожие видео