Python Intermediate Tutorial
Chapter 1 - Introduction
So recently there has been an influx of beginner/basic Python tutorials which often cover the same topics or cover elementary topics such as
print('Hello World'), which to be honest are really boring.
Due to this, I thought it would be cool to make a Python tutorial that covers some of the more challenging and fascinating topics. These topics would include:
- For Loops
- Advanced Arguments
- List Comprehension
Note: For loops technically should be beginner concepts but it is important to have a strong knowledge of them for this tutorial
Chapter 2 - Why Learn This?
Many of the topics that I listed above may seem irrelevant or unpractical but in reality, these topics are extremely useful at problem-solving as well as building reasoning skills.
These concepts come in handy when solving logic challenges or algorithm problems that differ from the so-called "beginner problems" which often just consist of repeating memorized syntax that offers no educational value.
After reading this tutorial, I 100% recommend that you go over to some of these sites and try a few problems out:
- Project Euler(Great for people who enjoy math): https://projecteuler.net/archives
- Edabit(Medium and Hard are probably the best difficulties): https://edabit.com/challenges/python3
- Practice Python(Great for learning and making slightly more useful projects): https://www.practicepython.org/
Note: Project Euler is definitely the hardest, but is super rewarding when you finish a problem or even just a part of it :)
Chapter 3 - For Loops
For loops are used to iterate over data in Python. They can also iterate through numbers while using the
range() function. For loops are also the basis for list comprehension which is a major time saver.
To imagine a for loop in practice, it is best to use it with a list as it makes it very easy to visualize what a for loop actually does. For example:
In the code above, the for loop iterates over the list of hardware and sets a variable named item to the first value in hardware. After that, it prints that variable and then continues down the list printing the second element and so on.
Now in this code example below I will make use of the
range() function which will allow the loop to cycle through numbers. For example:
This code will output:
Note: It starts with 0 and goes up to 4 because the
range() function is non-inclusive which means it will not go all the way up to 5
This happens becuase
range() works very similarly to a list with numbers. What it does is it sets
i to a number and prints that number before moving on to the next one.
This was a fundamental rundown of for loops so I have listed some additional resources:
- w3schools: https://www.w3schools.com/python/python_for_loops.asp
- Programiz: https://www.programiz.com/python-programming/for-loop
- Learnpython: https://www.learnpython.org/en/Loops
Chapter 4 - Advanced Arguments
In python an argument can be passed quite easily by following this syntax:
The issue is that you must create a new argument for every value that you want to pass on to the function. This can be bypassed by using a non-keyworded argument known as
*args is used to pass more arguments into the function than previously defined.
*args can be named whatever you want as long as the is an
* in front of the variable name. They can be used in the following context:
This will output:
*args works very similarly to a list in the code example below and is extremely useful when multiple arguments need to be passed in a list-like fashion.
On the other hand, Python also has
**kwargs which are keyworded variables. They look quite similar to regular variables as they contain keywords or names for the values. An example would be:
This works because the for loop finds the value of each named argument and prints that value which in this case are 3, 4, and 6.
**kwargs are two versatile and efficient arguments that can be used for a variety of programs. For those of you looking to conduct more research into these arguments, here are some useful links:
- w3schools: https://www.w3schools.com/python/gloss_python_function_arbitrary_keyword_arguments.asp
- Programiz: https://www.programiz.com/python-programming/args-and-kwargs
Chapter 5 - List Comprehension
From experience, I can confidently say the list comprehension is within the top 3 most useful and time-saving skills that any Python programmer can have. List comprehension allows you to condense long and messy for loops and if statements into one clear and concise line. For example, I have put a comparison below:
Both of these code snippets do the same exact task, except the difference is that the regular method takes 5 lines, unlike list comprehension which only requires 2 lines of code.
Note: list comparison doesn't always require a conditional buy can contain one if needed
List comprehension also comes in handy when splitting a string into a list of characters in Python. This can be done quite easily as it is similar to a for loop with the exception of all of it being in one line.
This splits the string in one clean line as compared to the traditional method which would require a for loop and an
append() which would make the code overly long.
Here are some additional resources:
- w3schools: https://www.w3schools.com/python/python_lists_comprehension.asp
- Programiz: https://www.programiz.com/python-programming/list-comprehension
Chapter 6 - Lambdas
Though lambdas may not be a core concept of Python it is important to learn them nonetheless as they help make code more efficient. In a nutshell, a lambda is an anonymous function which unlike a normal function that has to be defined by
def, can be defined using the
The basic syntax of a lambda function is:
As you see a lambda function works very similarly to a regular function apart from the fact that it remains anonymous or non-named. Just for comparison, I have added code to show the same thing as a regular function:
Clearly, you can see that the lambda function takes only 2 lines compared to the regular function which takes 5 lines to do the same task.
Lambda functions also can be used with other functions such as,
Map allows code to perform a certain opperation on a list as a whole without the need for a
for loop which can often be useful. For example:
You can also use
filter() which allows you to apply a condition to a list as a whole. For example:
As you can see above the
filter() function does exactly what its name says and filters a list.
Here are some additional resources:
- w3schools: https://www.w3schools.com/python/python_lambda.asp
- Programiz: https://www.programiz.com/python-programming/anonymous-function
Chapter 7 - Conclusion
Along with the topics I already explained in this tutorial, I would highly suggest learning both recursion as well as other algorithms and data structures which are great topics to look into.
I hope you enjoyed this tutorial and please leave any feedback/comments as I would greatly appreciate them.
Hi!, I need help on a minor issue with my python script here in replit. I have this line : if muteuser <= 2: I'm getting a "TypeError: '<=' not supported ....... " the same error if program reaches if muteuser => 3: it will trigger 'TypeError: => blah blah ... ' ... why ? ..what is my coconut head overlooking or missing .. ? ... thx **NOTE: my python script is for a discord bot I'm coding / programming ....