Day5: Python For Loops and Practical Projects

Day5: Python For Loops and Practical Projects

In this lesson, I explored for loops in Python, which allow us to repeat actions and iterate over collections like lists. I also worked on real-world projects like finding the highest score in a list, solving the FizzBuzz challenge, and building a password generator.


?? For Loops in Python:

A for loop lets you iterate over items in a sequence like a list, string, or range of numbers. Here’s a basic syntax:

for item in sequence:
    # do something with the item        

Example: Iterating Over a List:

fruits = ["Apple", "Peach", "Pear"]
for fruit in fruits:
    print(fruit)
    print(fruit + " pie")        

Output:

Apple
Apple pie
Peach
Peach pie
Pear
Pear pie        

?? Finding the Highest Score:

Let's find the highest score in a list using loops and conditionals. This is how the max() function could be implemented manually:

student_scores = [150, 142, 185, 120, 171, 184, 149, 24, 59, 68, 199, 78, 65, 89, 86, 55, 91, 64, 89]

max_score = 0
for score in student_scores:
    if score > max_score:
        max_score = score

print(max_score)  # Output: 199        

?? For Loops with the range() Function:

The range() function generates a sequence of numbers that you can use in loops.

for number in range(1, 10):  # Will print numbers from 1 to 9
    print(number)        

To include both bounds:

for number in range(1, 11):  # Will print numbers from 1 to 10
    print(number)        

?? The Gauss Challenge: Summing numbers from 1 to 100

To calculate the sum of numbers from 1 to 100, inclusive:

total = 0
for number in range(1, 101):
    total += number
print(total)  # Output: 5050        

?? FizzBuzz Challenge:

Project Question Statement:

Write a Python program that iterates through the numbers from 1 to 100 and prints:

  • "Fizz" for numbers divisible by 3.
  • "Buzz" for numbers divisible by 5.
  • "FizzBuzz" for numbers divisible by both 3 and 5.
  • If a number is not divisible by either 3 or 5, print the number itself.

Requirements:

  1. The program should iterate through numbers from 1 to 100.
  2. For each number:If it is divisible by both 3 and 5, print "FizzBuzz".If it is divisible by 3 but not 5, print "Fizz".If it is divisible by 5 but not 3, print "Buzz".If it is divisible by neither, print the number itself.

Example Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...        

Code:

for number in range(1, 101):
    if number % 3 == 0 and number % 5 == 0:
        print("FizzBuzz")
    elif number % 5 == 0:
        print("Buzz")
    elif number % 3 == 0:
        print("Fizz")
    else:
        print(number)        

?? Password Generator Project:

This project generates a random password based on user preferences for letters, symbols, and numbers. You can solve it at two difficulty levels: easy (with characters in sequence) and hard (with characters shuffled).

Step 1: Ask for User Input:

letters = ['a', 'b', 'c', ..., 'Z']
numbers = ['0', '1', '2', ..., '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("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))        

Step 2: Easy Version (Sequential Characters):

In this version, the password is generated in the order of letters, then symbols, and finally numbers:

password = ""

for _ in range(nr_letters):
    password += random.choice(letters)

for _ in range(nr_symbols):
    password += random.choice(symbols)

for _ in range(nr_numbers):
    password += random.choice(numbers)

print(f"Your password is: {password}")        

If the user wants 4 letters, 2 symbols, and 3 numbers, the password might look like this: fgdx$*924

Step 3: Hard Version (Shuffled Characters):

In the advanced version, you shuffle the characters so the order of letters, symbols, and numbers is random:

password_list = []

for _ in range(nr_letters):
    password_list.append(random.choice(letters))

for _ in range(nr_symbols):
    password_list.append(random.choice(symbols))

for _ in range(nr_numbers):
    password_list.append(random.choice(numbers))

random.shuffle(password_list)

# Convert the list back to a string
password = ''.join(password_list)
print(f"Your password is: {password}")        

If the user wants 4 letters, 2 symbols, and 3 numbers, the password might look like this: x$d24g*f9


?? Key Concepts and Takeaways:

  • For Loops: Looping over lists, ranges, or other iterables to repeat actions.
  • range() Function: Used to generate sequences of numbers.
  • Summing Numbers: Adding up a series of numbers using loops.
  • FizzBuzz Challenge: A classic problem that tests your understanding of loops and conditionals.
  • Password Generator: A fun project combining user input, randomization, and lists to generate secure passwords.

Stay tuned for more as I continue building projects and solving challenges during my #100DaysOfCode journey! ??

要查看或添加评论,请登录

Muhammad Hanzala的更多文章

社区洞察

其他会员也浏览了