Fundamental Concepts and Problem Solving
with Python
SUBJECT CODE-ENCS-121 &ENCS-121P
UNIT-III
• Data Structures in Python:
• Strings: String operations and methods, String formatting and manipulation,
• Lists: List operations and methods, List comprehensions, Nested lists,
• Tuple: Tuple operations and methods, Advantages of using tuples,
• Dictionaries: Dictionary operations and methods, Dictionary comprehensions, Nested
dictionaries,
• Sets: Set operations and methods, Set theory operations (union, intersection, difference)
IIMT UNIVERSITY MEERUT 08-04-2026 1
Data Structures in Python
• Data structures are used to store and organize data efficiently so that it
can be accessed and modified easily.
IIMT UNIVERSITY MEERUT 08-04-2026 2
STRINGS
• A string is a sequence of characters enclosed in quotes (' ' or " ").
• s = "Hello World“
• String Syntax
• string_name = "value“
• It can contain:
• Letters → "Hello"
• Numbers → "123"
• Symbols → "@#&“
• In Python, strings are immutable
That means once created, they cannot be changed
IIMT UNIVERSITY MEERUT 08-04-2026 3
String Indexing (Position Access)
• s = "Python“
Character P y t h o n
Index 0 1 2 3 4 5
IIMT UNIVERSITY MEERUT 08-04-2026 4
Example:
• print(s[0]) # P
• print(s[3]) # h
IIMT UNIVERSITY MEERUT 08-04-2026 5
Negative Indexing
• print(s[-1]) # n
• print(s[-2]) # o
IIMT UNIVERSITY MEERUT 08-04-2026 6
String Slicing
• Used to extract part of a string.
• string[start:end:step]
• s = "Python"
• print(s[0:3]) # Pyt
• print(s[2:5]) # tho
• print(s[:4]) # Pyth
• print(s[::2]) # Pto
IIMT UNIVERSITY MEERUT 08-04-2026 7
String Operations
• 1. Concatenation (+)
• a = "Hello"
• b = "World"
• print(a + " " + b) # Hello World
IIMT UNIVERSITY MEERUT 08-04-2026 8
Important String Methods
• 1. lower() – Convert to lowercase
• print("HELLO".lower()) # hello
• 2. upper() – Convert to uppercase
• print("hello".upper()) # HELLO
• strip() – Remove spaces
• s = " hello "
• print([Link]()) # hello
IIMT UNIVERSITY MEERUT 08-04-2026 9
• 4. replace()
• print("Hello".replace("H", "Y")) # Yello
• 5. find()
• print("Python".find("t")) # 2
• count()
• print("banana".count("a")) # 3
IIMT UNIVERSITY MEERUT 08-04-2026 10
8. String Membership
• s = "Python"
• print("P" in s) # True
• print("z" in s) # False
IIMT UNIVERSITY MEERUT 08-04-2026 11
9. String Formatting
• 1. Using format()
• name = "Aditya"
• age = 20
• print("My name is {} and age is {}".format(name, age))
IIMT UNIVERSITY MEERUT 08-04-2026 12
2. Using f-string
• print(f"My name is {name} and age is {age}")
IIMT UNIVERSITY MEERUT 08-04-2026 13
10. Escape Characters
Escape Meaning
\n New line
\t Tab
\' Single quote
\" Double quote
IIMT UNIVERSITY MEERUT 08-04-2026 14
11. Looping Through String
• s = "Python"
• for ch in s:
• print(ch)
IIMT UNIVERSITY MEERUT 08-04-2026 15
12. Immutability of String
• s = "Hello"
• # s[0] = 'Y' ERROR
• # Correct way:
• s = "Yello"
• print(s)
IIMT UNIVERSITY MEERUT 08-04-2026 16
Example 1: Reverse a String
• s = "Python"
• print(s[::-1]) # nohtyP
IIMT UNIVERSITY MEERUT 08-04-2026 17
Example 2: Palindrome Check
• s = input("Enter string: ")
• if s == s[::-1]:
• print("Palindrome")
• else:
• print("Not Palindrome")
IIMT UNIVERSITY MEERUT 08-04-2026 18
Example 3: Count vowels
• s = "education"
• count = 0
• for ch in s:
• if ch in "aeiou":
• count += 1
• print("Vowels:", count)
IIMT UNIVERSITY MEERUT 08-04-2026 19
Repetition (*)
• print("Hi " * 3) # Hi Hi Hi
IIMT UNIVERSITY MEERUT 08-04-2026 20
String Operations
Operation Example Output
Concatenation "Hi" + " Aditya" "Hi Aditya"
Repetition "Hi"*3 "HiHiHi"
Indexing s[0] 'H'
Slicing s[0:5] "Hello"
IIMT UNIVERSITY MEERUT 08-04-2026 21
Important String Methods
• s = "hello world"
• print([Link]()) # HELLO WORLD
• print([Link]()) # hello world
• print([Link]()) # Hello world
• print([Link]()) # Hello World
• print([Link]("world", "Python")) # hello Python
• print([Link]("world")) # 6
• print([Link]("l")) # 3
• print([Link]()) # ['hello', 'world']
IIMT UNIVERSITY MEERUT 08-04-2026 22
Using join() (better for multiple strings):
• words = ["Python", "is", "fun"]
• sentence = " ".join(words)
• print(sentence) # Python is fun
IIMT UNIVERSITY MEERUT 08-04-2026 23
Removing Whitespace
• text = " Hello "
• print([Link]()) # "Hello"
• print([Link]()) # "Hello "
• print([Link]()) # " Hello"
IIMT UNIVERSITY MEERUT 08-04-2026 24
Searching in Strings
• text = "Python programming"
• print([Link]("pro")) # 7
• print([Link]("pro")) # 7 (error if not found)
• print("Python" in text) # True
IIMT UNIVERSITY MEERUT 08-04-2026 25
Replacing Text
• text = "I like Java"
• new_text = [Link]("Java", "Python")
• print(new_text) # I like Python
IIMT UNIVERSITY MEERUT 08-04-2026 26
Splitting Strings
• text = "apple,banana,orange"
• fruits = [Link](",")
• print(fruits) # ['apple', 'banana', 'orange']
IIMT UNIVERSITY MEERUT 08-04-2026 27
Checking String Content
• text = "Python123"
• print([Link]()) # False
• print([Link]()) # False
• print([Link]()) # True
• print([Link]()) # False
• print([Link]("Py")) # True
• print([Link]("123")) # True
IIMT UNIVERSITY MEERUT 08-04-2026 28
Counting Occurrences
• text = "banana"
• print([Link]("a")) # 3
IIMT UNIVERSITY MEERUT 08-04-2026 29
What is a List? (Definition)
• A list in Python is a collection of items stored in a single variable.
• It can store multiple values
• Items can be of different data types
• Lists are mutable (can be changed)
• Example:
• my_list = [10, 20, 30, "Python", 5.5]
IIMT UNIVERSITY MEERUT 08-04-2026 30
List Syntax
• list_name = [item1, item2, item3, ...]
• Accessing List Elements
• nums = [10, 20, 30, 40]
• print(nums[0]) # 10
• print(nums[-1]) # 40
IIMT UNIVERSITY MEERUT 08-04-2026 31
Slicing:
• print(nums[1:3]) # [20, 30]
IIMT UNIVERSITY MEERUT 08-04-2026 32
List Operations
• Concatenation (+)
• a = [1, 2]
• b = [3, 4]
• print(a + b) # [1, 2, 3, 4]
IIMT UNIVERSITY MEERUT 08-04-2026 33
Repetition (*)
• print([1, 2] * 3)
• # [1, 2, 1, 2, 1, 2]
IIMT UNIVERSITY MEERUT 08-04-2026 34
Membership (in / not in)
• nums = [10, 20, 30]
• print(20 in nums) # True
• Length
• print(len(nums)) # 3
IIMT UNIVERSITY MEERUT 08-04-2026 35
List Methods (Important)
• append()
• Adds element at end-
• nums = [1, 2]
• [Link](3)
• print(nums) # [1, 2, 3]
IIMT UNIVERSITY MEERUT 08-04-2026 36
insert()
• Adds element a
• [Link](1, 10)
• # [1, 10, 2, 3]t specific index
IIMT UNIVERSITY MEERUT 08-04-2026 37
remove()
• Removes specific value
• [Link](10)
IIMT UNIVERSITY MEERUT 08-04-2026 38
pop()
• Removes element by index
• [Link]() # last element
• [Link](1) # index 1
IIMT UNIVERSITY MEERUT 08-04-2026 39
sort()
• nums = [3, 1, 2]
• [Link]()
• print(nums) # [1, 2, 3]
IIMT UNIVERSITY MEERUT 08-04-2026 40
reverse()
• [Link]()
IIMT UNIVERSITY MEERUT 08-04-2026 41
count()
• print([Link](2))
IIMT UNIVERSITY MEERUT 08-04-2026 42
index()
• print([Link](2))
IIMT UNIVERSITY MEERUT 08-04-2026 43
clear()
• [Link]()
IIMT UNIVERSITY MEERUT 08-04-2026 44
List Comprehension
• A short and easy way to create lists using a single line of code.
• [expression for item in iterable if condition]
• Simple list
• squares = [x**2 for x in range(5)]
• print(squares)
• # [0, 1, 4, 9, 16]
IIMT UNIVERSITY MEERUT 08-04-2026 45
With condition
• even = [x for x in range(10) if x % 2 == 0]
• print(even)
• # [0, 2, 4, 6, 8]
IIMT UNIVERSITY MEERUT 08-04-2026 46
Convert to uppercase
• words = ["python", "java"]
• upper = [[Link]() for w in words]
IIMT UNIVERSITY MEERUT 08-04-2026 47
Nested Lists
• A list inside another list is called a nested list.
• matrix = [
• [1, 2, 3],
• [4, 5, 6],
• [7, 8, 9]
• ]
IIMT UNIVERSITY MEERUT 08-04-2026 48
Accessing Elements:
• print(matrix[0][1]) # 2
• print(matrix[2][2]) # 9
IIMT UNIVERSITY MEERUT 08-04-2026 49
Looping through nested list:
• for row in matrix:
• for item in row:
• print(item, end=" ")
IIMT UNIVERSITY MEERUT 08-04-2026 50
Nested List Comprehension:
• matrix = [[j for j in range(3)] for i in range(3)]
• print(matrix)
IIMT UNIVERSITY MEERUT 08-04-2026 51
IIMT UNIVERSITY MEERUT 08-04-2026 52
IIMT UNIVERSITY MEERUT 08-04-2026 53