FR.
CONCEICAO RODRIGUES COLLEGE OF ENGINEERIG
Department of Mechanical Engineering
Academic Year 2024-2025 Estimated Experiment No. 2 – 02 Hours
Time
Course & Semester F.E. CE Subject Name Creative Coding in Python
Module No. 02 Chapter Title
Experiment Type Software Subject Code VSE11ME02
Performance
Name of Olin Xavier Lemos Roll No. 10303
Student
Date of 08/01/2025 Date of 08/01/2025
Performance.: Submission.
:
CO Mapping CO2: Demonstrate basic concepts of python programming.
Objective of Experiment: The aim of this experiment is to understand the concepts of Lists in python.
Pre-Requisite:
Tools: Any IDLE, Juypter Notebook / google colab/pycharm/VScode etc
Theory:
In simple language, a list is a # Creating a List of strings and len() function is used to get the
collection of things, enclosed in [ ] accessing length of list.
and separated by commas. # using index List1 = [10, 20, 14]
List = ["Geeks", "For", "Geeks"] print(len(List1))
The list is a sequence data type print("List Items: ") you will get the output as 3
which is used to store the print(List[0])
collection of data. print(List[2]) Accessing elements from the list
Example of list in python— List = ["Geeks", "For", "Geeks"]
Var = ["Geeks", "for", "Geeks"] A list can contain duplicate values
print(Var) also. # accessing a element from the
A list can contain elements of # list using index number
Creating a list in python: various data types. A list can print("Accessing a element from
List = [] contain a list also. the list")
print("Blank List: ") print(List[0])
print(List) List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print(List[2])
print("List with the use of
# Creating a List of numbers Numbers: ") In Python, negative sequence
List = [10, 20, 14] print(List) indexes represent positions from
print("\nList of numbers: ") the end of the List. Instead of
print(List) # Creating a List with mixed having to compute the offset as in
datatype values List[len(List)-3], it is enough to
# (Having numbers and strings) just write List[-3].
List = [1, 2, 'Geeks', 4, 'For', 6, Negative indexing means
'Geeks',True,[1,2,3]] beginning from the end, -1 refers to
print("\nList with the use of Mixed the last item, -2 refers to the
Values: ") second-last item, etc.
print(List)
Taking Input of a Python List The list data type has some list.extend
We can take the input of a list of methods. Here are all of the extend --this method is used to add
elements as string, integer, float, methods of list objects: multiple elements at the same time
etc. But the default one is a string. at the end of the list.
list.append(x) List = [1, 2, 3, 4]
# input the list as string Add an item to the end of the list. print("Initial List: ")
string = input("Enter elements List = [] print(List)
(Space-Separated): ") print("Initial blank List: ")
print(List) # Addition of multiple elements
# split the strings and store it to a # Addition of Elements in the List # to the List at the end
list List.append(1) # (using Extend Method)
lst = string.split() List.append(2) List.extend([8, 'Geeks', 'Always'])
print('The list is:', lst) # printing List.append(4) print("\nList after performing
the list print("\nList after Addition of Extend Operation: ")
Three elements: ") print(List)
l=["apple","mango","peru"] print(List)
print(type(l)) list.remove(x)
for i in l: list.insert() Remove the first item from the list
print(i) whose value is equal to x. It raises
if "mango" in l: For the addition of elements at the a ValueError if there is no such
print("yes") desired position, insert() method is item.
if "man" in "mango": used. Remove() method only removes
print("yes") Unlike append() which takes only one element at a time, to remove a
list1=["abc",1,2,3,True,[4,5,6]] one argument, the insert() method range of elements, the iterator is
print(list1) requires two arguments(position, used. The remove() method
print(list1[-1])) value). removes the specified item.
print(list1[1::2]) List = [1,2,3,4]
List.insert(3, 12) List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
observe the output of the above List.insert(0, 'Geeks') 11, 12]
program print("\nList after performing print("Initial List: ")
Insert Operation: ") print(List)
print(List)
# Removing elements from List list.pop([i]) List.reverse()
# using Remove() method A list can be reversed by using
List.remove(5) Remove the item at the given the reverse() method in Python.
List.remove(6) position in the list, and return it. mylist = [1, 2, 3, 4, 5, 'Geek',
print("\nList after Removal of two If no index is 'Python']
elements: ") specified, a.pop() removes and mylist.reverse()
print(List) returns the last item in the list. It print(mylist)
raises an IndexError if the list is A list can also be reversed using
List = [1, 2, 3, 4, 5, 6, empty or the index is outside the the reversed function.
7, 8, 9, 10, 11, 12] list range. By default it removes my_list = [1, 2, 3, 4, 5]
# Removing elements from List reversed_list =
# using iterator method only the last element of the list, to list(reversed(my_list))
for i in range(1, 5): remove an element from a specific print(reversed_list)
List.remove(i) position of the List, the index of
print("\nList after Removing a the element is passed as an
range of elements: ") argument to the pop() method.
print(List) List.pop()
List.pop(2)
list.clear()
Remove all items from the list.
Slicing of a List
We can get substrings and sublists
using a slice. In Python List, there
are multiple ways to print the
whole list with all the elements, but
to print a specific range of
elements from the list, we use
the Slice operation.
Problem Description:
Grade Sorter App:
It effectively collects grades from the user, sorts them, calculates the average, and converts the average grade
into a letter grade.
Input:Enter grade 1 (0-100): A Output: Libraries: NA
Invalid input. Please enter a Sorted Grades: [40.0, 50.0, 60.0,
numeric grade. 86.0, 90.0]
Enter grade 1 (0-100): 40 Average Grade: 65.20
Enter grade 2 (0-100): 50 Letter Grade: C
Enter grade 3 (0-100): 60
Enter grade 4 (0-100): 86
Enter grade 5 (0-100): 90
Methods used in Libraries: NA
Program: def main():
grades = [] # List to store grades
# Collect grades from the user
while len(grades) < 5: # Ensure exactly 5 grades are collected
grade_input = input(f"Enter grade {len(grades) + 1} (0-100): ")
try:
grade = float(grade_input)
if 0 <= grade <= 100:
grades.append(grade) # Add valid grades to the list
else:
print("Please enter a grade between 0 and 100.")
except ValueError:
print("Invalid input. Please enter a numeric grade.")
# Sort the grades
sorted_grades = sorted(grades)
# Calculate the average grade
average = sum(grades) / len(grades)
# Determine letter grade
if average >= 80:
letter_grade = 'A'
elif average >= 70:
letter_grade = 'B'
elif average >= 60:
letter_grade = 'C'
elif average >= 40:
letter_grade = 'D'
else:
letter_grade = 'F'
# Output results
print(f"\nSorted Grades: {sorted_grades}")
print(f"Average Grade: {average:.2f}")
print(f"Letter Grade: {letter_grade}")
if __name__ == "__main__":
main()
Output:
Practice Programs:
1. Write a Python program to sum all the items in a list.
2. Write a Python program to multiply all the items in a list.
3. Write a Python program to get the largest number from a list.
4. Write a Python program to get the smallest number from a list.
5. Write a Python program to remove duplicates from a list.
6. Write a Python program to clone or copy a list.
7. Write a Python program to find the list of words that are longer than n from a given list of
words
8. Write a Python function that takes two lists and returns True if they have at least one common
member.
9. Write a Python program to print the numbers of a specified list after removing even numbers
from it
10. Write a Python program to append a list to the second list.
11. Write a Python program to concatenate elements of a list.
Programs and Outputs
1. Write a Python program to sum all the items in a list.
2. Write a Python program to multiply all the items in a list.
3. Write a Python program to get the largest number from a list.
4. Write a Python program to get the smallest number from a list.
5. Write a Python program to remove duplicates from a list.
Correctness & Logic & Problem Use of Python Timeline Total
Functionality Solving Approach Features (1 Marks) (10)
(3 Marks) with creativity (3 Marks)
(3 Marks)
References:
Study Materials Online repositories:
1. Yashvant Kanetkar, “Let us Python: Python is 1. Python 3 Documentation: https://docs.python.org/3/
Future, Embrace it fast”, BPB Publications;1 3. "The Python Tutorial",
st edition (8 July 2019). http://docs.python.org/release/3.0.1/tutorial/
2. Dusty Phillips, “Python 3 object-oriented 4. http://spoken-tutorial.org
Programming”, Second Edition PACKT 5. Python 3 Tkinter library Documentation:
Publisher, August 2015. https://docs.python.org/3/library/tk.html
3. John Grayson, “Python and Tkinter 6. Numpy Documentation: https://numpy.org/doc/
Programming”, Manning Publications (1 March 7. Pandas Documentation: https://pandas.pydata.org/docs/
1999). 8. Matplotlib Documentation:
4. Core Python Programming, Dr. R. Nageswara https://matplotlib.org/3.2.1/contents.html
Rao, Dreamtech Press 9. Scipy Documentation :
5. Beginning Python: Using Python 2.6 and Python https://www.scipy.org/docs.html
3.1. James Payne, Wrox publication 10. Machine Learning Algorithm Documentation:
6. Introduction to computing and problem solving https://scikit-learn.org/stable/
using python, E Balagurusamy, 11. https://nptel.ac.in/courses/106/106/106106182/
McGrawHill Education 12. NPTEL course: “The Joy of Computing using Python”
13. https://www.w3resource.com/python-exercises/list/
Video Channels:
https://www.youtube.com/watch?v=t2_Q2BRzeEE&list=PLGjplNEQ1it8-0CmoljS5yeV-GlKSUEt0
https://www.youtube.com/watch?v=7wnove7K-ZQ&list=PLu0W_9lII9agwh1XjRt242xIpHhPT2llg
Data Visualization:
https://www.youtube.com/watch?v=_YWwU-gJI5U