0% found this document useful (0 votes)
9 views

Unit4python 1 1

This document provides an introduction to various Python string and list operations and methods, including slicing strings, concatenating strings, understanding string immutability, built-in string methods, modifying lists by assigning to slices, inserting elements into lists, repeating and merging comma-separated lists, built-in list and tuple functions, basic dictionary operations, built-in set functions, list and dictionary comprehensions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit4python 1 1

This document provides an introduction to various Python string and list operations and methods, including slicing strings, concatenating strings, understanding string immutability, built-in string methods, modifying lists by assigning to slices, inserting elements into lists, repeating and merging comma-separated lists, built-in list and tuple functions, basic dictionary operations, built-in set functions, list and dictionary comprehensions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

INTRODUCTION TO STRINGS

STRING OPERATIONS
SLICING
REPETITION OF STRING
UNDERSTANDING STRING IMMUTABLITY
BUILT-IN STRING METHODS
CONCATENATION OF STRINGS
ESCAPE CHARACTER
REPR()
The repr() function returns a printable representation of the given object.

numbers = [1, 2, 3, 4, 5]

# create a printable representation of the list


printable_numbers = repr(numbers)
print(printable_numbers)

# Output: [1, 2, 3, 4, 5]
repr() Parameters
The repr() function takes a single parameter:
obj - the object whose printable representation has to be returned
repr(obj)

var = 'foo'

print(repr(var))
MODULES
INTRODUCTION TO LISTS
a = [1, 2, 3, 4, 5]
a[0:3] = [100, 100, 100] Changing multiple elements
print(a) # Output: [100, 100, 100, 4, 5]

a = [1, 2, 3, 4, 5]
Certain elements from a list can also be
a[0:3] = [ ]
removed by assigning an empty list to them
print(a) # Output: [4, 5]

a = [1, 2, 3, 4, 5] The elements can be inserted into a list by


a[0:0] = [20, 30, 45] squeezing them into an empty slice at the desired
print(a) # Output: [20, 30, 45, 1, 2, 3, 4, 5] location
Create a list with the user-given inputs. Take an index value n from the user.
Write a program to update a list with user given element based on index n, if the
index is not in the range, print the error message as shown in the sample test
cases.

Sample Input and Output 1:


data: James,John,Jacob
before updation: ['James', 'John', 'Jacob']
index: -2
element: Oliver
after updation: ['James', 'Oliver', 'Jacob']

Sample Input and Output 2:


data: 10,20,54,26
before updation: ['10', '20', '54', '26']
index: 4
invalid
Write a program that takes input for two comma-separated lists, along with a number. The program
should repeat and print each list the specified number of times and then combine the lists and display
the final merged list as shown in the sample test cases.

Sample Input and Output 1:


data1: Python,Java
data2: Perl,Swift
num: 2
['Python', 'Java', 'Python', 'Java']
['Perl', 'Swift', 'Perl', 'Swift']
extending list1 with list2: ['Python', 'Java', 'Perl', 'Swift']

Sample Input and Output 2:


data1: 10,20,30
data2: 40,50
num: 3
['10', '20', '30', '10', '20', '30', '10', '20', '30']
['40', '50', '40', '50', '40', '50']
extending list1 with list2: ['10', '20', '30', '40', '50']
data1 = input("data1: ")
list1 = data1.split(",")
data2 = input("data2: ")
list2 = data2.split(",")
num = int(input("num: "))
print(list1 * num)
print(list2 * num)
list1.extend(list2)
print("extending list1 with list2:", list1)
LIST METHODS
INTRODUCTION TO TUPLES
BUILT-IN TUPLE FUNCTIONS
BASIC DICTIONARY OPERATIONS
BUILT-IN SET FUNCTIONS AND METHODS
LIST COMPREHENSIONS
DICTIONARY COMPREHENSIONS

You might also like