Python Program-Set 3(P21-P30)

21) White a program that takes two lists as input and returns the merged list.
Way1
# Example usage for append:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.append(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, [6, 7, 8, 9]]


Way2
# Example usage for extend:
first_list = [1, 2, 3, 4, 5]
second_list = [6, 7, 8, 9]
first_list.extend(second_list)
print(first_list)
--> [1, 2, 3, 4, 5, 6, 7, 8, 9]

way 3
list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

list1.extend(list2)
print(list1)

way 4
listone = [1,2,3]
listtwo = [4,5,6]

joinedlist = listone + listtwo

way 5
listone = [1,2,3]
listtwo = [4,5,6]
mergedlist = []
mergedlist.extend(listone)
mergedlist.extend(listtwo)

22)To print given number of *s in a row
if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print('*', end=' ')


Output:
Enter n value:4
* * * * 
Process finished with exit code 0
23) To print square pattern with * symbols

if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print('* '*n)

Output:
Enter n value:6
* * * * * * 
* * * * * * 
* * * * * * 
* * * * * * 
* * * * * * 
* * * * * * 

Process finished with exit code 0
24) To print square pattern with provided fixed digit.

Ex:

Enter n value:4
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4


if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print((str(n)+' ')*n)

Output: 
Enter n value:6
6 6 6 6 6 6 
6 6 6 6 6 6 
6 6 6 6 6 6 
6 6 6 6 6 6 
6 6 6 6 6 6 
6 6 6 6 6 6 

Process finished with exit code 0

25) To print square pattern with provided fixed digit in every row

Example:

Enter n value:6
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
6 6 6 6 6 6


if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print((str(i+1)+' ')*n)

output:
Enter n value:7
1 1 1 1 1 1 1 
2 2 2 2 2 2 2 
3 3 3 3 3 3 3 
4 4 4 4 4 4 4 
5 5 5 5 5 5 5 
6 6 6 6 6 6 6 
7 7 7 7 7 7 7 

Process finished with exit code 0
26) To print square pattern with fixed alphabet symbol

Example-Enter n value:4
X X X X
X X X X
X X X X
X X X X


if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print(('Z ')*n)

Output:
Enter n value:6
Z Z Z Z Z Z 
Z Z Z Z Z Z 
Z Z Z Z Z Z 
Z Z Z Z Z Z 
Z Z Z Z Z Z 
Z Z Z Z Z Z 

Process finished with exit code 0
27) To print square pattern with alphabet symbols

A A A A A A A
B B B B B B B
C C C C C C C
D D D D D D D
E E E E E E E
F F F F F F F
G G G G G G G


if __name__ == '__main__':
    n = int(input('Enter n value:'))
    for i in range(n):
        print((chr(65+i)+' ')*n)

Output:
Enter n value:5
A A A A A 
B B B B B 
C C C C C 
D D D D D 
E E E E E 

Process finished with exit code 0
28) To print square pattern with digits

1 2 3
1 2 3
1 2 3


if __name__ == '__main__':
    n = int(input('Enter No Of Rows:'))
    for i in range(n):
        for j in range(n):
            print(str(j + 1), end=' ')
        print()

output:
Enter No Of Rows:5
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
29) To print square pattern with alphabet symbols in dictionary order

A B C D
A B C D
A B C D
A B C D


if __name__ == '__main__':
    n = int(input('Enter No Of Rows:'))
    for i in range(n):
        for j in range(n):
            print(chr(65 + j), end=' ')
        print()

Output
Enter No Of Rows:5
A B C D E 
A B C D E 
A B C D E 
A B C D E 
A B C D E 
30)To print square pattern with digits in descending order

3 3 3
2 2 2
1 1 1


if __name__ == '__main__':
    n = int(input('Enter No Of Rows:'))
    for i in range(n):
        print((str(n - i) + ' ') * n)

Output:
Enter No Of Rows:5
5 5 5 5 5 
4 4 4 4 4 
3 3 3 3 3 
2 2 2 2 2 
1 1 1 1 1 
To print square pattern with alphabets in reverse of dictionary order

C C C
B B B
A A A


if __name__ == '__main__':
    n = int(input('Enter No Of Rows:'))
    for i in range(n):
        print((chr(64+n-i)+' ')*n)

Output
Enter No Of Rows:6
F F F F F F 
E E E E E E 
D D D D D D 
C C C C C C 
B B B B B B 
A A A A A A