67 Py
67 Py
______________________________________________________________________________
1))Write a python program to add two numbers.
______________________________________________________________________________ a
= 100
b = 50
print("the sum of" ,a+b) a = int(input("Enter
value of a:")) b = int(input("Enter value of
b:")) print("the sum is %d and %d =
%d"%(a,b,a+b))
-----------------------------------
OUTPUT
----------------------------------- the sum
of 150
Enter value of a:100 Enter
value of b:50 the sum is 100
and 50 = 150
______________________________________________________________________________
2))Write a python program to find quotient and remainder.
_____________________________________________________________________________
_ a = float(input("Enter value of a:")) b = float(input("Enter value of b:")) print("%.2f / %.2f = %.2f"
----------------------
OUTPUT
---------------------- Enter
value of a:12 Enter
value of b:5
12.00 / 5.00 = 2.40
12.00 mod 5.00 = 2.00
___________________________________________________________________________________
__
3) Write a python program to find type of objects in your system.
___________________________________________________________________________________
__
objects_to_check = [1, "Hello", [1, 2, 3], {’a’: 1, ’b’: 2}, (1, 2, 3), True, None]
{type(obj).__name__}")
------------------------------------------
OUTPUT
------------------------------------------
The type of 1 is: int
The type of Hello is: str
The type of [1, 2, 3] is: list
The type of {’a’: 1, ’b’: 2} is: dict
The type of (1, 2, 3) is: tuple
The type of True is: bool The
type of None is: NoneType
______________________________________________________________________________
4) Write a python program to read two numbers and display the greatest number
_____________________________________________________________________________
_ a=(input("enter value of a: ")) b=(input("enter value of b:
is an even number
enter a number:17 it
is an odd number
_____________________________________________________________________________
6) Write a python program to check whether a character is vowel or consonant
___________________________________________________________________________
__ c=str(input("enter a character:")) if
c==’a’ or c==’e’or c==’i’ or c==’o’ or c==’u’:
print("it is a vowel")
else: print("it is a
consonant")
----------------------
OUTPUT
----------------------
enter a character:n
it is a consonant
enter a character:i
it is a vowel
___________________________________________________________________________________
_
a=int(input("enter a:"))
b=int(input("enter b:"))
c=int(input("enter c:"))
if a>b and a>c: print("a
is largest")
elif b>a and b>c:
print("b is largest") else:
print("c is
largest")
------------------
OUTPUT
------------------
enter a:1 enter
b:2 enter c:3 c is
largest
___________________________________________________________________________________
_
---------------------------------
OUTPUT
---------------------------------
Enter the coefficient of x^2: 2
Enter the coefficient of x: 8
Enter the constant term: 3
The roots are real and distinct.
Root 1: -0.41886116991581024
Root 2: -3.58113883008419
___________________________________________________________________________________
_
range(1,num+1): a+=i;
print(a); -----------------
OUTPUT
------------------
enter a number:10
55
___________________________________________________________________________________
10) Write a python program to check leap year
__________________________________________________________________________________ _
fact *= i
fibonacci(num)
---------------------------------
OUTPUT
---------------------------------
Enter the number of terms: 10 Fibonacci series:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
______________________________________________________________________________
14) Write a python program to find gcd.
______________________________________________________________________________ def
gcd(a,b):
while b != 0:
a,b = b, a % b;
return a num1 = int(input("Enter first number : "))
of",num1,"and",num2,"is:" ,result)
-----------------------------------
OUTPUT
-----------------------------------
Enter first number : 3
Enter second number : 2
The GCD of 3 and 2 is: 1
______________________________________________________________________________
_ 15) Write a python program to find lcm.
______________________________________________________________________________
_
def gcd(a,b):
while b != 0: a,b = b, a % b; return a num1 =
(a *
-----------------------------------
OUTPUT
-----------------------------------
Enter a number : 18
The reversed number is : 81
______________________________________________________________________________ _
17) Write a python program to calculate power of a number.
______________________________________________________________________________
_ base = int(input("Enter the base : ")) exponent = int(input("Enter the exponent : ")) result
is : {result}")
-----------------------------------
OUTPUT
-----------------------------------
Enter the base : 2
Enter the exponent : 2
2 raised to the power of 2 is : 4
______________________________________________________________________________
_ 18) Write a python program to find binary value of a character.
______________________________________________________________________________
_ x = input("Enter a character: ") binary
"is", binary)
-----------------------------------
OUTPUT
----------------------------------- Enter
a character: n
Binary value of n is 0b1101110
___________________________________________________________________________________
_ ____
19) Write a python program to display two separate strings in single line continuously.
___________________________________________________________________________________
_ ____ str1 = input("Enter the first
string : ") str2 = input("Enter the
second string : ") while True:
print(str1,end="
")
print(str2, end=" ") break
--------------------------------------
OUTPUT
-------------------------------------- Enter the
first string : NIRALI
Enter the second string : CHAUDHARY NIRALI
CHAUDHARY
______________________________________________________________________________
_
print("number is palindrome")
else: print("number is not palindrome")
-----------------------------------
OUTPUT
-----------------------------------
Enter number : 181 number is
palindrome
for i in range(2,num): if
num%i==0:
prime=True break if prime:
print(num,"is not prime") else:
print(num,"is prime")
-----------------------------------
OUTPUT
---------------------------------enter a
number :11 11 is prime
enter a number :20
20 is not prime
______________________________________________________________________________
_ 22) Write a python program to display prime numbers between two intervals.
_______________________________________________________________________________ def
is_prime(num): if num<2:
return False
for i in range(2,int(num ** 0.5)+1): if
num%2==0: return
False return
True
def display(start,end): print(f"prime numbers between
{start} and {end} :") for num
in range(start,end+1):
if is_prime(num):
print(num,end=’ ’)
display(start,end)
-----------------------------------
OUTPUT
-----------------------------------
Enter the start of the interval: 1
Enter the end of the interval: 20 prime numbers
between 1 and 20 :
2 3 5 7 9 11 13 15 17 19
______________________________________________________________________________
_
23) Write a python program to check armstrong number. (eg. 13 + 33 + 53 = 153)
______________________________________________________________________________
_ num=int(input("enter a number :")) sum=0
temp=num while temp>0:
digit=temp%10 sum+=digit
** 3
temp=temp//10
if num==sum: print(num," is an
armstrong number")
else: print(num," is not an armstrong number")
-----------------------------------
OUTPUT
----------------------------------
enter a number :153 153 is an armstrong
number
-----------------------------------
OUTPUT
----------------------------------- enter
start :100
enter end :200 153
______________________________________________________________________________
_ 25) Write a python program to display factors of a number.
______________________________________________________________________________
_ def fact(num): factors=[] for i in range(1,num+1): if num % i==0: [Link](i) return
factors
27) Write a python program to make a simple calculator to add, subtract, multiply or divide
___________________________________________________________________________________
_ _________
-----------------------------------
OUTPUT
----------------------------------- Enter
first number : 1
Enter second number : 2
Enter operator (+, -, *, /) : +
Addition : 3.0
Enter first number : 2
Enter second number : 2
Enter operator (+, -, *, /) : Subtraction
: 0.0
OUTPUT
-----------------------------------------
1 16
12 16
123 16
1234 17
1
1234
5_____________________________________________________________________________
__ 15
----------------------------------------
OUTPUT
----------------------------------------
Enter number of rows to be printed : 6
123456
12345
1234
123
12
1
______________________________________________________________________________
_ 30) Write a python program to display inverted pyramid of descending numbers
______________________________________________________________________________
_ rows = int(input("Enter number of rows to be printed : ")) for
i in range(rows,0,-1): print("
"*(rows-i),end="") for
j in range(i,0,-1):
print(j,end=" ")
print()
-----------------------------------------
OUTPUT
-----------------------------------------
Enter number of rows to be printed : 6
654321
54321
4321
321
21
1
______________________________________________________________________________
_ 31) Write a python program to display inverted pyramid of the same digit
______________________________________________________________________________
_ rows = int(input("Enter the number of rows to be printed :
------------------------------------------------
OUTPUT
------------------------------------------------
Enter the number of rows to be printed : 8
Enter the digit to be printed : 6
66666666
6666666
666666
66666
6666
666
66
6
______________________________________________________________________________ _
32) Write a python program to display reverse pyramid of numbers
______________________________________________________________________________
_ def rev_pyr(n):
for i in range(n, 0, -1): print("
" * (n - i), end="") for
j in range(i, 0, -1):
print(j, end=" ")
print()
-----------------------------------
OUTPUT
-----------------------------------
rows = 5 rev_pyr(rows)
54321
4321
321
21
1
______________________________________________________________________________
_ 33) Write a python program to display inverted half pyramid number pattern
______________________________________________________________________________
_ rows=int(input("enter number of rows:")) for
print(i,end=" ")
print()
-----------------------------------
OUTPUT
----------------------------------- enter number
of rows:10
10 10 10 10 10 10 10 10 10 10
999999999
88888888
7777777
666666
55555
4444
333
221
______________________________________________________________________________
_ 34) Write a python program to display pyramid of natural numbers less than 10
______________________________________________________________________________
_ n=10 for i in range(1,n):
for j in
range(n-i):
print("",end=" ") for k
in range(1,i+1):
print(k,end=" ")
print()
-----------------------------------
OUTPUT
-----------------------------------
1
12
123
1234
12345
123456
1234567
12345678
123456789
______________________________________________________________________________
_ 35) Write a python program to display reverse pattern of digits from 10
______________________________________________________________________________
_ num = 10
for i in range(num,0,-1):
for j in range(i,0,-1):
print(j,end=" ")
print()
---------------------
OUTPUT
---------------------
10 9 8 7 6 5 4 3 2 1
987654321
87654321
7654321
654321
54321
4321
321
21
1
___________________________________________________________________________________
_
36) Write a python program to display connected inverted pyramid pattern of numbers
___________________________________________________________________________________
_ rows=int(input("enter number of rows:"))
for i in range(rows,0,-1):
for j in range(rows-i):
---------------------
OUTPUT
---------------------
Enter rows : 6
2
46
8 10 12
14 16 18 20
22 24 26 28 30
32 34 36 38 40 42
______________________________________________________________________________
_ 38) Write a python program to display pyramid of horizontal tables
______________________________________________________________________________
_ rows = int(input("Enter the number of rows for the horizontal table pyramid: ")) for
i in range(1,rows+1): for j in range(1, i+1): print(f"{j} x {i} = {i * j}",end="\t") print()
------------------------------------------------------------------
OUTPUT
------------------------------------------------------------------
Enter the number of rows for the horizontal table pyramid: 6
1x1=1
1x2=22x2=4
1x3=32x3=63x3=9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
___________________________________________________________________________
___ _ 39) Write a python program to display pyramid pattern of alternate numbers
______________________________________________________________________________
_ rows = int(input("Enter the number of rows for the alternate number pyramid: ")) num = 1 for i in
")
num+=2 print()
---------------------------------------------------------------
OUTPUT
---------------------------------------------------------------
Enter the number of rows for the alternate number pyramid: 6 1
35
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39 41
______________________________________________________________________________
_
40) Write a python program to display mirrored pyramid (right-angled triangle) pattern of numbers
______________________________________________________________________________
_ rows = int(input("Enter the number of rows for the mirrored pyramid: "))
for i in range(1,rows+1):
for j in range(1,i+1): print(j,end="
")
print() for i in
range(rows-1,0,-1):
for j in range(1,i+1):
print(j,end=" ")
print()
-------------------------------------------------------
OUTPUT
-------------------------------------------------------
Enter the number of rows for the mirrored pyramid: 6 1
12
123
1234
12345
123456
12345
1234
123
121
______________________________________________________________________________
_
41) Write a python program to display equilateral triangle with stars (asterisk symbol)
______________________________________________________________________________
_ rows = int(input("Enter number of rows to be printed : "))
----------------------------------------
OUTPUT
----------------------------------------
Enter number of rows to be printed : 6
*
**
***
****
*****
******
______________________________________________________________________________
_
42) Write a python program to display downward triangle pattern of stars
______________________________________________________________________________
_ rows = int(input("Enter number of rows to be printed : "))
for i in range(rows,0,-1):
print(" " * (rows-i),end=" ")
print("* " * i)
-----------------------------------------
OUTPUT
-----------------------------------------
Enter number of rows to be printed : 6
******
*****
****
***
**
*
______________________________________________________________________________
_
43) Write a python program to display pyramid pattern of stars
______________________________________________________________________________
----------------------------------------
OUTPUT
----------------------------------------
Enter number of rows to be printed : 6
*
**
***
****
*****
******
______________________________________________________________________________
_
44) Write a python program to display hourglass pattern program
______________________________________________________________________________
_ rows = int(input("Enter the number of rows : "))
for i in range(1,rows+1):
for k in range(1,i): print("",end="
")
for j in range(i,rows+1): print(j,end="
")
print() for i in
range(rows-1,0,-1):
-----------------------------------
OUTPUT
-----------------------------------
Enter the number of rows : 6
123456
23456
3456
456
56
6
56
456
3456
23456
123456
______________________________________________________________________________
_
45) Write a python program to display pascal’s triangle program
______________________________________________________________________________
_ n=5 for i in
range(1,n+1):
for j in range(0,n-i+1):
print("",end="")
C = 1 for j in range(1,i+1):
print("",C,sep="",end="")
C = C*(i-j)//j print()
-----------------------------------
OUTPUT
----------------------------------- 1
11
121
1331
14641
ASSIGNMENT-2
Question 1: Reverse a String # Reverse a
string input_str = "hello world" reversed_str =
input_str[::-1] print("Reversed String:",
reversed_str) # Output: Reversed String:
dlrow olleh
Question 14: Extract Alternate Elements from a Given List Using Slicing # Extract alternate
elements from a given list using slicing input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
alternate_elements = input_list[::2] # Extract elements at even indices
print("Alternate Elements:", alternate_elements) # Output: Alternate
Elements: [1, 3, 5, 7, 9]
Question 15: Replace a Sublist Within a List with Another Sublist Using Slicing # Replace a
sublist within a list with another sublist using slicing input_list = [1, 2, 3, 4, 5, 6]
replacement_list = [7, 8, 9] start_index = 2
end_index = 5 input_list[start_index:end_index] =
replacement_list print("Modified List:", input_list)
# Output: Modified List: [1, 2, 7, 8, 9, 6]
Question 16: Compute the Factorial of a Given Number Recursively # Compute the
factorial of a given number recursively def factorial(n): if n == 0 or n == 1:
return 1 else: return n * factorial(n
- 1)
# Example usage number
= 5 result = factorial(number)
print(f"Factorial of {number}: {result}")
# Output: Factorial of 5: 120
# Example usage
numbers = [1, 2, 3, 4, 5]
total_sum = sum_of_elements(numbers) print("Sum of
Elements:", total_sum)
# Output: Sum of Elements: 15
Question 22: Remove Duplicates from a List
# Remove duplicates from a list def
remove_duplicates(lst):
return list(set(lst))
# Example usage
dict1 = {'a': 1, 'b': 2} dict2 = {'c': 3, 'd': 4}
merged_dict = merge_dictionaries(dict1, dict2)
print("Merged Dictionary:", merged_dict)
# Output: Merged Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4} Question 27: Access Value
Associated with a Given Key in a Dictionary
# Access value associated with a given key in a dictionary def
access_dictionary_value(dictionary, key): return
[Link](key)
# Example usage
student_scores = {'Alice': 85, 'Bob': 92, 'Charlie': 78} score =
access_dictionary_value(student_scores, 'Bob') print("Score for 'Bob':",
score)
# Output: Score for 'Bob': 92
Question 28: Get All Keys from a Dictionary
# Get all keys from a dictionary def
get_dictionary_keys(dictionary): return
list([Link]())
# Example usage
original_dict = {'a': 1, 'b': 2} update_dict = {'c': 3, 'd': 4} updated_dict =
update_dictionary(original_dict, update_dict) print("Updated Dictionary:",
updated_dict)
# Output: Updated Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Question 30: Create a Dictionary with Keys as Numbers from 1 to N and Values as Squares
# Create a dictionary with keys as numbers from 1 to N and values as squares of the keys
def create_squared_dictionary(n): return {i: i**2 for i in range(1, n+1)}
# Example usage N
=4
squared_dict = create_squared_dictionary(N) print(f"Dictionary with Keys (1 to {N})
and Squared Values:", squared_dict) # Output: Dictionary with Keys (1 to 4) and
Squared Values: {1: 1, 2: 4, 3: 9, 4: 16}