Videos
I am an absolute beginner in programming, so please forgive my utter lack of understanding.
I came across this concept quite recently through a course I am taking in Python. I re-watched the lessons, I have tried doing some separate research on this topic, but I am still having a hard time wrapping my head around it.
The project I am currently working on is a random password generator. I was stumped and couldn't figure it out on my own so I watched the teacher's solution as a last resort. Especially puzzled when she used this code:
password = ""
for char in password_list:
password += charFor context, this code is supposed to convert a list of characters (from password_list) into a string.
I thought that for loops were used for iteration. I am only familiar with using "for __ in __ range(a, b)" so far, so I was quite puzzled by this loop as I don't understand how it works. Could someone please help me break it down into simpler terms? That would be super appreciated.
Please and thank you.
A for loop takes each item in an iterable and assigns that value to a variable like w or number, each time through the loop. The code inside the loop is repeated with each of those values being re-assigned to that variable, until the loop runs out of items.
Note that the name used doesn't affect what values are assigned each time through the loop. Code like for letter in myvar: doesn't force the program to choose letters. The name letter just gets the next item from myvar each time through the loop. What the "next item" is, depends entirely on what myvar is.
As a metaphor, imagine that you have a shopping cart full of items, and the cashier is looping through them one at a time:
for eachitem in mybasket:
# add item to total
# go to next item.
If mybasket were actually a bag of apples, then eachitem that is in mybasket would be an individual apple; but if mybasket is actually a shopping cart, then the entire bag could itself meaningfully be a single "item".
A for loop works on an iterable: i.e., an object that represents an ordered collection of other objects (it doesn't have to actually store them; but most kinds of iterable, called sequences, do). A string is an iterable:
>>> for c in "this is iterable":
... print(c, end=" ")
...
t h i s i s i t e r a b l e
However, a number is not:
>>> for x in 3:
... print("this is not")
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
The built-in range function allows an easy way to iterate over a range of numbers:
>>> for x in range(3):
... print(x)
...
0
1
2
In 2.x, range simply creates a list with those integer values; in 3.x, it makes a special kind of object that calculates the numbers on demand when the for loop asks for them.
About a month in to my Intro to Python class at my local community college, this week was the lecture and HW on "While" and "For In" Loops. I gotta say, I'm completely lost. Anyone have any tips or possibly helpful material for a noobie? Genuinely just seems like everything before this was linear and somewhat straightforward on what the solution or answer was going to become. Now print statements are not what they seem or not in the order in which I believe they were gonna print. The formulas as well, just seems like the difficulty level was raised tremendously lol. Either way Thanks and any help is appreciated.