Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views

Python File

Here is a program that takes a full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is: #code name = input("Enter your full name: ").split() first = name[0] middle = name[1] last = name[2] print(first[0] + ". " + middle[0] + ". " + last) OUTPUT: Enter your full name: Robin Ramesh Kuma R. R. Kuma

Uploaded by

kumarjass5251
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Python File

Here is a program that takes a full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is: #code name = input("Enter your full name: ").split() first = name[0] middle = name[1] last = name[2] print(first[0] + ". " + middle[0] + ". " + last) OUTPUT: Enter your full name: Robin Ramesh Kuma R. R. Kuma

Uploaded by

kumarjass5251
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2028202 Subject code-BTCS-513-18

2. Write a program to perform different Arithmetic Operations on


numbers in Python.
#code
d=int(input('Enter first number : ')) # enter float value
e=int(input('Enter second number : ')) # enter float value
#sum
print(f'Sum = {d+e}')
#sub
print(f'Difference = {d-e}')
#multiply
print(f'Multiplication = {d*e}')
#division
print(f'Division = {d/e}')
# floor division
print(f'Floor Division = {d//e}')
# modulus
print(f'Modulus = {d%e}')
# Exponent
print(f'Exponent = {d**e}')

OUTPUT:
Enter first number : 2
Enter second number : 3
Sum = 5
Difference = -1
Multiplication = 6
Division = 0.6666666666666666
Floor Division = 0
Modulus = 2
Exponent = 8
2028202 Subject code-BTCS-513-18
3. Write a program to create, concatenate and print a string and accessing
substring from a given string.
while(1):
print("select the option")
print("1.creating a string")
print("2.concatenate a string")
print("3.printing a string")
print("4.accessing a substring")
print("5.exit")
ans=int(input())
if ans==1:
str=input("Enter the string:")
print("your entered string is:",str)
if ans==2:
str1=input("enter your first string:")
str2=input("enter your second string:")
str3=str1+str2
print("your concatenated string is:",str3)
if ans==3:
str4=input("Enter the string:")
print("your entered string is:",str4)
if ans==4:
str=input("Enter the string:")
sc=input("enter the value to check substring:")
if sc in str:
print(sc, "is a substring")
else:
print(sc, "is a substring")
if ans==5:
print("you exit from program")
2028202 Subject code-BTCS-513-18
exit()
OUTPUT:
select the option
1.creating a string
2.concatenate a string
3.printing a string
4.accessing a substring
5.exit
1
Enter the string:cse
your entered string is: cse

select the option


1.creating a string
2.concatenate a string
3.printing a string
4.accessing a substring
5.exit
2
enter your first string:cse
enter your second string:5th
your concatenated string is: cse5th

select the option


1.creating a string
2.concatenate a string
3.printing a string
4.accessing a substring
5.exit
4
Enter the string:cse in 5th
enter the value to check substring:in
in is a substring

select the option


1.creating a string
2.concatenate a string
3.printing a string
4.accessing a substring
5.exit
5
you exit from program
2028202 Subject code-BTCS-513-18
5. Write a program to create, append, and remove lists in python.
while(1):
print("\n")
print('1.Create a list')
print('2.Append into a list')
print('3.Remove from a list')
print('4.Exit')
a=int(input('Enter your choice :'))
if(a==1):
lst=[]
n=int(input('Enter the number of elements you want to add into list : '))
print("enter element:")
for i in range(0,n):
ele=input()
lst.append(ele)
print(lst)
elif(a==2):
ap=input("Enter the element you want to append into list : ")
lst.append(ap)
print(lst)
elif(a==3):
rm=input("Enter the element which you want to remove :")
lst.remove(rm)
print(lst)
elif(a==4):
print("you exit from program")
exit()
2028202 Subject code-BTCS-513-18
OUTPUT:
1.Create a list
2.Append into a list
3.Remove from a list
4.Exit
Enter your choice :1
Enter the number of elements you want to add into list : 3
enter element:
1
2
3
['1', '2', '3']

1.Create a list
2.Append into a list
3.Remove from a list
4.Exit
Enter your choice :3
Enter the element which you want to remove :2
['1', '3']

1.Create a list
2.Append into a list
3.Remove from a list
4.Exit
Enter your choice :4
you exit from program
2028202 Subject code-BTCS-513-18
6. Write a program to demonstrate working with tuples in python.
#code
while(1):
print("\n")
print('1.Create a tuple')
print('2.Append/Insert into a tuple')
print('3.Remove from a Tuple')
print('4.Update values of Tuple')
print('5.Exit')
a=int(input('Enter your choice :'))
if(a==1):
lst=[]
n=int(input('Enter the number of elements you want to add into Tuple : '))
for i in range(0,n):
ele=input('Enter element :')
lst.append(ele)
tup=tuple(lst)
print(tup)
elif(a==2):
ap=input("Enter the element you want to append into Tuple : ")
lst.append(ap)
tup=tuple(lst)
print(tup)
elif(a==3):
rm=input("Enter the element which you want to remove :")
lst.remove(rm)
tup=tuple(lst)
print(tup)
elif(a==4):
ind=int(input("Enter the index of element which you want to update :"))
el=input("Enter value :")
lst[ind]=el
tup=tuple(lst)
print(tup)
elif(a==5):
print("exited")
exit()
2028202 Subject code-BTCS-513-18
OUTPUT:
1.Create a tuple
2.Append/Insert into a tuple
3.Remove from a Tuple
4.Update values of Tuple
5.Exit
Enter your choice :1
Enter the number of elements you want to add into Tuple : 4
Enter element :1
Enter element :2
Enter element :3
Enter element :43
('1', '2', '3', '43')

1.Create a tuple
2.Append/Insert into a tuple
3.Remove from a Tuple
4.Update values of Tuple
5.Exit
Enter your choice :3
Enter the element which you want to remove :43
('1', '2', '3')

1.Create a tuple
2.Append/Insert into a tuple
3.Remove from a Tuple
4.Update values of Tuple
5.Exit
Enter your choice :5
exited
2028202 Subject code-BTCS-513-18
9. Write a Python program to convert temperatures to and from Celsius,
Fahrenheit.
[ Formula: c/5 = f-32/9]
#code
F=float(input("Enter temperature in fahrenheit:"))
C=(F-32)*5/9
print("%.2f Fahrenheit is: %0.2f Celsius" %(F,C))
C=float(input("Enter temperature in celsius:"))
F=(C*9/5)+32
print("%.2f Celsius is: %0.2f Fahrenheit" %(C,F))

OUTPUT:
Enter temperature in fahrenheit:212
212.00 Fahrenheit is: 100.00 Celsius
Enter temperature in celsius:100
100.00 Celsius is: 212.00 Fahrenheit

10. Write a Python program to construct the following pattern, using a


nested for Loop.
x=int(input("Enter the number:"))
for i in range(x):
for j in range(i+1):
print("*",end=" ")
print()
for i in range(x-1,0,-1):
for j in range(i):
print("*",end=" ")
print()

OUTPUT:
Enter the number:5
*
**
***
****
*****
****
***
**
*
2028202 Subject code-BTCS-513-18
11. Write a Python script that prints prime numbers less than 20.
#code
lower_val=int(input("enter the lower number:"))
greater_value=int(input("enter the higher number:"))
for number in range(lower_val,greater_value+1):
if number>1:
for i in range(2,number):
if(number%i)==0:
break
else:
print(number)

OUTPUT:

enter the lower number: 1


enter the higher number: 20
2
3
5
7
11
13
17
19

12. Write a python program to find factorial of a number using Recursion.


#code
def recursive_factorial(n):
if n == 1:
return n
else:
return n*recursive_factorial(n-1)
number = int(input("User Input : "))
print("The factorial of", number, "is", recursive_factorial(number))

OUTPUT:
User Input : 4
The factorial of 4 is 24
2028180 Subject code-BTCS-513-18
25. WAP to check is a substring is present in a string.
#code
s=input("Enter the string:")
sub=input("Enter the substring:")
if sub in s:
print("Yes, the substring is present in the string")
else:
print("No, the substring is not present in the string")

OUTPUT:
Enter the string: hi hello
Enter the substring: hey
No, the substring is not present in the string

26. WAP to find even length word(s) in a string.


#code
s=input("Enter the string:")
s=s.split(' ')
for i in s:
if len(i)%2==0:
print(i,end=" ")
OUTPUT:
Enter the string: hi hello
Enter the string: My name is Alok
My name is Alok
2028180 Subject code-BTCS-513-18

27. WAP to swap '.' with ','.


#code
s=input("Enter the string:")
s1=s.replace(".","#")
s2=s1.replace(",",".")
s3=s2.replace("#",",")
print(s3)
OUTPUT:
Enter the string:Hi.Hello,
Hi,Hello.

28. WAP to change F.R.I.E.N.D.S to friends.


#code
s="F.R.I.E.N.D.S"
s1=s.lower()
s2=s1.replace(".","")
print(s2)
OUTPUT:
friends

29. WAP to extract digit form a string.


#code
str=input("enter the string:")
list=[*str]
print(list)
for i in list:
if i.isdigit():
print(i,end='')
if i.isspace():
print(" ")
OUTPUT:
enter the string:cse45 5th python34
45
5
34
2028180 Subject code-BTCS-513-18

30.WAP to insert a number in a list before a given number.


#code
#creating a list
elements=int(input("How many elements do you want in list:"))
list=[]
print("enter the elements of list:")
for i in range(0,elements):
list.append(int(input()))
print("Your list:",list)
num=int(input("enter the number you want to add:"))
num2=int(input("enter element before you want to insert:"))
place=list.index(num2)
list.insert(place,num)
print(list)
OUTPUT:
How many elements do you want in list:3
enter the elements of list:
34
45
67
Your list: [34, 45, 67]
enter the number you want to add:25
enter element before you want to insert:45
[34, 25, 45, 67]
2028180 Subject code-BTCS-513-18

31.write a program to remove consonant from a given list.


#code
str=input("enter the string:")
list=[*str]
list2=[]
for i in list:
if i=='A' or i=='a' or i=='E' or i=='e' or i=='I' or i=='i' or i=='o' or i=='O' or
i=='U' or i=='u' or i==' ':
list2.append(i)
str2=''.join(list2)
print(str2)
OUTPUT:
enter the string: hello cse five
eo e ie
32. Write a program that takes your full name as input and displays the
abbreviations of the first and middle names except the last name which is
displayed as it is. For example, if your name is Robin Ramesh Kumar, then
the output should be R. R. Kumar .
#code
name=input("Enter your full name:")
list=name.split(" ")
shortname=""
for i in range(0,len(list)-1):
fname=list[i]
shortname+=(fname[0].upper()+'.')
shortname+=list[-1].title()
print("Short form of name:",shortname)
OUTPUT:
Enter your full name:Robin Ramesh Kumar
Short form of name: R.R.Kumar

You might also like