Python for loop Beginner Projects

Python-for-loop-Beginner-Projects
Credit: Canva

Overview

This Python For loop beginner projects is designed for new or beginner programmers to solve a couple of Python For loop challenges. Python For loop is used to iterate over an item of list i.e., to access each element from the list individually.

What’s Next!

In this Python For loop beginner projects, we will cover beginner Python For loop projects to explore the “for loop” in detail and make a strong command of Python Arithmetic operators.

Table of Contents

Project 1: Times Table Generator

Create a program that generates times tables for a given number. The program should ask the user to input a number and then print the times table for that number up to a specified range (e.g., up to 10).

				
					x = int(input("Enter a number: "))
for y in range(1,11):
    print(x,'*',y,'=',x*y)
				
			

Explanation: The program first asks the user to enter a number. In the next step, ‘for loop’ is used with the range() function to iterate through each number until it reaches the last n-1, i.e., 11-1=10. When the program accesses the first number from the loop, the Python interpreter follows the print() function and executes the statement written inside print() function.

Example: Whatever the number is entered by the user, according to the code, the Python interpreter will execute the display of the print() function’s statement. For example, if a user enters the 2 as the number, the Python interpreter will display the Times Table of 2.

Let’s take another call in this same project, and this time we do not want to take input from the user, instead we define a variable to store the number.

				
					for num in range(2,3):
    for y in range(1,11):
        print(num,'*',y,'=',num*y)
				
			

Explanation: In this example, we will use the ‘for loop’ to define the number of Times Table Generator. So, the program starts with a ‘for loop’ with a range() function to iterate with numbers. After the first ‘for loop’ the Python interpreter reaches to the next ‘for loop’ and iterates with the number until it reaches n-1, i.e., 11-1=10. Next, the program accesses each number one by one from the loop, the Python interpreter follows the print() function and executes the statement written inside print() function.

Project 2: Sum of Numbers

Write a program that calculates the sum of all numbers from 1 to a specified number. The program should ask the user to input a number and then calculate and print the sum of all numbers from 1 to that number.

				
					num =  int(input("Enter a number: ")) 
sum = 0
for x in  range(1, num+1):
    sum = x+sum 
print(sum)
				
			

Explanation: The program starts with asking the number. A variable ‘sum’ is defined as an initial value of 0. As the ‘for loop’ iterates through the number, it starts adding to ‘x+sum’ and stores it in ‘sum’ to upgrade the value of ‘sum’. This process is followed by the Python interpreter until the ‘for loop’ completes iteration through each number in the range() function.

Finally, ‘for loop’ breaks down and the Python interpreter executes the print() function i.e., the last & upgraded value of ‘sum’.

Project 3: Pattern Printing

Develop a program that prints various patterns using numbers or symbols. For example, a program that prints a pyramid pattern.

				
					#number of lines
for i in range(1,6):
    #space
    for j in range(0,6-i-1): 
        print(" ",end="")
        #asterisk
    for j in range(0,2*i+1): 
        print("*",end="")
    print()
				
			

Explanation: The program starts with the first ‘for loop’ with the range() function. Next, the Python interpreter reaches the second ‘for loop’, which prints the number of spaces before the stars. Now, the Python interpreter is on the third ‘for loop’ which is used to print the number of stars after the spaces.

Now according to the first ‘for loop’ i.e., ‘i’, start iteration with number 1, therefore the current value of ‘i’ is 1. Next, the Python interpreter reaches the second ‘for loop’ i.e., ‘j’, right now the starting number of the range function is 0 and the ending number is solved using equation 6-i-1, where ‘i’ is the variable of the first loop which will be deducted every time with the equation and finally displays the number of spaces

Next, now Python Interpreter reaches the third ‘for loop’ and according to the range() function starting number is 0 but the ending is decided with equation 2*i+1, where ‘i’ is the value from the first ‘for loop’ and finally displays the number of asterisks.

Now according to the ‘for loop’ number of times spaces with the asterisk are displayed.

Let’s take one more example of Pattern Printing. This example will display the letter ‘E’. 

				
					for x in range(1):
    print("*"*10)

for i in range(0,4):
    print("*")
        
for y in range(1):
    print("*"*10)

for z in range(0,4):
    print("*")
       
for a in range(1):
    print("*"*10)

				
			

Explanation: The program starts with ‘for loop’, for the horizontal line in the letter ‘E’, we used for loop along with a range function to display the asterisk 10 times. Simply, we run the loop with the range of 1 and display the asterisk 10 times by multiplying the asterisk with 10.

Now, for the vertical line, we run for loop with a range function for 4 times starting from 0 to 4.

Project 4: Fibonacci Sequence

Create a program that generates the Fibonacci sequence up to a specified number of terms. The program should ask the user to input the number of terms and then print the Fibonacci sequence up to that number of terms.

				
					n = int(input("Enter the number of terms you want in Fibonacci sequence : "))
num = [0,1]
while len(num) < n:
    c =  num.append(num[-1] + num[-2])
print ("The Fibonacci sequence is:")

for x in  range (len(num)):
    print(num[x], end=" ")
				
			

Let’s first understand what is the Fibonacci sequence. A Fibonacci sequence is the number of series which is the sum of the last two numbers.

So, the Fibonacci sequence starts by default with 2 numbers, i.e., 0,1. To generate the next number, we need to access the last 2 numbers from the backward position. So, we add 1+0=1, i.e. next number is the series, now we have [0,1,1] in the Fibonacci sequence, now add again the last two numbers, 1+1=2, now we have [0,1,1,2] in the Fibonacci sequence and so on.

Explanation: The program starts by asking the user a number to define how many numbers, the sequence has to generate. Next, a variable defined num with a list of default Fibonacci sequences [0,1].

Now, set a condition that till a number of elements in the Fibonacci sequence < n, loop runs access the last two numbers from the list, and add them to the Fibonacci sequence list. Since we have to access the last two numbers, therefore; we used negative indexing and to add numbers to the list, we used the append() function.

Since the condition is inside the while loop therefore the Python interpreter follows the command until the while loop breaks by having the condition false i..e, number of elements in num > n.

Now, the Python interpreter reaches the for loop outside the while loop; in this, it displays each number in the Fibonacci sequence one by one by using the separator space.

Project 5: Factorial Calculator

Write a program that calculates the factorial of a given number. The program should ask the user to input a number and then calculate and print the factorial of that number using a for loop.

				
					num = int(input("Enter a number, you want factorial of: "))
factorial = 1

if num <= 0:
    print("Factorial is not defined for 0 and Negative number.")
else:
    for i in range(factorial,num+1): #1,2,3...n
        factorial= factorial*i #1*1, 1*2, 2*3, 6*4 ... n*n
    print("The factorial of ",num, "is",factorial)
				
			

Explanation: The program starts by asking for a number, the user wants the factorial. By default factorial is 1. Since we know no factorial of 0 and negative number, we set a condition that if num <= 0, there is no factorial number. 

Next, we moved to the else condition, we started with the range() function and accessed all numbers from the factorial to the num, i.e., all numbers from 1 to the number of factorial.

Now we access each number one by one and multiply with factorial. In each multiplication, the value of the factorial is updated.

Hence, when all numbers are multiplied, the last number is the factorial of the number entered by the user. For example: user enters = 5, then the factorial will be = 1*2*3*4*5 = 120.

Recent Articles