3.8 HACKS
Student led teaching on Abstraction. Teaching how various data types can use abstraction for computational efficiency.
# Initialize a counter variable
counter = 0
# While loop to print "hello" 5 times
while counter < 5:
print("hello")
counter += 1 # Increment the counter
hello
hello
hello
hello
hello
# Define your names
first_name = "John"
middle_name = "Michael"
last_name = "Doe"
# Repeat the whole process 4 times
for _ in range(4):
print(first_name) # Print the first name once
print(middle_name * 2) # Print the middle name twice
print(last_name * 3) # Print the last name three times
John
MichaelMichael
DoeDoeDoe
John
MichaelMichael
DoeDoeDoe
John
MichaelMichael
DoeDoeDoe
John
MichaelMichael
DoeDoeDoe
# Define your name
name = "John Doe"
# Iterate through each letter in the name, excluding spaces
for letter in name:
if letter != " ": # Check if the letter is not a space
print(letter)
J
o
h
n
D
o
e
# Create a dictionary with some fruit and their colors
fruit_colors = {
"Apple": "Red",
"Banana": "Yellow",
"Grapes": "Purple",
"Orange": "Orange",
"Lemon": "Yellow"
}
# Iterate through each key and value in the dictionary
for fruit, color in fruit_colors.items():
print(f"The {fruit} is {color}. That's a delicious fruit!")
The Apple is Red. That's a delicious fruit!
The Banana is Yellow. That's a delicious fruit!
The Grapes is Purple. That's a delicious fruit!
The Orange is Orange. That's a delicious fruit!
The Lemon is Yellow. That's a delicious fruit!
# Define the correct password
correct_password = "securepassword123"
# Initialize a variable to store the user's input
user_input = ""
# Use a while loop to prompt the user for a password until the correct one is entered
while user_input != correct_password:
# Ask the user for their password
user_input = input("Please enter your password: ")
# Check if the password is correct
if user_input == correct_password:
print("Password is correct!")
else:
print("Incorrect password. Please try again.")
Incorrect password. Please try again.
Password is correct!
# Ask the user for their name
user_name = input("Please enter your name: ")
# Iterate through each letter in the name
for letter in user_name:
print(letter)
n
a
t
h
a
n
# List of different fruits
fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape"]
# Iterate through the list of fruits
for fruit in fruits:
print(fruit)
Apple
Banana
Cherry
Date
Elderberry
Fig
Grape