Print the alphabet pattern ‘O’

In this python program, we will print the alphabet pattern ‘O’, with a special character *. pattern printing programs are very helpful to get expertise on loop fundamentals and flow design.

Please refer to the image, we consider a square of 7×7 size, which means 7 rows and 7 colons, Now in this square, the circle marked as green will be part of the ‘0’ pattern.

Steps to solve the program
o pattern2

1).  In this program have considered the matrix of 7*7 and where each row colon’s initial index position is zero as shown in the image.
2). In the code first loop act and the row and nested loop act as colons to print the pattern.
3). First loop value range is 0-6 and the nested loop range is the same 0-6.
4). First If condition makes sure only three * will print in the first and last row.
5). Second if the condition makes sure only two * should print at 1 and 5 indexes of the colon, for remaining 5 rows. 

 

				
					for row in range(0, 7):
        for column in range(0, 7):
            # here in first and last row we want to three *
            if (row == 0 or row == 6) and (1 < column < 5) :
                print("*", end=' ')
            # here from 2 to 6 row, * will print on 1 and 5 index only.
            elif (0 < row <= 5) and (column ==1 or column ==5):
                print("*", end=' ')
            else:
                print(" ", end=' ')
        print()
  
				
			

Output :

				
					---------

   ***
  *   *
  *   *
  *   *
  *   *
  *   *
   ***
   
---------   
				
			

accepts a string and calculates the number of digits and letters

print all natural numbers from 1 to n

Calculate the number of digits and letters

In this program, we will accept a string and calculate the number of digits and letters.

Steps to solve the program
  1. Take a word as input.
  2. Create two variables to calculate the number of digits and letters and assign their values equal to 0.
  3. Use for loop to iterate over each character from the word.
  4. Check whether the character is a letter or a digit using isaplha() and isnumeric().
  5. Add 1 to the corresponding variable according to the type of the charater.
  6. Print the output.
Solution 1
				
					word = "python1234"
digit = 0
character = 0

for ele in word:
    if ele.isalpha():
        character += 1
    elif ele.isnumeric():
        digit += 1
        
print("Digits :",digit)
print("Characters: ",character)
				
			
Output
				
					Digits : 4
Characters:  6
				
			
Solution 2
				
					inputstr = 'pythons12345'
digits = '0123456789'
letters = ''
# create small case and capital case sequence with ASCII
for i in range(1, 27):
    letters = letters + chr(65+i) + chr(97+i)

digit_count = 0
letter_count = 0
# iterate over each character with loop
for word in inputstr:
    # check if word is digit and increase digit count
    if word in digits:
        digit_count += 1
    # check if word is letters and increase letter count
    elif word in letters:
        letter_count += 1


print("Digit Count :", digit_count)
print("Letter Count :", letter_count)

				
			
Output
				
					Digits Count : 5
Letter Count : 7
				
			

converts all uppercases in the word to lowercase

print the alphabet pattern ‘O’

Convert all uppercase to lowercase in a word

In this program, we will take a word as input from the user and convert all uppercase to lowercase in a word.

Steps to solve the program
  1. Take a word as input from the user.
  2. Use for loop to iterate over each character of the word.
  3. Check whether the character is uppercase or not using isupper().
  4. If it is an uppercase character convert it to lowercase using lower().
  5. Print the output.
Solution 1 :
				
					input1 = input("Enter word: ")
result = ''
for char in input1:
    if char.isupper():
        print(char.lower(),end="")
    else:
        print(char,end="")
				
			
Output :
				
					Enter word: SQATOOLS
sqatools
				
			
Solution 2 :
				
					str1 = input("Enter input string:")
result = ""
for char in str1:
    # get character ascii value 
    char_ascii = ord(char)
    # check ascii value between 65 - 90 range
    # then character is in upper case
    if 65 <= char_ascii <= 90:
        # get character exact location
        char_position = char_ascii - 65
        # convert upper char ascii to lower case.
        # add character to the result
        result = result + chr(97+char_position)
    else:
        result = result + char

print("Result :", result)
				
			
Output
				
					Enter Input String: SQATOOLS Learning
Result : sqatools learning
				
			

Fizz-Buzz Program

accepts a string and calculates the number of digits and letters

Print Fizz Buzz for multiples of certain numbers

In this program, we will print Fizz Buzz and FizzBuzz for multiples of certain numbers.

Steps to solve the program
  1. Use for loop with the range function to iterate over all the numbers from 1 to 30.
  2. If a number is multiple of 3 and 5 then print FizzBuzz.
  3. If a number is multiple of 3 print Fizz.
  4. If a number is multiple of 5 print Buzz.
				
					#Printing output
for i in range(1,31):
    if i%3 == 0 and i%5 == 0:
        print("FizzBuzz")
    elif i%3 == 0:
        print("Fizz")
    elif i%5 == 0:
        print("Buzz")
				
			

Output :

				
					Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
				
			

program to get the Fibonacci series

converts all uppercases in the word to lowercase

Get the Fibonacci series between 0 to 20.

In this program, we will get the Fibonacci series between 0 to 20.

Steps to solve the program
  1. Create two variables and assign their values equal to 0 and 1 respectively.
  2. Create another variable count and assign its value equal to 0.
  3. While the count variable is less than 20 print the num1.
  4. Add num1 and num2 and assign their addition to the n2 variable.
  5. Change the value of num1 with num2 and num2 with n2.
  6. Add 1 to the count variable.
  7. Print the Fibonacci series.
				
					num1 = 0
num2 = 1
count = 0

while count < 20:
    print(num1,end=" ")
    n2 = num1 + num2
    num1 = num2
    num2 = n2
    count += 1  
				
			

Output :

				
					0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 
				
			

prints all the numbers from 0 to 6 except 3 and 6

Fizz-Buzz program

Print all numbers except certain numbers.

In this program, we will print all the numbers except certain numbers.

Steps to solve the program
  1. Use for loop to print all the numbers from 1 to 10.
  2. If the number is not equal to 3 or 6 only then print the number.
				
					for i in range(0,11):
        if i != 3 or i != 6:
            print(i,end=" ")
				
			

Output :

				
					0 1 2 4 5 7 8 9 10 
				
			

count the number of even and odd numbers from a series of numbers

program to get the Fibonacci series

Count the number of even and odd numbers

In this program, we will count the number of even and odd numbers from a series of numbers.

Steps to solve the program
  1. Take a series of numbers as input.
  2. Create two variables even and odd and assign their value equal to 0.
  3. Use for loop to iterate over each number from the series.
  4. If the number is even add 1 to the even and if the number is odd add 1 to the odd.
  5. Print the output.
				
					numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9)
even = 0
odd = 0

for val in numbers:
    if val%2 == 0:
        even += 1
    else:
        odd += 1
        
print("Number of even numbers: ",even)
print("Number of odd numbers: ",odd)
				
			

Output :

				
					Number of even numbers:  4
Number of odd numbers:  5
				
			

add the word from the user to the empty string

prints all the numbers from 0 to 6 except 3 and 6

Construct the following pattern.

In this program, we will construct the following star pattern.

   *
   * *
   * * *
   * * * *
   * * * * *
   * * * *
   * * *
   * *
   *

Steps to solve the program
  1. Use for loop with range function to print the first 5 rows of the pattern.
  2. Use for loop with range function but in reverse order to print the last 4 rows of the pattern.
  3. Use -1 in the range function to print the pattern in reverse order.
				
					for i in range(6):
    print(i*"*")
for i in range(4,-1,-1):
    print(i*"*")
				
			

Output :

				
					
*
**
***
****
*****
****
***
**
*
				
			

numbers which are divisible by 7 and multiple of 5

add the word from the user to the empty string

Find numbers divisible by a certain number

In this program, we will find those numbers divisible by a certain number and a multiple of another number.

Steps to solve the program
  1. Use for loop with range function to iterate over all the numbers from 1500 to 2700.
  2. Using an if statement check if the number is divisible by 7 and a multiple of 5.
  3. Print such numbers
				
					for i in range(1500,2701):
        if i%7 == 0 and i%5 == 0:
            print(i, end=" ")
				
			

Output :

				
					1505 1540 1575 1610 1645 1680 1715 1750 1785 1820 1855 1890 1925 1960 
1995 2030 2065 2100 2135 2170 2205 2240 2275 2310 2345 2380 2415 2450 
2485 2520 2555 2590 2625 2660 2695 
				
			

construct the following pattern

Python Loops Programs, Exercises

1). Write a Python loops program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included).
Input1:1500
Input2:2700

2). Python Loops program to construct the following pattern, using a nested for loops.
Output :
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

3). Python Loops program that will add the word from the user to the empty string using python.
Input: “python”
Output : “python”

4). Python Loops program to count the number of even and odd numbers from a series of numbers using python.
Input : (1, 2, 3, 4, 5, 6, 7, 8, 9)
Output :
Number of even numbers: 4
Number of odd numbers: 5

5). Write a program that prints all the numbers from 0 to 6 except 3 and 6 using python.

6). Write a program to get the Fibonacci series between 0 to 20 using python.
Fibonacci Series : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

7). Write a program that iterates the integers from 1 to 30 using python. For multiples of three print “Fizz” instead of the number and for multiples of five print “Buzz”.
For numbers that are multiples of both three and five print “FizzBuzz”. 

8). Write a program that accepts a word from the user and converts all uppercases in the word to lowercase using python.
Input : “SqaTOOlS”
Output : “sqatools”

9). Python loops program that accepts a string and calculates the number of digits and letters using python.
Input : “python1234”
Output :
Letters 6
Digits 4

10). Python for loop program to print the alphabet pattern ‘O’ using python.
Output:
  ***  
*       *
*       *
*       *
*       *
*       *
   ***  

11). Python Loops program to print all natural numbers from 1 to n using a while loop in python.

12). Write a program to print all natural numbers in reverse (from n to 1) using a while loop in python.

13). Python Loops program to print all alphabets from a to z using for loop
        Take chr method help to print characters with ASCII values
        chr(65) = ‘A’
        A-Z ASCII Range  65-90
        a-z ASCII Range  97-122

14). Python Loops program to print all even numbers between 1 to 100 in python.

15). Python Loops program to print all odd numbers between 1 to 100 using python.

16). Python Loops program to find the sum of all natural numbers between 1 to n using python.

17). Python program to find the sum of all even numbers between 1 to n using python.

18). Python Loops program to find the sum of all odd numbers between 1 to n using python.

19). Write a program to count the number of digits in a number using python.

20). Write a program to find the first and last digits of a number using python.

21). Write a program to find the sum of the first and last digits of a number using python.

22). Write a program to calculate the sum of digits of a number using python.

23). Write a program to calculate the product of digits of a number using python.

24).Python loops program to enter a number and print its reverse using python.

25). Write a program to check whether a number is a palindrome or not using python loops.

26). Program to find the frequency of each digit in a given integer using Python Loops.

27). Python loops program to enter a number and print it in words.

28). Python loops program to find the power of a number using Python for loop. Take values as an input base number and power, and get the total value using a loop.

29). Python loops program to find all factors of a number using Python. Get all the numbers that can divide this number from 1 to n.
 

30). Write a program to calculate the factorial of a number using Python Loops.

31). Write a program to check whether a number is a Prime number or not using Python Loops.

32). Write a program to print all Prime numbers between 1 to n using Python Loops.

33). Python loops program to find the sum of all prime numbers between 1 to n using Python.

34). Python loops program to find all prime factors of a number using Python Loops.

35). Python loops program to check whether a number is an Armstrong number or not using Python Loops.

Armstrong Example : 153 = 1*1*1 + 5*5*5 + 3*3*3

36). Python loops program to print all Armstrong numbers between 1 to n using Python Loops.

Armstrong Example : 153 = 1*1*1 + 5*5*5 + 3*3*3

37). Write a program to check whether a number is a Perfect number or not using Python.
Get factors of any number and the sum of those factors should be equal to the given number. The factor of 28: 1, 2, 4, 7, 14, and their sum is 28.
38). Python loops program to print all Perfect numbers between 1 to n using Python. The factor of 28: 1, 2, 4, 7, 14, and their sum is 28.
39). Python loops program to print the Fibonacci series up to n terms using Python Loops.
0, 1, 1, 2, 3, 5, 8, 13, 21 …..n

40). Python loops program to print the multiplication table of any number using Python Loops.

41).  Python loops program to print the pattern T using Python Loops.
 
       Output :
 
       * * * * * * * * *
       * * * * * * * * *
               * * *
               * * *
               * * *
               * * *
               * * *
42).  Write a program to print number patterns using Python Loops.
 
     Output:
 
     1
     2   3
     4   5   6
     7   8   9   10
     11  12  13  14  15
     14  13  12  11
     10  9   8
     7   6
     5
43). Write a program to print the pattern using Python Loops.
 
    Output :
 
       *  *  *
    * * * * * *
    * *      * *
    * *      * *
    * * * * * *
    * * * * * *
    * *      * *
    * *      * *
    * *      * *
44). Write a program to print the pyramid structure using Python Loops.
 
    Output:
 
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
45). Write a program to count total numbers of even numbers between 1-100 using Python Loops.

Output = 50

46). Write a program to count the total numbers of odd numbers between 1-100 using Python Loops.
Output = 50

47). Write a program to get input from the user if it is a number insert it into an empty list using Python Loops.

Input :
L=[]
125python
Output = [1,2,5]

48). Write a program to get input from the user if it is a string insert it into an empty list using Python Loops.
Input :
L=[]
‘sqatools007’
Output = [‘s’,’q’,’a’,’t’,’o’,’o’,’l’,’s’]

49). Write a program to accept the kilometers covered and calculate the bill according to the following using Python Loops.
First 5 km- 15rs/km
Next 20 km- 12rs/km
After that- 10rs/km

50). Write a program to input electricity unit charges and calculate the total electricity bill according to the given condition using Python Loops.
For the first 50 units Rs. 0.50/unit.
For the next 100 units Rs. 0.75/unit.
For the next 100 units Rs. 1.25/unit.
For units above 250 Rs. 1.50/unit.
An additional surcharge of 17% is added to the bill.
Input = 350
Output = 438.75

51). Write a program to calculate the sum of all odd numbers between 1-100 using Python Loops.

Output = 2500

52). Find the numbers which are divisible by 5 in 0-100 using Python Loops.

53). Write a program to construct the following pattern, using a for loop in Python.
Output :
*
* *
* * *
* * * *
* * * * *

54). Write a program to construct the following pattern, using a for loop in Python.
Output :
* * * * *
* * * *
* * *
* *
*

55). Write a program to construct the following pattern, using a nested for loop in Python.
Output :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

56). Write a program to get the Fibonacci series between 0 to 10 using Python Loops.
Output = 0 1 1 2 3 5 8 13 21 34 55

57). Write a program to check the validity of password input by users using Python Loops.
At least 1 letter between [a-z] and 1 letter between [A-Z].
At least 1 number between [0-9].
At least 1 character from [$#@].
Minimum length 5 characters.
Maximum length 15 characters.
Input = Abc@1234
Output = Valid password

58). Write a program to check whether a string contains an integer or not using Python Loops.
Input = Python123
Output = True

59). Write a program to print the following pattern using Python Loops.
Output :
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

60). Write a program to print a table of 5 using for loop using Python Loops.
Output :
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

61). Write a program to print the first 20 natural numbers using for loop in Python.
Output = 1,2,3,….,20

62). Write a program to display numbers from a list using Python Loops.
Input = [1,5,8,0,4]
Output = 1,5,8,0,4

63). Write a program to print each word in a string on a new line using Python Loops.
Input = Sqatools
Output :
S
q
a
t
o
o
l
s

64). Write a program to create an empty list and add even numbers from 1-10 in it including 10 using Python Loops.
Input = []
Output :
[2,4,6,8,10]

65). Write a program to create an empty list and add odd numbers from 1-10 in it including 1 using Python.
Input = []
Output : [1,3,5,7,9]

66). Write a program to print the keys of a dictionary using Python Loops.
Input = {‘name’:’virat’,’sports’:’cricket’}
Output :
name
cricket

67). Write a program to print the values of the keys of a dictionary using Python.
Input = {name’:’virat’,’sports’:’cricket’}
Output :
Virat
Cricket

68). Write a program to print the keys and values of a dictionary using Python Loops
Input = {name’:’virat’,’sports’:’cricket’}
Output :
name Virat
sports cricket

69). Write a program to print the first 20 natural numbers using a while loop in Python.

70). Write a program to print a table of 2 using a while loop in Python.
Output :
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20

71). Find the sum of the first 10 natural numbers using the while loop in Python Loops.
Output = 55

72). Find the multiplication of the first 10 natural numbers using Python Loops.
Output = 3628800

73). Print numbers from 1-10 except 5,6 using a while loop in Python.
Output :
1
2
3
4
7
8
9
10

74). Write a program to print the days in a week except Sunday using a while loop in Python.
Output :
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

75). Write a program to find the total number of special characters in a string using Python Loops.
Input = ‘@sqa#tools!!’
Output = 4

76). Write a program to add even numbers in an empty list from a given list using Python Loops.
Input :
A=[2,3,5,76,9,0,16], B=[]
Output = [2,76,0,16]

77). Write a program to add odd numbers in an empty list from a given list using Python Loops.
Input :
A=[3,8,5,0,2,7], B=[]
Output = [3,5,7]

78). Write a program to add special characters in an empty list from a given list using Python Loops.
Input :
A=[s,2,4,6,a,@,!,%,#], B=[]
Output = [@,!,%,#]

79). Write a program to print the last element of a list using a while loop using Python Loops.
Input :
C=[s,q,a,t,o,o,l,s]
Output = s

80). Write a program to print 1-10 natural numbers but it should stop when the number is 6 using a while loop in Python.
Output :
1
2
3
4
5

81). Write a program to print 1-10 natural numbers but it should stop when the number is 6 using Python Loops
Output :
1
2
3
4
5

82). Write a program to count the total number of characters in a file using Python Loops.
Input :
(file.txt
Content= I’m learning python
At
Sqatools)
Output = 26

83). Write a program to find the total number of digits in a file using Python Loops.
Input :
(file.txt – file name
Content-
Virat Kohli scored 100 in the last match)
Output = 3

84). Write a program to find the total number of Uppercase letters in a file using Python Loops.
Input :
(file.txt – file name
Content-
Virat Kohli scored 100 in the last match)
Output = 2

85). Write a program to find the total number of special characters in a file using Python Loops.
Input :
(file.txt – file name
Content-
student@gmail.com
##comment)
Output = 3

86). Write a program to sort a list using for loop in Python Loops. 
Input = [6,8,2,3,1,0,5]
Output = [0,1,2,3,5,6,8]

87). Write a program to add elements from one list to another list and print It in descending order using Python Loops.
Input :
A=[2,5,8,0,1,4], B=[]
Output = [8,5,4,2,1,0]

88). Write a program to find the maximum number from the list using Python Loops
Input : [12,14,45,88,63,97,88]
Output : Maximum number: 97

89). Print the following camel letter pattern using Python Loops.
Output =
A
B C
D E F
G H I J
K L M N O
P Q R S
T U V
W X
Y

90). Print the following small alphabet letter pattern using Python Loops.
Output =
           a
       b c d
     e f g h i
   j k l m n o p
     q r s t u
       v w x
          y