You are currently viewing Using loops and conditional statements together

Using loops and conditional statements together

Using loops and conditional statements together in Python can be powerful and flexible. You can use a loop to iterate over a sequence of data, and use conditional statements to execute different blocks of code based on certain conditions. Here’s an example:

# Loop over a list of numbers and print "even" or "odd" for each number
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
    if number % 2 == 0:
        print(number, "is even")
    else:
        print(number, "is odd")

In this example, the program loops over a list of numbers and uses a conditional statement (if number % 2 == 0) to check whether each number is even or odd. If the number is even, the program prints a message saying so; if the number is odd, the program prints a message saying so.

Another example could be to calculate the sum of all even numbers between 1 and 100:

# Calculate the sum of all even numbers between 1 and 100
total = 0

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

print("The sum of all even numbers between 1 and 100 is:", total)

In this example, the program uses a for loop to iterate over all the numbers between 1 and 100. For each number, the program uses a conditional statement to check whether the number is even (if number % 2 == 0). If the number is even, the program adds it to a running total (total += number). Finally, the program prints the total to the console.

I hope these examples help you understand how to use loops and conditional statements together in Python. Let me know if you have any other questions!

# Calculate the average of all numbers in a list that are greater than 10
numbers = [5, 12, 3, 17, 8, 21, 9]

total = 0
count = 0

for number in numbers:
    if number > 10:
        total += number
        count += 1

if count > 0:
    average = total / count
    print("The average of all numbers greater than 10 is:", average)
else:
    print("There are no numbers greater than 10 in the list")

In this example, the program loops over a list of numbers and uses a conditional statement to check whether each number is greater than 10 (if number > 10). If the number is greater than 10, the program adds it to a running total (total += number) and increments a counter (count += 1).

After the loop, the program uses another conditional statement to check whether any numbers in the list were greater than 10 (if count > 0). If there were, the program calculates the average (average = total / count) and prints it to the console. If there weren’t, the program prints a message saying so.

I hope this example helps you see how powerful and flexible loops and conditional statements can be in Python! Let me know if you have any other questions or if you’d like to see more examples.

# Create a new list with only the even numbers from an existing list
numbers = [5, 12, 3, 17, 8, 21, 9]

evens = []

for number in numbers:
    if number % 2 == 0:
        evens.append(number)

print("The even numbers in the list are:", evens)

In this example, the program loops over a list of numbers and uses a conditional statement to check whether each number is even (if number % 2 == 0). If the number is even, the program appends it to a new list called evens using the append() method.

After the loop, the program prints out the new list of even numbers.

This is a simple example, but it demonstrates how you can use loops and conditional statements together to perform a variety of tasks in Python. You could modify this code to create a new list with only odd numbers, for example, or to create a list of numbers that are greater than a certain value.

I hope this example helps, but let me know if you have any other questions or if you’d like to see more examples!

# Find the longest word in a sentence
sentence = "The quick brown fox jumped over the lazy dog"

longest_word = ""

for word in sentence.split():
    if len(word) > len(longest_word):
        longest_word = word

print("The longest word in the sentence is:", longest_word)

In this example, the program takes a string sentence and uses the split() method to create a list of individual words. Then, the program loops over each word in the list and uses a conditional statement to check whether the length of the current word is greater than the length of the current longest word (if len(word) > len(longest_word)). If it is, the program sets longest_word to the current word.

After the loop, the program prints out the longest word in the sentence.

This is another simple example, but it demonstrates how you can use loops and conditional statements together to perform more complex tasks. You could modify this code to find the shortest word in the sentence, for example, or to count the number of words with a certain length.

I hope this example helps, but let me know if you have any other questions or if you’d like to see more examples!

Leave a Reply