Data types Contd,Slicing the data,Inbuilt
functions in python
Dictionaries,Sequence
methods,Concatenate,Repetition,len,min,max
functions,Index position,Addition and deletion of
elements,Reverse,Sorting
1. Data Types in Python Python has several built-in data types:
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Text Type: str
Set Types: set, frozenset
Mapping Type: dict
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Example 1: Numeric Types
x = 10 # int
y = 10.5 # float
z = 3 + 4j # complex
print(type(x), type(y), type(z))
Example 2: String Type
s = "Hello Python"
print(type(s))
Example 3: List Type
lst = [1, 2, 3, 4, "Python"]
print(type(lst))
Example 4: Tuple Type
tp = (10, 20, 30, "Tuple")
print(type(tp))
Example 5: Dictionary Type
dct = {"name": "Alice", "age": 25}
print(type(dct))
Example 6: Set Type
s = {1, 2, 3, 4, 5}
print(type(s))
Example 7: Boolean Type
b = True
print(type(b))
Example 8: Bytes Type
bt = bytes(5)
print(type(bt))
Example 9: Bytearray Type
ba = bytearray(5)
print(type(ba))
Example 10: Memoryview Type
mv = memoryview(bytes(5))
print(type(mv))
2. Slicing the Data Slicing allows extracting a portion of a sequence (string,
list, tuple) using the format:
sequence[start:stop:step]
Example 1: String Slicing
text = "Python Programming"
print(text[0:6]) # Output: Python
Example 2: Reverse String
print(text[::-1]) # Output: gnimmargorP nohtyP
Example 3: List Slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40]
Example 4: Tuple Slicing
tuple_data = (1, 2, 3, 4, 5, 6)
print(tuple_data[2:]) # Output: (3, 4, 5, 6)
Example 5: Skipping Elements
print(numbers[::2]) # Output: [10, 30, 50]
Example 6: Negative Indexing
print(numbers[-3:-1]) # Output: [30, 40]
Example 7: Changing List Values using Slicing
numbers[1:3] = [25, 35]
print(numbers) # Output: [10, 25, 35, 40, 50]
Example 8: Replacing a Range of Values
numbers[2:4] = [100, 200]
print(numbers) # Output: [10, 25, 100, 200, 50]
Example 9: Clearing a List using Slicing
numbers[:] = []
print(numbers) # Output: []
Example 10: Copying a List using Slicing
numbers_copy = numbers[:]
print(numbers_copy) # Output: []
3. Sequence Methods Python provides several built-in methods to manipulate
sequences such as lists, tuples, and strings.
Example 1: append() - Adding Elements to a List
lst = [1, 2, 3]
[Link](4)
print(lst) # Output: [1, 2, 3, 4]
Example 2: extend() - Merging Lists
[Link]([5, 6])
print(lst) # Output: [1, 2, 3, 4, 5, 6]
Example 3: insert() - Inserting at a Specific Position
[Link](2, 99)
print(lst) # Output: [1, 2, 99, 3, 4, 5, 6]
Example 4: remove() - Deleting an Element
[Link](99)
print(lst) # Output: [1, 2, 3, 4, 5, 6]
Example 5: pop() - Removing Last Element
[Link]()
print(lst) # Output: [1, 2, 3, 4, 5]
Example 6: count() - Count Occurrences
print([Link](2)) # Output: 1
Example 7: index() - Find Position of an Element
print([Link](3)) # Output: 2
Example 8: reverse() - Reverse the List
[Link]()
print(lst) # Output: [5, 4, 3, 2, 1]
Example 9: sort() - Sorting a List
[Link]()
print(lst) # Output: [1, 2, 3, 4, 5]
Example 10: copy() - Copying a List
new_lst = [Link]()
print(new_lst) # Output: [1, 2, 3, 4, 5]
Index Position in Python
In Python, indexing is used to access individual elements of sequences like
strings, lists, and tuples. Python uses zero-based indexing, meaning the
first element has an index of 0, the second has an index of 1, and so on.
Types of Indexing
1. Positive Indexing → Left to Right (Starts from 0)
2. Negative Indexing → Right to Left (Starts from -1)
1. Positive Indexing
Each element is assigned an index starting from 0 up to n-1 (where n is the
length of the sequence).
Example 1: String Indexing
python
COMMANDS
text = "Python"
print(text[0]) # Output: P
print(text[3]) # Output: h
Example 2: List Indexing
python
COMMANDS
numbers = [10, 20, 30, 40, 50]
print(numbers[2]) # Output: 30
print(numbers[4]) # Output: 50
Example 3: Tuple Indexing
python
COMMANDS
tup = ('apple', 'banana', 'cherry')
print(tup[1]) # Output: banana
2. Negative Indexing
Negative indexing starts from -1 for the last element, -2 for the second-last, and
so on.
Example 4: String Negative Indexing
python
COMMANDS
text = "Python"
print(text[-1]) # Output: n
print(text[-3]) # Output: h
Example 5: List Negative Indexing
python
COMMANDS
numbers = [10, 20, 30, 40, 50]
print(numbers[-1]) # Output: 50
print(numbers[-3]) # Output: 30
Example 6: Tuple Negative Indexing
python
COMMANDS
tup = ('apple', 'banana', 'cherry')
print(tup[-2]) # Output: banana
3. Accessing Index using enumerate()
The enumerate() function allows us to get the index along with the value.
Example 7: Using enumerate()
python
COMMANDS
fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
Output:
yaml
COMMANDS
Index 0: apple
Index 1: banana
Index 2: cherry
4. Finding the Index of an Element using .index()
The .index() method finds the position of the first occurrence of an element in a
list or tuple.
Example 8: Finding an Index in a List
python
COMMANDS
numbers = [10, 20, 30, 40, 50]
print([Link](30)) # Output: 2
Example 9: Finding an Index in a Tuple
python
COMMANDS
tup = ('a', 'b', 'c', 'd')
print([Link]('c')) # Output: 2
5. Modifying Elements Using Indexing
We can change list elements using index positions. (Strings and tuples are
immutable, so they cannot be modified this way.)
Example 10: Changing a List Element
python
COMMANDS
numbers = [1, 2, 3, 4, 5]
numbers[2] = 99
print(numbers) # Output: [1, 2, 99, 4, 5]
Summary
Positive Indexing (0 to n-1) → Left to Right
Negative Indexing (-1 to -n) → Right to Left
enumerate() → Retrieves index and value
.index() → Finds the position of an element
List elements can be modified using indexes