Python Tutorial

Week - 2

1. Print the below triangle using for loop.

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

Answer:-


def numpattern(n):                     # Function to demonstrate printing pattern of numbers
        num = 5                        # initializing starting number
       for i in range(0, n):           # outer loop to handle number of rows
       num = 5                         # re- assigning num
                                       # Inner loop to handle number of columns
            for j in range(0, i-1):
            print(num, end="")         # printing number
            num = num - 1              # incrementing number at each column
       print("\r")                     # ending line after each row
# Driver code
n = 5
numpattern(n)

Output:

5

4 4

3 3 3

2 2 2 2

1 1 1 1 1

2. Write a program to check whether the given input is digit or lowercase character or uppercase character or a special character (use 'if-else-if' ladder)

Answer:

  • Capital letter Alphabets (A-Z) lie in the range 65-91 of the ASCII value
  • Small letter Alphabets (a-z) lie in the range 97-122 of the ASCII value
  • Any other ASCII value is a non-alphabetic character.

Method-1:


    def check(ch):
      if (ch >= 'A' and ch <= 'Z'):
        print(ch,"is an UpperCase character");
     elif (ch >= 'a' and ch <= 'z'):
        print(ch,"is an LowerCase character");
     else:
        print(ch,"is not an alphabetic character");

    # Driver Code
    ch = 'A';  # Get the character
    check(ch); # Check the character
    ch = 'a';  # Get the character
    check(ch); # Check the character
    ch = '0';  # Get the character
    check(ch); # Check the character

 

Output:

A is an UpperCase character

a is an LowerCase character

0 is not an alphabetic character

Method -2:


def check(ch):
    if ch.isupper():
        print(ch, "is an upperCase character")
    elif ch.islower():
        print(ch, "is a lowerCase character")
    else:
        print(ch, "is not an alphabetic character")

    # Driver Code
    if __name__ == '__main__':
    ch = 'A'
    check(ch) # Check the character
    ch = 'a' # Get the character
    check(ch) # Check the character
    ch = '0' # Get the character
    check(ch) # Check the character


Output:

A is an UpperCase character

a is an LowerCase character

0 is not an alphabetic character

3. Python Program to Print the Fibonacci sequence using while loop

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.

Answer:-


nterms = int(input("How many terms? "))
n1, n2 = 0, 1      					# first two terms
count = 0
if nterms <= 0:  					# check if the number of terms is valid
   print("Please enter a positive integer")
elif nterms == 1:					# if there is only one term, return n1
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:							# generate fibonacci sequence
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       n1 = n2						# update values
       n2 = nth
       count =count+ 1

     


Output:

How many terms? 12

Fibonacci sequence:

0

1

1

2

3

5

8

13

21

34

55

89

4. Python program to print all prime numbers in a given interval (use break)

Answer:


starting_value = int(input (" Enter the starting  Value: "))  
ending_value = int(input ("Enter the ending  Value: "))  
print ("The Prime Numbers in the range are: ")  
for number in range (starting_value, ending_value + 1):  
    if number > 1:  				# all prime numbers are greater than 1
        for i in range (2, number):  
            if (number % i) == 0:  
                break  
        else:  
            print (number)  


Output:

Enter the Starting Range Value: 10

Enter the Ending Range Value: 50

11

13

17

19

23

29

31

37

41

43

47

Method:-2


lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1): 
   if num > 1:                  				# all prime numbers are greater than 1
        for i in range(2, num):
           if (num % i) == 0:
               break
       else:
           print(num)