Learn to Code via Tutorials on Repl.it!

← Back to all posts

# 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
• Lambdas

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:

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:

``````hardware = ['Monitor', 'Keyboard', 'Mouse', 'Computer']

for item in hardware:
print(item)``````

Output:

``````Monitor
Keyboard
Mouse
Computer``````

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:

``````for i in range(5):
print(i)``````

This code will output:

``````0
1
2
3
4``````

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:

# Chapter 4 - Advanced Arguments

In python an argument can be passed quite easily by following this syntax:

``````def do_something(num):
print(num)

do_something(5)``````

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`

`*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:

``````def print_nums(*args):
for num in args:
print(num)

print_nums(1, 2, 3, 4)``````

This will output:

``````1
2
3
4``````

`*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:

``````def print_stuff(**kwargs):
for item in kwargs.values():
print(item)

print_stuff(a=3, b=4, c=6)``````

Output:

``````3
4
6``````

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.

In summation, `*args` and `**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:

# 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:

Regular Method:

``````hardware = ['monitor, 'keyboard', 'mouse', 'pc']
result = []

for item in hardware:
if 'm' in item:
result.append(item)``````

List Comprehension:

``````hardware = ['monitor', 'keyboard', 'mouse', 'pc']
result = [item for item in hardware if 'm' in item]``````

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.

``````string = 'Hello'
split_list = [char for char in string]``````

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:

# 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 `lambda` keyword.

The basic syntax of a lambda function is:

``````result = lambda x: x + 1
print(result(5))``````

This outputs:

``6``

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:

``````def add(x):
return x + 1
result = add(5)
print(result)``````

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, `filter()` and `map()`.

`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:

``````lst = [1, 2, 3, 4, 5]
new_lst = list(map(lambda x: x + 1, lst))
print(new_lst)``````

Output:

``[2, 3, 4, 5, 6]``

You can also use `filter()` which allows you to apply a condition to a list as a whole. For example:

``````lst = [4, 9, 15, 7, 12]
new_lst = list(filter(lambda x : (x%3 == 0), lst))
print(new_lst)``````

Output:

``[9, 15, 12]``

As you can see above the `filter()` function does exactly what its name says and filters a list.

Here are some additional resources:

# 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.