Basics of Python_Lab3[6264]
Basics of Python_Lab3[6264]
Practical No 3
PART A
A.2 Prerequisite
Programming for problem-solving and Object Oriented Programming
A.3 Outcome
After successful completion of this experiment, students will be able to understand and
implement
A.4 Theory
name = "Python"
print(name)
1
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
● Negative Indexing: Similar to a list, Python allows negative indexing for its strings.
For example,
greet = 'hello'
● Slicing: Access a range of characters in a string by using the slicing operator colon :.
For example,
greet = 'Hello'
Note: If we try to access an index out of the range or use numbers other than an integer, we
will get errors.
Output
TypeError: 'str' object does not support item assignment
However, we can assign the variable name to a new string. For example,
message = 'Hola Amigos'
2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
print(message)
Output
Never gonna give you up
Never gonna let you down
In the above example, anything inside the enclosing triple-quotes is one multiline string.
Output
False
True
In the above example,
● str1 and str2 are not equal. Hence, the result is False.
● str1 and str3 are equal. Hence, the result is True.
3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
In Python, we can join (concatenate) two or more strings using the + operator.
greet = "Hello, "
name = "Jack"
# using + operator
result = greet + name
print(result)
In the above example, we have used the + operator to join two strings: greet and name.
Output
H
e
l
l
o
# Output: 5
4
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Methods Description
5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Output:
('Python ', 'is ', 'fun')
('Python is fun', '', '')
('Python ', 'is', " fun, isn't it")
('Python ', 'is ', 'fun')
('Python is fun', '', '')
('Python ', 'is', " fun, isn't it")
# replace b with c
replaced_text = text.replace('b', 'c')
print(replaced_text)
# Output: 12
6
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
print(text.split(' '))
# Output: True
# Output: True
# Output: 7
Since strings are represented by single or double quotes, the compiler will treat "He said, " as
the string. Hence, the above code will cause an error.
To solve this issue, we use the escape character \ in Python.
7
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
print(example)
\\ Backslash
Output
Cathy is from UK
Here, f'{name} is from {country}' is an f-string.
Python List
A list is a collection of similar or different types of data. For example,
Suppose we need to record the age of 5 students. Instead of creating 5 separate variables, we
can simply create a list:
Elements of a list
8
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
A list is created in Python by placing items inside [], separated by commas . For example,
# A list with 3 integers
numbers = [1, 2, 5]
print(numbers)
# Output: [1, 2, 5]
9
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Python
Negative Indexing
Note: If the specified index does not exist in the list, Python throws the IndexError exception.
my_list = ['p','r','o','g','r','a','m','i','z']
10
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Output
['o', 'g', 'r']
['a', 'm', 'i', 'z']
['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']
Here,
● my_list[2:5] returns a list with items from index 2 to index 4.
● my_list[5:] returns a list with items from index 1 to the end.
● my_list[:] returns all list items
Note: When we slice lists, the start index is inclusive but the end index is exclusive.
Output
Before Append: [21, 34, 54, 12]
After Append: [21, 34, 54, 12, 32]
In the above example, we have created a list named numbers. Notice the line,
numbers.append(32)
Here, append() adds 32 at the end of the array.
2. Using extend()
We use the extend() method to add all items of one list to another. For example,
prime_numbers = [2, 3, 5]
print("List1:", prime_numbers)
11
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
even_numbers = [4, 6, 8]
print("List2:", even_numbers)
Output
List1: [2, 3, 5]
List2: [4, 6, 8]
List after append: [2, 3, 5, 4, 6, 8]
In the above example, we have two lists named prime_numbers and even_numbers. Notice
the statement,
prime_numbers.extend(even_numbers)
Here, we are adding all elements of even_numbers to prime_numbers.
# languages list
languages = ['French', 'English']
You can also append all elements of an iterable to the list using:
1. the + operator
a = [1, 2]
b = [3, 4]
a += b # a = a + b
a=a+b
a=[1,2]+[3,4]
a=[1,2,3,45]
# Output: [1, 2, 3, 4]
print('a =', a)
12
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Output
a = [1, 2, 3, 4]
a[len(a):] = b
# Output: [1, 2, 3, 4]
print('a =', a)
Output
a = [1, 2, 3, 4]
# a1 = [1, 2, 3, 4]
a1.extend(b)
print(a1)
13
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Here, initially the value at index 3 is 'C++'. We then changed the value to 'C' using
languages[2] = 'C'
2. Using remove()
We can also use the remove() method to delete a list item. For example,
languages = ['Python', 'Swift', 'C++', 'C', 'Java', 'Rust', 'R']
Method Description
extend() add items of lists and other iterables to the end of the list
14
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
print('List:', vowel)
# Output:
# Removed Element: 5
# Updated List: [2, 3, 7]
15
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
print(index)
# Output: 1
# Output: Count of 2: 3
print(prime_numbers)
16
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
# copying a list
numbers = prime_numbers.copy()
Output
Python
Swift
C++
17
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Here,
● 'C' is not present in languages, 'C' in languages evaluates to False.
● 'Python' is present in languages, 'Python' in languages evaluates to True.
Output
List: ['Python', 'Swift', 'C++']
Total Elements: 3
Tasks:
1. Write a code for the following: (String Operations)
● Reverse a sentence accepted as input by User
● Find the characters at an odd position in string input by User
● Check string starts with a specific character entered by the user
● Remove all newlines from the String
● Replace all occurrence of substring in string
● Remove punctuation mark from list of string
● Find the number of matching characters in two string
● Convert a string into a list.
● To convert all string elements of the list to int.
● Count Total numbers of upper case and lower case characters in input string
● To find vowels in a string
● To sort a list of string in Python
● To print input string in upper case and lower case
● Convert Int To String In Python
2. Write a Python program to accept a string and replace all spaces by ‘#’ symbol.
3. Write a program to accept two strings from the user and display the common
words (ignore case)
4. Write a program to accept a string and count the frequency of each vowel.
18
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
5. Create a list in python for storing supermarket bill details and perform the
following operations on it:
● Add an item to the end of the list
● Insert an item at a given position
● Modify an element by using the index of the element
● Remove an item from the list
● Remove all items from the list
● Slice Elements from a List
● Remove the item at the given position in the list, and return it
● Return the number of times 'x' appear in the list
● Sort the items of the list in place
● Reverse the elements of the list in place
6. Write a python program to count unique values inside a list
7. Write a python program to print a list excluding the duplicates
8. Write a python program to count positive and negative numbers in a list
9. Write a python program to sum all the elements in a list.
10. Write a Python program to find the list of words that are longer than n from a given
list of words.
11. Write a Python program to compute the difference between two lists.
Sample data: ["red", "orange", "green", "blue", "white"], ["black", "yellow", "green",
"blue"]
Expected Output:
Color1-Color2: ['white', 'orange', 'red']
Color2-Color1: ['black', 'yellow']
12. Write a Python program to concatenate elements of a list.
13. Write a Python program to insert a given string at the beginning of all items in a list.
Sample list : [1,2,3,4], string : emp
Expected output : ['emp1', 'emp2', 'emp3', 'emp4']
14. Write a Python program to find the list in a list of lists whose sum of elements is the
highest.
Sample lists: [1,2,3], [4,5,6], [10,11,12], [7,8,9]
Expected Output: [10, 11, 12]
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 Teams or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
Roll No. Name:
19
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Basics of Python Lab Manual
BTI Sem IV
Program: Division:
Semester: Batch :
Date of Experiment: Date of Submission:
Grade :
B.3 Conclusion:
(Students must write the conclusion as per the attainment of individual outcome listed
above and learning/observation noted in section B.1)
20