Sunday, January 10, 2021

Python - Loops (Day 5)

This is a 100 Days challenge to learn a new language (Python). 100 Days of Code - The Complete Python Pro Bootcamp 

I will post some notes to motivate myself to finish this challenge.


Loop for List



Ex:
# Define a list
fruits = ["Apple", "Banana", "Pear"]
print(fruits)

# Iterate a list
for fruit in fruits:
    print(fruit)

Result:
['Apple', 'Banana', 'Pear'] Apple Banana Pear



Common function for List



Ex:
scores = [1, 2, 3, 4, 5, 6]

print(f"max: { max(scores) }")
print(f"min: { min(scores) }")
print(f"len: { len(scores) }")
print(f"sum: { sum(scores) }")

Result:
max: 6 min: 1 len: 6 sum: 21



Loop with range()



Generate a range of a number Reference

Ex:
# NOTE: The second parameter is for STOP!
for number in range(1, 5):
    print(number)

Result:
1
2
3
4



Challenge - Find the max value from a list



Ex:
# NOTE: You are not allowed to use the max or min functions.

# Get user input
student_scores = input("Input a list of student scores ").split()
for n in range(0, len(student_scores)):
    student_scores[n] = int(student_scores[n])
print(student_scores)

# Define a variable for max
max = 0

# Iterate all score
for score in student_scores:
    # Store the bigger value
    if score > max:
        max = score

print(f"The highest score in the class is: { max }")

Result:
Input a list of student scores 1 2 3 [1, 2, 3] The highest score in the class is: 3



Challenge - Calculate sum of even number from 1 to 100



Ex:
# NOTE: Should use range() function in this exercise

count = 0
for number in range(2, 101):
    if number % 2 == 0:
        count += number

print(count)

Result:
2550



Challenge - PWD Generator



Ex:
# Password Generator Project
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"))

pwd_list = []
pwd = ''

# Eazy Level - Order not randomised:
# e.g. 4 letter, 2 symbol, 2 number = JduE&!91
for _ in range(0, nr_letters):
    pwd_list.append(random.choice(letters))

for _ in range(0, nr_symbols):
    pwd_list.append(random.choice(symbols))

for _ in range(0, nr_numbers):
    pwd_list.append(random.choice(numbers))

print(pwd_list)

# Hard Level - Order of characters randomised:
# e.g. 4 letter, 2 symbol, 2 number = g^2jk8&P
random.shuffle(pwd_list)

for s in pwd_list:
    pwd += s

print(pwd)

Result:
Welcome to the PyPassword Generator! How many letters would you like in your password? 6 How many symbols would you like? 2 How many numbers would you like? 2 ['N', 'u', 't', 'Z', 'd', 'A', ')', '+', '8', '8'] uA8Z)Nt8d+


No comments:

Post a Comment