Practical of Cs 23
Practical of Cs 23
message-input("enter a welcome
message:")
print (message)
Output:
enter a welcome messagewelcome to
python world
x=int(input("enter a number:"))
if x>y:
print('larger number:',x)
else:
print('larger number:',y)
Output:
enter a number: 70
enter another number:90
larger number: 90
Program:3
#Input three numbers and display the
largest / smallest number
else:
larger number 98
Program:4(a)
''Generate the following patterns using
nested loops
‘’’Generate the following patterns using
nested loops
*
**
***
****
*****’’’
print("",end='')
12345
1234
123
12
1'''
n=5
print(j,end='')
print()
Output:
12345
1234
123
12
1
Program :4 (c)
'''Generate the following patterns using
nested loops
A
AB
ABC
ABCD
ABCDE'''
print()
Output:
A
AB
ABC
ABCD
ABCDE
Program:5(a)
'''Write a program to input Write a program
to input the value of x and n and print the
sum of the following series:
1 + + 2+ 3 + x4 + n'''
x=int(input("enter a value for x:"))
n=int(input("enter a value for n:"))
s=0
for i in range(n+1):
k=x**i
s=s+k
print("for x:",x,"for n;", n,"that is equal to:",s)
output:
enter a value for x:2
enter a value for n:5
for x: 2 for n; 5 that is equal to: 63
Program:5(b)
'''Write a program to input the value of x and
n and print the sum of the following series:
1− + 2− 3+ 4− n'''
import math
x=int(input("enter a value for x:"))
n=int(input("enter a value for n:"))
s=0
j=1
for i in range(n+1):
j=math.pow(-1,i)
k=math.pow(x,i)
k=k*j
s+=k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x:2
enter a value for n:5
for x: 2 for n 5 that is equal to: -21.0
Program:5 (c)
'''Write a program to input the value of x and
n and print the sum of the following series
+ 2/2+ 3/3+ 4/4+ /n'''
import math
x=int(input("enter a value for x"))
n=int(input("enter a value for n"))
s=x
j=0
if n==1:
print("for x:",x,"for n",n, "that is equal to:",s)
else:
for i in range(2,n+1):
j=(-1)**i
k=(j)(x*i)/i
k=k*j
s=s+k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x2
enter a value for n8
for x: 2 for n 8 that is equal to:
78.01904761904763
Program:5(d)
‘’’Write a program to input the value of x
and n and print the sum of the following
series: + 2/ 2! + 3/ 3! + 4/ 4! + /
!’’’
x=int(input("enter a value for x"))
n=int(input("enter a value for n"))
s=x
j=0
factorial=1
if n==1:
print("for x:",x,"for n",n, "that is equal to:",s)
else:
for i in range(2,n+1):
factorial=factorial*i
j=(-1)**i
k=(j)(x*i)/i
k=k*j
s=s+k
print("for x:",x,"for n",n, "that is equal to:",s)
output:
enter a value for x3
enter a value for n9
for x: 3 for n 9 that is equal to:
3526.4035714285715
Program:6
number = int(input("Enter a number: "))
divisors_sum = 0
for i in range(1, number):
if number % i == 0:
divisors_sum += i
if divisors_sum == number:
print(f"{number} is a Perfect number.")
else:
print(f"{number} is not a Perfect
number.")
num_str = str(number)
power = len(num_str)
armstrong_sum = 0
for digit in num_str:
armstrong_sum += int(digit) ** power
if armstrong_sum == number:
print(f"{number} is an Armstrong
number.")
else:
print(f"{number} is not an Armstrong
number.")
# Output results
if is_palindrome:
print(f"The string '{string}' is a
palindrome.")
else:
print(f"The string '{string}' is not a
palindrome.")
print(f"The string with case converted:
'{converted_string}'")
Program:13
'''Input a list of numbers and swap
elements at the even location with the
elements at
the odd location'''
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
for i in range(0, len(numbers) - 1, 2
numbers[i], numbers[i + 1] = numbers[i + 1],
numbers[i]
print(numbers)
For the input list[1,2,3,4,5,6,7,8]
Output>>>
[2, 1, 4, 3, 6, 5, 8, 7]
Program:14
'''Input a list/tuple of elements, search for a
given element in a list'''
# Input list or tuple
elements = [1, 2, 3, 4, 5, 6, 7]
search_element = 4
Program :15
#Create a dictionary with the roll number,
name and marks of n students in a class
and display the names of students who
have marks above 75
student data
for i in range(n):
print (student_data)
print (details['name'])
Output:
Enter the number of students: 4
Enter roll number for student: 1