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

Python_Practicale

Uploaded by

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

Python_Practicale

Uploaded by

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

1.

Write a python function that takes a list of numbers as input and return the sum of all the numbers in the list.

def sum(num):

total = 0

for x in num:

total += x

return total

numbers=[]

n=int(input("Enter the number of list of numbers:"))

print("Enter ",n, "numbers for the list")

for i in range(0,n):

numbers.append(int(input()))

t=sum(numbers)

print("the sum of all numbers= ",t)

OUT PUT:

2. Create a function that takes two arguments, a string, and a character, and returns the number of times the
character appears in the string.

def count(s, c) :

res = 0

for i in range(len(s)) :

if (s[i] == c):

res = res + 1

return res

str= input("Enter a string: ")

c = input("Enter a character: ")

print(count(str, c))
Output:
3. Write a function that take list of integers and returns the largest number in the list.

def largest(n):

l = n[0]

for val in n:

if val > l:

l= val

return l

a = [10, 24, 76, 23, 12]

m=largest(a)

print("Largest number= ",m)

Output:

4. Develop a function that accepts a list of strings and returns a new list with each string reversed.

def rev_list_of_string(str):

r=str[::-1]

rev=[]

for i in r:

rev.append(i[::-1])

return rev

l=["Math","Physics","Chemistry","Biology","Computer"]

print("Original List: ")

print(l)

rev_list=rev_list_of_string(l)

print("Reverse List: ")

print(rev_list)
Output:

5. Write a function that calculates the area of a circle given the radius.

import math

def area(r):

area = math.pi* pow(r,2)

return print("Area of circle is: ",area)

area(4)

Output:

1. Given a list of integers write a python script to remove all the even numbers from the list

lis = [1, 2, 3, 4, 5]

out = []

for num in lis:

if num % 2 != 0:

out.append(num)

print(out)

2. write a program that merges two lists and sort the merged list in ascending order.

test_list1 = [1, 5, 6, 9, 11]

test_list2 = [3, 4, 7, 8, 10]

print ("The original list 1 is : " + str(test_list1))

print ("The original list 2 is : " + str(test_list2))


res = sorted(test_list1 + test_list2)

print ("The combined sorted list is : " + str(res))

output:

3. create list of 10 random integers. Write a program to find the index of the first occurrence of the maximum value in
the list.

def max_number_index(input_list):

idx = 0

max_number = input_list[0]

for i in range(1, len(input_list)):

if input_list[i] > max_number:

max_number = input_list[i]

idx = i

return idx

print("Maximum number index = ", max_number_index([0,1,10,3,4]))

output:

4. Write a python program that appends the squares of a numbers from 1 to 10 to an empty list

def printValues():

l = list()

for i in range(1, 11):

l.append(i**2)

print(l)

printValues()

output:
5. given a list of names, write a python program to count how many names start with the letter 'a'

name_list = ["Ram", "arun", "Vijay", "Shankavi", "Kumari", "Aarani", "Abi"]

c=0

for name in name_list:

if name.upper().startswith('A'):

c+=1

print("The total number of names start with A: ",c)

1. write a python program to count the frequency of each word in a given sentence using a dictionary

def word_count(str):

counts = dict()

words = str.split()

for word in words:

if word in counts:

counts[word] += 1

else:

counts[word] = 1

return counts

print( word_count('the quick brown fox jumps over the lazy dog.'))

output:

2. create a dictionary where keys are the names of students and values are their marks. Write a program to find
the student with the highest marks

n = int(input("Enter number of students: "))

data = {}

for i in range(n):

stu_name = input("Enter student name: ")

marks = int(input("Enter marks: "))

data[stu_name] = marks

max=0

name=""
for std in data:

if(data[std]>max):

max=data[std]

name=std

print("Name\tMarks")

print(name,":\t", max)

output

3. write a python script to merge two python dictionaries and print the resulting dictionary

d1 = {'x': 1, 'y': 2}

d2 = {'y': 3, 'z': 4}

print("Before merge the dictionaries are :")

print(d1)

print(d2)

d1.update(d2)

print("After merge the dictionaries are :")

print(d1)

output:

4.

You might also like