Type Conversion in Python
Type Conversion in Python
2
6. List, Tuple, and Set Conversions:
You can convert between list, tuple, and set types.
Lists in Python
In Python, lists are versatile data structures used to store collections of
items. They are ordered, mutable (modifiable), and allow duplicate
elements. Here are some key points about lists in Python:
1. Declaration: Lists are declared using square brackets `[]` and can
contain elements of different data types.
```
3
2. Accessing Elements: Elements in a list are accessed by their index.
Indexing starts at 0.
```python
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # Output: 10
```
4
7. Common List Methods:
• `append()`: Adds an element to the end of the list.
• `insert()`: Inserts an element at a specified index.
• `pop()`: Removes and returns the element at a given index (by
default, the last element).
• `index()`: Returns the index of the first occurrence of a value.
• `len()`: Returns the number of elements in a list.
• extend(): adds one list at the end of another list.
• reverse():Reverses the elements of the list.
• count(): this method counts how many times an element has
occurred in a list
• remove() : it is when we know the element to be deleted , not the
index.
Lists in Python are fundamental and widely used due to their flexibility
and capability to handle various types of data structures efficiently.
Traversing a List
Accessing each element of a list.
a) Using ‘in’ operator inside for loop
L=list(“python”)
for i in L:
print(i)
5
b) Using range() function range() function can be used for accessing
individual elements of a list, using indexes of elements only, along
with len() method.
L=list(“python”)
n = len(L)
for i in range(n):
print(L[i])
Tuples
In Python, tuples are ordered collections similar to lists, but they have
a few key differences:
6
3. Single-element Tuple: If a tuple has only one element, a comma is
required at the end to differentiate it from a simple expression in
parentheses.
6. Use Cases: Tuples are commonly used for fixed collections of items,
such as coordinates, database records, or any group of related,
immutable data. They can also be used as keys in dictionaries because
of their immutability.
7
8. Functions Returning Multiple Values: Functions can return multiple
values as a tuple, allowing easy extraction of these values.
Tuple Fucntions
While tuples don't have as many methods as lists due to their
immutability, they do have a few functions and operations that are
available:
8
4. `sorted()`: Creates a sorted list from the elements of a tuple. Since
tuples are immutable, this returns a list.
6) any() – Returns True if a tuple is having at least one item. If the tuple
is empty it returns False.
Syntx: any(tuple_name)
7) min() - Returns the element with minimum value from the tuple.
Syntax: min(tuple_name)
8) max() – Returns the element with maximum value from the tuple.
Syntax: max(tuple_name)
9
Traversing a Tuple
a) Using in operator with for loop
tup=tuple(‘python’)
for i in tup:
print(i)
b) Using range()
tup=tuple(‘python’)
n=len(tup)
for i in range(n):
print(tup[i]
Sample Program
1)Write a program to create a list of odd and even numbers from another
list
A)
10
2)
Write a program to fund the maximum and minimum number from a tuple
A)
11