FOR loops and Lists
Useful stuff
Homework from last time (ignore if you haven't done the last lesson)
First homework
So the first part was to make a program output 100 times , Arstotzka makes good tutorials for beginners ,the point of this task is just to use the while loop to do it . So here's how [if you still don't get the while loop read the last lesson again or ask me]

When you run this code , it will print exactly 100 times . I hear you ask , how do we know if its printed 100 , 99 or 101 times , if X was 1 instead of 0 the result would be different. I will now modify the code to make it also print the X . We also gonna do it in the same line so that we fill 100 [hopefully 100] lines instead of 200

Well that was easy , we just added the x to the print function
If we run the code we can see that the last line printed is 'Arstotzka makes good tutorials for beginners 99' well shouldnt it be 100? Yeah it should , but X is 0 at the start so it counts from 0 , to make it display 100 instead of 99 and all numbers correctly when can do this :

We added '+1' so that it wouldn't print from 0 . And that's all about the first homework
Second Homework
For the second task you had to make a program that gets user input , then it would check if the input is Glory To Arstotzka and if it's true then it should print ' That's true ' and if you type something else in the user input then it should output Do you mean, Glory To Arstotzka?

Note that 'That's True' will print ONLY if the input is EXACTLY "Glory to Arstotzka" . So it's case sensitive. There are workarounds for the case senstivity , but we won't use them here.
For now take a look at the print("That's True!") I used double quotes to start and end the print so that I was allowed to use single quotes. I also did the same to the last print
For loops and Lists
For loops and why it's different from others loops
In Python the for loop is really different from other languages. In C++ for examples , it allows you to define a variable, make a condition , then make something run at the end of the loop and it runs as long as the condition equals to True
In python the FOR is used to iterate trough elements and it also creates a local variable that works only inside the loop.
For loops can be used with lists and it also be used with range(<int that shows how much>)
Range(<int>)
Let's see what range() does.

Not really useful with print , but it can be using FOR loops .
How we use these loops?
In the homework I asked you to print something 100 times using while loops , you had to have 1 line to specify the variable . With for loop , you don't need to specify a variable before , you can use range() . Actually you can't use a regular variable unless its inside the range() . Also the count is included in the for loop. So let's see an example

Great , now you can use a for loop instead of a while loop . Actually you don't have to , for loop is just cleaner , but the main reason I use for loop is to iterate trough a list since using a while loop can STILL be used , but it's extremly messy.
To use them in lists we need to know what lists are
Lists , why do I need them?
Lists are a way to store data , exactly like variables , but lists are more organised and easier to iterate trough checking conditions and so on.
Let's say that you try to make a hearthstone card value bot [Showing you the best possible choice to make according to stats and mana] . You will probably have a lot of possible cards let's say just only 10 . You will have to declare a variable for each and when you wanna check which of them is a spell you will have to write every variable in an IF loop. You might think its necesary to check each , but you can just make a temp variable for every card and then add those cards to a list. After they are all in the list , use a for loop to check which of them is a spell.
How to Use Them and How to Use Them Using a For Loop?
A list is defined like this:
name_of_the_list = [elements split by a comma] so
example = [0, 4, 5, 12, 36, 2]
Lists have a thing called "list comprehension " and it allows you to use commands like len(example) to see its LENgth , or use example.index(<slot>) or example.append(<element to add at the end of the list>)
We won't cover them now , it was just a brief overview . Let's use a for loop to check which of those 6 elements it's equal to 5.

When the code is being run it will iterate trough each element and say if it's five or not.
We gonna talk more about list comprehension and how to improve this code up there in the next lesson.
Practice , practice , practice
You most probably know , if you learnt something and you don't make use of it , it's gonna be forgotten . [Also if you multi tasking you can't memorise at all no matter what , I know this is unrelated]
The task this time is gonna be to use the same list from above to check which of those elements can be divided by 2.
Modulus works the same as any operator as syntax .
You can also modify the code from above that checks if the element is five . The task requires you to print if each element is or isn't divided by 2
Great job ! We are close to end this tutorial series [probably]
Last updated
Was this helpful?