/**/ Sanjeev Study Hub – Class 11 & 12 Notes, Question Papers, IP & CS Study Material

Tuesday, May 2, 2023

PYTHON PATTERN CODING

 PATTERN CODING (PYTHON)


# 1. Pyramid pattern of numbers

rows = 10

for i in range(1, rows + 1):

    for j in range(1, i + 1):

        print(j, end='  ')

    print(' ')

OUTPUT

1   

1  2   

1  2  3   

1  2  3  4   

1  2  3  4  5   

1  2  3  4  5  6   

1  2  3  4  5  6  7   

1  2  3  4  5  6  7  8   

1  2  3  4  5  6  7  8  9   

1  2  3  4  5  6  7  8  9  10   

# 2. Reverse pattern for loop from 7 to 1

rows = 7

n = 0

for i in range(rows, 0, -1):

    n += 1

    for j in range(1, i + 1):

        print(n, end=' ')

    print('\r')

OUTPUT

1 1 1 1 1 1 1 

2 2 2 2 2 2 

3 3 3 3 3 

4 4 4 4 

5 5 5 

6 6 

7 


# 3. Inverted Pyramid pattern with the same digit

rows = 7 #You can use any number like 8, 9, 4 etc.

num = rows

# reverse for loop

for i in range(rows, 0, -1):

    for j in range(0, i):

        print(num, end=' ')

    print("\r")

OUTPUT

7 7 7 7 7 7 7 

7 7 7 7 7 7 

7 7 7 7 7 

7 7 7 7 

7 7 7 

7 7 

7 

# 4. Another reverse number pattern (8 to 1)

rows = 8

for i in range(0, rows + 1):

    for j in range(rows - i, 0, -1):

        print(j, end=' ')

    print()

OUTPUT

8 7 6 5 4 3 2 1 

7 6 5 4 3 2 1 

6 5 4 3 2 1 

5 4 3 2 1 

4 3 2 1 

3 2 1 

2 1 

1 

# 5. Print other reverse number from 10 to 1

start = 1

stop = 2

current_num = stop

for row in range(2, 6):

    for col in range(start, stop):

        current_num -= 1

        print(current_num, end=' ')

    print("")

    start = stop

    stop += row

    current_num = stop

OUTPUT

1 

3 2 

6 5 4 

10 9 8 7 

# 6. Number triangle pattern

rows = 10

for i in range(1, rows):

    num = 1

    for j in range(rows, 0, -1):

        if j > i:

            print(" ", end=' ')

        else:

            print(num, end='  ')

            num += 1

    print(" ")

OUTPUT

                 1   

                1  2   

              1  2  3   

            1  2  3  4   

          1  2  3  4  5   

        1  2  3  4  5  6   

      1  2  3  4  5  6  7   

    1  2  3  4  5  6  7  8   

  1  2  3  4  5  6  7  8  9  

# 7. Pyramid pattern of stars in python


rows = 8

for i in range(0, rows):

    for j in range(0, i + 1):

       print("*", end=' ')

    print("\r")

OUTPUT

* 

* * 

* * * 

* * * * 

* * * * * 

* * * * * * 

* * * * * * * 

* * * * * * * * 








Friday, April 28, 2023

Python Programs

Python Programs

 # 1. Simple Addition, Subtraction, Multiplication and Division in Python by Input Method

a=int(input('Enter the First Number :'))

b=int(input('Enter the Second Number :'))

# For Addition

sum = a + b

print('Sum of two numbers : ', sum)

# For Subtraction

sub = a - b

print('Subtraction :', sub)

# For Multiplication

mul=a*b

print('Multiply a and b :', mul)

# For Division

div = a/b

print('Division :', div)

Output:

Enter the First Number :10

Enter the Second Number :5

Sum of two numbers :  15

Subtraction : 5

Multiply a and b : 50

Division : 2.0


# 2. WAP to obtain three numbers and print their sum.

x = int(input('Enter the first number :'))

y = int(input('Enter the second number :'))

z = int(input('Enter the third number :'))

sum = x+y+z

print('Sum of thre numbers are : ', sum)

Output:

Enter the first number :50

Enter the second number :40

Enter the third number :60

Sum of thre numbers are :  150

# 3. WAP to obtain length and breadth of a rectangle and calculate its area.

length = float(input('Enter the length :'))

breadth = float(input('Enter the breadth :'))

Area = length * breadth

print('Area of Rectangle :', Area)

Output:

Enter the length :5.5

Enter the breadth :7.8

Area of Rectangle : 42.9


# 4.WAP to calculate Body Mass Index (BMI) of a person.

weight = float(input('Enter the person weight in kg. : '))

height = float(input('Enter the person height in meters :'))

#Formula

BMI = weight/(height*height)

print('BMI of a person is :', BMI)

Output:

Enter the person weight in kg. : 50

Enter the person height in meters :1.8

BMI of a person is : 15.432098765432098

# 5. WAP to input a number and print its cube.

a = int(input('Enter any number :'))

cube = a*a*a

print('Cube of', a, 'is :', cube)

Output:
Enter any number :5
Cube of 5 is : 125

# 6. WAP to input two numbers and swap them.

a = int(input('Enter the first number :'))

b= int(input('Enter the second number :'))

print('Your Original number  :', a , b)

a, b = b, a

print('After swapping:', a, b)

Output:

Enter the first number :10

Enter the second number :20

Your Original number  : 10 20

After swapping: 20 10

# 6. WAP to find the average of five numbers.

a = int(input('Enter the first number :'))

b = int(input('Enter the second number :'))

c = int(input('Enter the third number :'))

d = int(input('Enter the fourth number :'))

e = int(input('Enter the fifth number :'))

avg = (a+b+c+d+e)/5

print('Average of five numbers :', avg)

Output:

Enter the first number :60

Enter the second number :65

Enter the third number :70

Enter the fourth number :80

Enter the fifth number :82

Average of five numbers : 71.4

# 7.Print Multiplication Table

def print_multiplication_table(n):

    for i in range(1, 11):

        for j in range(1, n+1):

            print(i*j, end='\t')

        print()

mul = int(input("Enter the number you want the table for :"))

print_multiplication_table(mul)

Output:

Enter the number you want the table for :10

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100





Class 10 Board Exam 2027: Important Chapter-Wise MCQs with Answers

💻 CBSE Class 10 Computer Applications Chapters: Images, Links, Tables, Forms & Frames (30 MCQs) ...