## DEV Community 👩‍💻👨‍💻 Kyle Leonard

Posted on • Originally published at blacknerd.dev

# Day 5: Python Loops

## Today's Lesson

So today was all about loops and the use of what we learned in the previous lessons. I found it to be a bit easier than I thought it was going to be. Maybe because I banged my head on my desk so much when learning for/forEach loop and its variations in JS, it just clicked this time around after coming back to it. The way you implement it in python is a bit more straightforward as well.

#### Example of a `for()` loop in JS:

``````for (let i = 0; i < 9; i++) {
console.log(i);
}
``````

Output:

``````012345678
``````
##### My hardships with learning this in JS

The conditions in JS vs Python are what I believe were the key differences with me getting the python way of doing for loops. I will say it's a bit more condensed in JS but that's where I start to trip up once the logic and for loop gets more complicated; How you write the loop iteration was confusing for me at the start. Then once you get to situations where the initialization block and the conditional block are not added or needed, my brain starts to melt trying to figure out how to move forward. Usually results in me stuck with an infinite loop cause I never remembered how to properly break the loop in the body.

#### Example of a `for()` loop in Python:

``````numbers = [1, 2, 3]

for x in numbers:
print(x)
``````

Output:

``````1
2
3
``````
##### Python's straightforward approach

As you can see, Python's (simple) version of a `for` loop is somewhat different but all in all similar. The main difference is the loops syntax itself where you don't have to classify the variable (Python doesn't use the var/let/const syntax or anything similar). You also don't need to tell it how many times to run through the loops since indentation is key for the loops in Python. Any condition or statement within the for loop itself will be run in succession. When it comes to looping through a range of elements, its as simple as using the `range(start, finish)` method.

This project was actually pretty cool considering the need for VERY strong passwords these days.
Not much to explain about the function of the project (pretty obvious from the name 😄) and you can see my solution code below:

``````import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '\$', '%', '&', '(', ')', '*', '+']

nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

for letter in range(1, nr_letters + 1):

for number in range(1, nr_numbers + 1):

for symbol in range(1, nr_symbols + 1):

``````

My final solution was a bit different from the instructors' solution but the way I have it implemented is a bit cleaner and uses some methods that haven't been discussed yet (I didn't know them prior; Learned these methods as I was looking for ways to break the password up, shuffle it, and add it back together in a string.)

If you are curious on the instructors solutions, I've added it below:

``````import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '\$', '%', '&', '(', ')', '*', '+']

nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

for char in range(1, nr_letters + 1):

for char in range(1, nr_symbols + 1):

for char in range(1, nr_numbers + 1):