@Lt.Smith Still a bit of a general problem, but I will give the first one a bash.
Solving problems with code is inherently a very logical process and the way you decide where to start writing is pretty much the same way you solve the problem otherwise. This is a typical procedural way of writing code, one of the main coding paradigms. So how does this work?
Say you want you are given a task to find all the even numbers in a large list of numbers. If you were to do this manually you probably follow the following procedure:
- Get your list of numbers
- Take the first number from the list of numbers
- Check if this number is a even number
(a) If it is a even number, add it to my list of even numbers that I already have
(b) If not, then just do nothing with the number
- Repeat these steps until you have checked all the numbers.
- Write out all the even numbers you found.
So where would you start if you had to do this programmatically? Well actually in exactly the same place.
1. Get your list of numbers
The numbers may in a csv
file in which case you would need to first read it from file using any number of methods, using the pandas
package popular for data analytics, or the normal python open
command etc.
But for now lets just assume you have already done this and have an actual list
of numbers s follows:
numbers = [3, 4, 5, 67, 445, 311, 764, 5678, 33345]
2. Take the first number of the list
This is easy. I have a list and to get to the first number I just need to do this numbers[0]
and this will return 3
. When I started coding this was very confusing to me. Why numbers[0]
and not numbers[1]
? Python, like many other languages (but not all!) start numbering things at 0
and not 1
. So numbers[0]
represents the first element in the list numbers
.
3. Check if this number is a even number
How do we check if it is even? Well there are many ways but let us go with the Modulus or Mod operator. What Mod does is it divides a number by another number and see if anything remains. Eg, 3 mod 2 = 1
but 4 mod 2 = 0
. So when any number mod 2 = 0, it is even.
I can write a little function to this work for me in Python and it may look something like this:
def is_even(number):
if number % 2 == 0:
return True
else:
return False
(a better way to write this function would be
def is_even(number):
return number % 2 == 0
which will provide the exact same result as the one above, but the first will work just fine.)
I send my number which is 3
to the function is_even
by using the following code:
result = is_even(3)
The function checks if the modulus is equal (==
) to 0
and if so returns True
, if not it returns False
.
So now I know if my first number is even or odd. I can create a new list to store this number in:
even_numbers = []
number = 3
if is_even(number):
even_numbers.append(number)
Cool. My first number is checked and not added because it turns out to be uneven so my list remains empty.
But what about the rest then? So this is where the loops come in handy.
4. Repeat these steps until you have checked all the numbers.
I can write code to manually go through each component in the list but that is really no fun. Let’s rather use a for
or a foreach
loop for this.
Using the for
loop is really easy and will look something like this:
for number in numbers:
if is_even(number):
even_numbers.append(number)
Here I cycle through every item in numbers
, checks if it is_even
and adds it to the even_number
list if it is.
I can then print the even number list to make sure I got it right and it will give me:
[4, 764, 5678]
So to solve problems with code you follow the exact same procedure as you would without code. Break the problem into simple steps and then write code to do the work.
The full code for this example is here:
numbers = [1, 4, 5, 67, 445, 311, 764, 5678, 33345]
def is_even(number):
return number % 2 == 0
even_numbers = []
for number in numbers:
if is_even(number):
even_numbers.append(number)
print(even_numbers)
There is an even more concise way to write this code and it looks like this:
numbers = [1, 4, 5, 67, 445, 311, 764, 5678, 33345]
even_numbers = [number for number in numbers if number % 2 == 0]
print(even_numbers)
This uses a more advanced concept called List Comprehensions which Mosh also talks about. It is powerful but as with your problem with lambda functions, it is not important to make use of this right from the start. Do what works for you and what you understand.
Hope this helps.