While loops
A while loop will repeat a certain action over and over again as long as the condition to that while loop is true. The basic syntax of it is as follows:
For example:
while condition:
code....
Below is an example:
while True:
print("Hello") # the condition is always True, so "Hello" will be printed infinitely
while False:
print("This Won't Be Printed")
As the comments of the code indicate, an infinite number of "Hello"s will be printed. While loops can have very useful applications, for example when generating all of the numbers from 1 to 10.
number = 1
while number <= 10:
print(number)
number += 1
Here, number will be increased by one each time until number reaches eleven. So therefore, all numbers, starting from one, will br printed until ten (including ten)
For loops
The second and arguably more useful type of loop in Python is the for loop, which loops through a given iterable and performs a command with the elements of the iterable. Lists are an iterable data type, so we can give an example with the list below:
for fruit in ["Apple", "Banana", "Cherry"]:
print(fruit)
Here, the value "fruit" will first be assigned "Apple" and "Apple" will be printed. Then, the loop continues and the value "fruit" will be assigned "Banana" and "Banana" will be printed. Finally, the value "fruit" will be assigned "Cherry" and "Cherry" will be printed.
Output:
Apple
Banana
Cherry
An important aspect of for loops is the usage of the range function. The range function creates an iterable which has the values of all the whole numbers between two specific whole numbers.
for number in range(1, 11)
# creates an iterable data type including all whole numbers from 1 up until 11 (not including 11)
In order to remove a key and it's value from a dictionary, we would use the del keyword we learned about previously. For example, let's remove Charlie and her value:
for number in range(1, 11):
print(number)
Output:
1
2
3
4
5
6
7
8
9
10
For loops can also be used with dictionaries, strings or any other iterable data types outside of just lists and the range function. Below is an example of the usage of for loops with dictionaries :
students = {
"male": ["Noah", "Charlie", "Ben"],
"female": ["Olivia", "Madison", "Ashley"]
}
for key in students.keys():
print(key)
for name in students[key]:
print(name)
Output:
male
Noah
Charlie
Ben
female
Olivia
Madison
Ashley
List Comprehensions
even_numbers = [x for x in range(1, 101) if x % 2 == 0]
print(even_numbers)
# x is going to loop through the entirety of the iterable data type
# we will receive from range(1, 101)
# then later, x will only be kept in the list if it is divisible by 2
odd_numbers = [a for a in range(1, 101) if a % 2 != 0]
print(odd_numbers)
strings_comprehension = ["hello", "cake", "birthday", "happy"]
upper_strings = [string.capitalize() for string in strings_comprehension]
print(upper_strings)
Make a program which will receive input from the user regarding how many letters, symbols and numbers they would like in the password and create a random password with the letters, numbers and symbols lists given. You may need to research the .split() method and the .join() method before doing this. Below are the lists of letters, numbers and symbols.
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
Step 1: Import the random module to your file.
Step 2: Add the lists given to your file.
Step 3: Using the input functions, receive how many letters, symbols and numbers a user wants in their password and store them all separately in three different variables.
Step 4: Loop through all of the lists in the amounts specified in step 2.
Step 5: After looping through and randomly receiving an element from each of the lists, add those elements into an empty string.
Step 6: Print that string to the screen.
This is only a suggested answer
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 = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password = ""
for i in range(nr_letters):
letter = random.randint(0, 51)
password += letters[letter]
for i in range(nr_symbols):
symbol = random.randint(0, 8)
password += symbols[symbol]
for i in range(nr_numbers):
number = random.randint(0, 9)
password += numbers[number]
print(password)
You have reached the end of Section 7: Loops!