Uploaded by hitarthsheth.nmims

EXPT2 DataStructures

advertisement
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering
Electronics and Telecommunication Engineering Department
Program: B.Tech. Semester: III
Course: Python Programming
List of Experiments
Faculty: Prof. Vipul Gohil
w.e.f. July 2023
Experiment No.02
PART A
(PART A: TO BE REFFERED BY STUDENTS)
Experiment 2
A.1 Aim: Data Structures
a. Give new name to the user by exchanging first and last characters of his/her name
b. Teacher has entered math marks for all the students in a list. Help her to print the second
highest and second lowest marks. Use list data structure.
c. Teacher has to select all the students having roll numbers, that are multiple of 5. Help the
teacher. Use list data structure
d. For a given number A which contains only digits 0's and 1's, if it is possible to make all
digits same by flipping only one digit, print YES, else print NO
e. Student names are saved in a tuple. Find the length of each student name.
A.2 Prerequisite:
Concept of string, list, tuple
A.3 Learning Outcome:
After completing this exercise you will be able to1. Understand the use indexing and slicing in string, list and tuple data structures
2. Develop simple programs based on the concepts of string, list and tuple
3. Use appropriate built-in Python functions
A.4 Theory:
A.4.1 StringStrings in python are surrounded by either single quotation marks, or double quotation marks.
Particular element in the string can be accessed using indexing as follows
str1= “Hello”
print(str1[2])
Slicing
Specify the start index and the end index, separated by a colon, to return a part of the string.
b= "Hello,World!"
print(b[2:5])
String concatenation
To concatenate, or combine, two strings you can use the + operator.
a= "Hello"
b= "World"
c=a+b
print(c)
A.4.2 Lists
Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3
are Tuple, Set and Dictionary.
Lists are created using square brackets
Create list
Fruit_list = ["apple", "banana", "cherry"]
print(Fruit_list)
List length
To determine how many items a list has, use the len() function:
A list can contain different data types
list1 = ["abc", 34, True, 40, "male"]
Access Items
thislist=["apple", "banana", "cherry"]
print(thislist[1])
List slicing
thislist
=
print(thislist[2:5])
["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
Append Items
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
A.4.3 TuplesTuples are used to store multiple items in a single variable
A tuple is a collection which is ordered and unchangeable.
Tuples are written with round brackets.
thistuple=("apple", "banana", "cherry")
print(thistuple)
A tuple can contain different data types.
tuple1 = ("abc", 34, True, 40, "male")
A.5 Task to be completed:
a. Give new name to the user by exchanging first and last characters of his/her name
b. Teacher has entered test marks for all the students in a list. Help her to sort the list and also
find out the second highest marks.
c. Create a list of Roll Numbers and find out:
i)
Roll numbers which are multiples of 3
ii)
Roll numbers which are divisible by 3
***************************************************************************
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the MS Teams assignment link at the end of
the practical)
Roll No. D030
Program : B. TECH - EXTC
Batch: D2
Date of Submission: 24th July, 2022
Name: HITARTH SHETH
Division: D
Date of Experiment: 19th July, 2022
Grade :
B.1 Tasks given in PART A to be completed here
(Students must write the answers of the task(s) given in the PART A /Students must copy the
code, output screenshots here based on the task(s) given in section A.5)
a) Give new name to the user by exchanging first and last characters of his/her name
b) Teacher has entered test marks for all the students in a list. Help her to sort the list and also
find out the second highest marks.
c) Create a list of Roll Numbers and find out:
i)
Roll numbers which are multiples of 3
ii)
Roll numbers which are divisible by 3
B.2 Observations and Learning:
(Students must write the observations and learning based on their understanding built about
the subject matter and inferences drawn)
In this experiment, I learnt about usage of lists and strings in Python. They’re useful for
storing multiple data items and provide a comprehensive way of accessing them.
B.3 Conclusion:
(Students must write the conclusive statements as per the actual attainment of individual
outcomes listed above and learning/observation noted in section B.2)
Through this experiment, after understanding the working of lists and strings, we can develop
simple programs based on these concepts which also make use of in-built functions such as sorting,
to sort a list.
B.4 Question of curiosity:
(This section aims to develop student’s curiosity, research ability. So students are free to
search the answer on web. This section includes general .case study based/out of syllabus
questions based on the theme of the experiment. Students should be able to answer these
questions based the experiment carried out.)
1. What will be the output of following program?
numbers = [1, 2, 3, 4]
numbers.append([5,6,7,8])
print(len(numbers))
4
5
8
12
Ans: 5
2. What will be displayed by the following code?
list1 = [1, 3]
list2 = list1
list1[0] = 4
print(list2)
[4,3]
[1,3]
[1,4]
[1,2,3]
Ans: [4,3]
3. What will be the output of the following code? Write your answer
str1="Investigator"
print(str1[2:8])
Ans: vestig
***************
Download