0% found this document useful (0 votes)
13 views5 pages

Pyhton Exp1

The document contains a series of Python programs that demonstrate basic operations such as calculating the length of a string, finding the largest number in a list, manipulating tuples and arrays, summing elements in a list, adding elements to a set, and sorting a dictionary by values. Each program includes input prompts, operations performed, and expected output. These examples serve as practical exercises for beginners to understand fundamental programming concepts.

Uploaded by

ankurkumar99421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Pyhton Exp1

The document contains a series of Python programs that demonstrate basic operations such as calculating the length of a string, finding the largest number in a list, manipulating tuples and arrays, summing elements in a list, adding elements to a set, and sorting a dictionary by values. Each program includes input prompts, operations performed, and expected output. These examples serve as practical exercises for beginners to understand fundamental programming concepts.

Uploaded by

ankurkumar99421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

WAP to calculate length of string :-


a = input(“Enter a string: “)
count = 0
for i in a:count += 1
print(“Length of given string is”, count)

Output :-

2. WAP to get largest no. from list :-


a = []
n = int(input(“Enter no. of elements: “))
for i in range(1, n+1):
b = int(input(“Enter element: “))
a.append(b)
a.sort()
print(“Largest element is: “,a[n-1])

Output :-
3. WAP to print element of tuple :-
tup = tuple(“LTCOE”)
print(tup[0])
print(tup[1:4])
print(tup[:3])
tup = tuple(“A”, “B”, “C”)
a,b,c = tup
print(a)
print(b)
print(c)

Output :-

4. WAP to append a element in array :-


import array as arr
a = arr.array(‘i’, [1,2,3])
print(a[0])
a.append(5)
print(a)

Output :-
5. WAP to insert a new item to the array :-
import array as arr
a = arr.array(‘i’, [1,2,3])
print(“Integer array before insertion: “,*a)
a.insert(1,4)
print(“Integer array after insertion: “,*a)

Output :-

6. WAP to append elements in list :-


a = []
n = int(input(“Enter the no. of elements: “))
for i in range(n):
element = input(f”Enter element {i+1}: “)
a.append(element)
print(“List: “,a)

Output :-
7. WAP to sum all elements in list :-
a = []
sum = 0
n = int(input("Enter no. of elements in list: "))
for i in range(n):
b = int(input(f"Enter element {i + 1}: "))
a.append(b)
print("Given array is:", a)
for i in range(n):
sum += a[i]
print("Sum of numbers in list is:", sum)

Output :-

8. WAP to add members in set :-


my_set = set()
print("Enter no. of elements in set: ")
n = int(input())
for i in range(n):
b = int(input(f"Enter element {i + 1}: "))
my_set.add(b)
print("The set is:", my_set)

Output :-
9. WAP to sort dictionary by values :-
daynames = {"Monday": 1, "Saturday": 6, "Wednesday": 3, "Tuesday": 2, "Friday": 5,
"Sunday": 7, "Thursday": 4}
Keys = list(daynames.keys())
Values = list(daynames.values())
for i in range(len(Values)):
for j in range(len(Values) - 1):
if Values[j] > Values[j + 1]:
temp = Values[j]
Values[j] = Values[j + 1]
Values[j + 1] = temp
temp1 = Keys[j]
Keys[j] = Keys[j + 1]
Keys[j + 1] = temp1
daynames2 = {}
for i in range(len(Values)):
daynames2[Keys[i]] = Values[i]
print("Given dictionary is:", daynames)
print("Sorted dictionary is:", daynames2)

Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy