Young Pythoneers: Recap & Review for Variables, Selections, Loops & Operators
Issue 7: Sharpen Your Python Skills: Test Your Knowledge Coding Challenges
Hello Python learners,
It's time to solidify your Python foundations! This newsletter is a quick review of the core concepts we've covered so far: variables, selection statements (if/else), iteration (loops), and operators. Get ready to test your skills with some hands-on exercises.
Key Concepts Refresher:
Variables: Named containers for storing data (numbers, text, etc.).
Selections (if/else): Make decisions in your code based on conditions.
Iterations (Loops): Repeat actions multiple times efficiently.
Operators: Perform calculations, comparisons, and logical operations.
Coding Challenges:
Variable Swap: Write code to swap the values of two variables (without using a temporary variable).
FizzBuzz: Print numbers 1 to 100, replacing multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
Prime Checker: Create a function that checks if a given number is prime.
List Manipulation: Given a list of numbers, calculate the sum and average.
Password Strength: Write a function that checks if a password meets certain criteria (e.g., length, mix of characters).
Let's Get Coding! Try solving these challenges on your own. Don't worry if you get stuck – the answers are below.
Answers to the challenges
# Challenge 1
x, y = 10, 20
x, y = y, x # Swap values
print("x = ",x, ", y = ",y)
# Challenge 2
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
# Challenge 3
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Challenge 4
numbers = [5, 12, 8, 3, 21]
sum_of_numbers = sum(numbers)
print("Sum: ",sum_of_numbers )
average = sum_of_numbers / len(numbers)
print("Average:", average)
# Challenge 5
def check_password(password):
if len(password) < 8:
return False
if not any(char.isdigit() for char in password):
return False
if not any(char.isupper() for char in password):
return False
if not any(char.islower() for char in password):
return False
return True
if (check_password("Pythoneers24") == False):
print("Password if wrong!!")
else:
print("Password is Correct :)")
Answer to the previous newsletter challenge (Issue 6)
def calculate_triangle_area():
# Get base from user
base = float(input("Enter the base of the triangle: "))
# Get height from user
height = float(input("Enter the height of the triangle: "))
# Calculate area
area = 0.5 * base * height
# Display result
print("The area of the triangle is:", area)
# Run the function to start the program
calculate_triangle_area()
Stay Tuned!
We have much more exciting Python to explore! Please share & Keep an eye out for future newsletters covering new concepts, projects, and more practice opportunities.
Happy Coding!
YoungPythoneers Team