Tuples and Lists
- We have understood some of the python’s basic data types
- Boolean’s
- integer’s
- Float’s
- String’s
- I want to store
- my favourite holiday spot’s
- courses which I enrolled
- Python has two sequence structures
- Tuple
- lists
- These contain zero or more elements, unlike string the elements can be of different types.
- Why does python contain both lists and tuples?
- Tuples are immutable
- Lists are mutable
Tuples
- Create with Commas and ()
- Tuple unpacking:
combo_meal = ('biryani', 'coke')
main_course, desert = combo_meal
-
Combining tuples using + :
-
Duplicate items with * :
-
Iterating over tuples: Refer Here for the example with while and for based iteration
Lists
-
Lists are good at keeping track of things/elements by their order, especially when the order and contents might change.
-
Unlike, strings or tuples lists are mutable.
-
Create with []
- Convert to list
- Convert to list
-
Converting list to tuple
-
Getting an Item by [ offset ]
-
Get Items with a Slice
-
Adding an item to the list with append()
-
Adding an Item by offset using insert()
-
Duplicate All items with *
-
Combine lists using extend() or +
-
Delete an item by offset with del
-
Delete an Item by Value with remove
-
Get and Item by Offset and Delete it with pop()
-
Delete all the items with clear
Lets write a Python Program to Store
- How do you represent the following data in python (covered so far)
name | phone num | Qual |
---|---|---|
ram | 99999999 | BTech |
shyam | 8888888 | MTech |
sita | 77777777 | MCA |
geeta | 6666666 | BSc |
[
['ram', 99999999, 'BTech'],
['shyam',8888888,'Mtech' ],
['sita', 77777777, 'MCA'],
['geeta', 6666666, 'BSc'],
]
- Refer Here for the code created in the class