Python_Practicale
Python_Practicale
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=[]
for i in range(0,n):
numbers.append(int(input()))
t=sum(numbers)
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
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
m=largest(a)
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(l)
rev_list=rev_list_of_string(l)
print(rev_list)
Output:
5. Write a function that calculates the area of a circle given the radius.
import math
def area(r):
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 = []
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.
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]
max_number = input_list[i]
idx = i
return idx
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()
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'
c=0
if name.upper().startswith('A'):
c+=1
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()
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
data = {}
for i in range(n):
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(d1)
print(d2)
d1.update(d2)
print(d1)
output:
4.