Python For Loop Practice Exercises
Python For Loop Practice Exercises
To print a star pattern using a for loop in Python, you can nest two loops: one for rows and one for columns or stars per row. The outer loop controls the number of rows, and the inner loop controls the number of stars. For example, a simple triangular pattern can be achieved as follows: ```python for i in range(1, 6): for j in range(i): print('*', end='') print('') ``` This pattern increases the number of stars printed in each row, starting from 1 up to 5. The inner loop prints the stars on the same line, while the outer loop moves to the next line after each row.
To print the multiplication table of a number using a for loop in Python, iterate from 1 to a specified range, multiplying the given number by the loop index, and print each result. Here’s how: ```python number = 5 # Example number for i in range(1, 11): print(f"{number} x {i} = {number * i}") ``` This code iterates 10 times (or as specified), multiplying 'number' by each value from 1 to 10, outputting the product, effectively demonstrating a multiplication table.
To calculate the factorial of a number using a for loop in Python, start by initializing a 'result' variable to 1. Use a for loop to iterate over a range from 1 to the given number (inclusive), multiplying 'result' by each number in the range. For example: ```python def factorial(number): result = 1 for i in range(1, number + 1): result *= i return result print(factorial(5)) # Outputs: 120 ``` This approach multiplies each integer up to the given number to compute the factorial.
To calculate the sum of the first N natural numbers using a for loop in Python, you can initialize a variable, like 'sum', to 0. Then, use a for loop to iterate over a range from 1 to N+1, adding each number to 'sum'. For example: ```python N = 10 # this is the given N sum = 0 for number in range(1, N + 1): sum += number print("Sum of the first", N, "natural numbers is", sum) ``` This program initializes 'sum' and iterates through the range, accumulating the sum of numbers up to N.
To print even numbers between 1 and 50 using a for loop in Python, iterate through the numbers from 1 to 50. Within the loop, use an if statement to check if each number is divisible by 2 without a remainder, indicating it is even, then print it. For example: ```python for number in range(1, 51): if number % 2 == 0: print(number) ``` This program uses the modulo operator to identify even numbers within the specified range, printing each even number found.
To count the number of vowels in a string using a for loop, iterate over each character in the string checking if it is a vowel, using a list or set of vowels for comparison. Increment a counter for each vowel found. Here’s how it can be done: ```python def count_vowels(input_string): vowels = 'aeiouAEIOU' count = 0 for char in input_string: if char in vowels: count += 1 return count print(count_vowels("hello world")) # Outputs: 3 ``` This method efficiently counts vowels by leveraging direct membership testing in the vowels string.
To print all elements of a list using a for loop in Python, first create a list. Then, use a for loop to iterate over the list, printing each element. For example: ```python my_list = ['apple', 'banana', 'cherry'] for item in my_list: print(item) ``` This loop goes through each item in 'my_list' and prints it. This approach works for lists of any size and type of content, effectively using the in-built capabilities of Python for iterating through lists.
To check if a number is prime using a for loop in Python, start by checking if the number is less than 2, as these are not prime. For numbers 2 and greater, iterate using a for loop from 2 to the square root of the number. If the number is divisible evenly by any of these, it is not prime. Otherwise, it is prime. Here’s a basic implementation: ```python def is_prime(number): if number <= 1: return False for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True num = 29 print(is_prime(num)) # Outputs: True ``` This function checks divisibility up to the square root for efficiency, returning false if a divisor is found.
To reverse a string using a for loop in Python, you can iterate over the string in reverse order and build a new string. Here’s how you can do it: ```python original_string = "hello" reversed_string = "" for char in range(len(original_string) - 1, -1, -1): reversed_string += original_string[char] print("Reversed String:", reversed_string) ``` This program initializes 'reversed_string' as an empty string, iterates over 'original_string' from the end to the start, and appends each character to 'reversed_string', effectively reversing it.
To write a Python program that loops over numbers from 1 to 10, use the 'range' function in a for loop to iterate from 1 to 10, printing each number. Here is a simple example: ```python for number in range(1, 11): print(number) ``` This uses the range function to create an iterator that produces numbers from 1 to 10, ensuring they are printed line-by-line with each iteration.