filmov
tv
Learn Code Quiz: Data Science | Loops in Python, and If-Else Statements | Learn Python in a Week

Показать описание
Ready to embark on an exciting coding adventure? Join us for our "Learn Code Quiz" series, where we explore the fascinating world of Python programming! 🚀In this episode, we'll delve into two essential topics: Data Science and Loops in Python. Get ready to master the art of iterating through data and harness the power of loops using both 'for' and 'while' constructs. 📊🔍Additionally, we'll unravel the mystery of 'if', 'elif', and 'else' statements. Learn how to make your code more intelligent and responsive with conditional statements in Python. 🧠💡
LOOPS IN PYTHON: for, while, if, elif
# Step 1: Initialize a list of numbers
numbers = [5, 10, 15, 20, 25]
Step 2: Using a 'for' loop
print("Using 'for' loop:")
for number in numbers:
# This line starts the 'for' loop. The loop will iterate through each element in the numbers list.
# During each iteration, the current element in the list will be assigned to the loop variable number.
result = number * 2
# Inside the 'for' loop, this line multiplies the current element number by 2 and stores the result in the variable result.
print(f"{number} multiplied by 2 is: {result}")
# F-strings (also known as formatted string literals) are a feature introduced in Python 3.6
# that provides a concise and convenient way to format strings.
# They make it easier to embed variables and expressions inside strings without the need for
# complicated string concatenation or formatting functions.
# The f-string is enclosed in the outermost quotes (either single or double quotes) with an
# 'f' prefix before the opening quote. Inside the f-string, you can include expressions within curly braces {}.
# These expressions are evaluated and replaced with their corresponding values when the string is constructed.
Using 'for' loop:
Output: 5 multiplied by 2 is: 10
Output: 10 multiplied by 2 is: 20
Output: 15 multiplied by 2 is: 30
Output: 20 multiplied by 2 is: 40
Output: 25 multiplied by 2 is: 50
Step 3: Using a 'while' loop
numbers = [5, 10, 15, 20, 25]
numbers = [5, 10, 15, 20, 25]
print("\nUsing 'while' loop:")
index = 0
while index "use leass than symbol" len(numbers):
number = numbers[index]
result = number * 2
print(f"{number} multiplied by 2 is: {result}")
index += 1
# So, index += 1 is equivalent to writing index = index + 1, but the former is more concise and widely used in Python.
Output: Using 'while' loop:
Output: 5 multiplied by 2 is: 10
Output: 10 multiplied by 2 is: 20
Output: 15 multiplied by 2 is: 30
Output: 20 multiplied by 2 is: 40
Output: 25 multiplied by 2 is: 50
Step 4: Using 'if', 'elif', and 'else' statements
numbers = [5, 10, 15, 20, 25]
print("\nClassifying numbers:")
for number in numbers:
if number % 2 == 0:
print(f"{number} is even.")
elif number % 3 == 0:
print(f"{number} is divisible by 3 but not even.")
else:
print(f"{number} is not even and not divisible by 3.")
Output: Classifying numbers:
Output: 5 is not even and not divisible by 3.
Output: 10 is even.
Output: 15 is divisible by 3 but not even.
Output: 20 is even.
Output: 25 is not even and not divisible by 3.
Whether you're a complete beginner or a seasoned programmer, this series is designed to help you learn Python in just one week! 📚 Don't miss out on this fantastic opportunity to elevate your coding skills and join our vibrant community of learners. See you at the quiz! 🎉
LOOPS IN PYTHON: for, while, if, elif
# Step 1: Initialize a list of numbers
numbers = [5, 10, 15, 20, 25]
Step 2: Using a 'for' loop
print("Using 'for' loop:")
for number in numbers:
# This line starts the 'for' loop. The loop will iterate through each element in the numbers list.
# During each iteration, the current element in the list will be assigned to the loop variable number.
result = number * 2
# Inside the 'for' loop, this line multiplies the current element number by 2 and stores the result in the variable result.
print(f"{number} multiplied by 2 is: {result}")
# F-strings (also known as formatted string literals) are a feature introduced in Python 3.6
# that provides a concise and convenient way to format strings.
# They make it easier to embed variables and expressions inside strings without the need for
# complicated string concatenation or formatting functions.
# The f-string is enclosed in the outermost quotes (either single or double quotes) with an
# 'f' prefix before the opening quote. Inside the f-string, you can include expressions within curly braces {}.
# These expressions are evaluated and replaced with their corresponding values when the string is constructed.
Using 'for' loop:
Output: 5 multiplied by 2 is: 10
Output: 10 multiplied by 2 is: 20
Output: 15 multiplied by 2 is: 30
Output: 20 multiplied by 2 is: 40
Output: 25 multiplied by 2 is: 50
Step 3: Using a 'while' loop
numbers = [5, 10, 15, 20, 25]
numbers = [5, 10, 15, 20, 25]
print("\nUsing 'while' loop:")
index = 0
while index "use leass than symbol" len(numbers):
number = numbers[index]
result = number * 2
print(f"{number} multiplied by 2 is: {result}")
index += 1
# So, index += 1 is equivalent to writing index = index + 1, but the former is more concise and widely used in Python.
Output: Using 'while' loop:
Output: 5 multiplied by 2 is: 10
Output: 10 multiplied by 2 is: 20
Output: 15 multiplied by 2 is: 30
Output: 20 multiplied by 2 is: 40
Output: 25 multiplied by 2 is: 50
Step 4: Using 'if', 'elif', and 'else' statements
numbers = [5, 10, 15, 20, 25]
print("\nClassifying numbers:")
for number in numbers:
if number % 2 == 0:
print(f"{number} is even.")
elif number % 3 == 0:
print(f"{number} is divisible by 3 but not even.")
else:
print(f"{number} is not even and not divisible by 3.")
Output: Classifying numbers:
Output: 5 is not even and not divisible by 3.
Output: 10 is even.
Output: 15 is divisible by 3 but not even.
Output: 20 is even.
Output: 25 is not even and not divisible by 3.
Whether you're a complete beginner or a seasoned programmer, this series is designed to help you learn Python in just one week! 📚 Don't miss out on this fantastic opportunity to elevate your coding skills and join our vibrant community of learners. See you at the quiz! 🎉