Python list tuple dictionary
Python list tuple dictionary full notes in english and tamil
Sure, I'd be happy to provide you with an overview of Python lists, tuples, and dictionaries.
## Lists:
A list in Python is an ordered collection of items that can hold a mix of different data types. Lists are defined using square brackets `[]` and items are separated by commas. They are mutable, meaning you can modify their contents after creation.
### Creating Lists:
```python
my_list = [1, 2, 3, 'apple', 'banana', True]
empty_list = []
```
### Accessing Elements:
You can access elements in a list using indexing (starting from 0) and negative indexing (starting from -1 for the last element).
```python
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: True
```
### List Methods:
- `append(item)`: Adds an item to the end of the list.
- `insert(index, item)`: Inserts an item at the specified index.
- `remove(item)`: Removes the first occurrence of the specified item.
- `pop(index)`: Removes and returns the item at the specified index.
- `index(item)`: Returns the index of the first occurrence of the item.
- `len(list)`: Returns the number of elements in the list.
## Tuples:
A tuple is similar to a list but is immutable, meaning you can't change its contents after creation. Tuples are defined using parentheses `()` or just commas.
### Creating Tuples:
```python
my_tuple = (1, 2, 3, 'apple', 'banana')
single_item_tuple = (5,) # Note the comma after the single item
```
### Accessing Elements:
You access tuple elements in the same way as list elements, using indexing and negative indexing.
### Tuple Methods:
Tuples have fewer methods compared to lists because they are immutable. Common methods include `count()` and `index()`.
## Dictionaries:
A dictionary is an unordered collection of key-value pairs. Each key must be unique, and keys are used to access their corresponding values. Dictionaries are defined using curly braces `{}`.
### Creating Dictionaries:
```python
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
empty_dict = {}
```
### Accessing Values:
You can access values in a dictionary using keys.
```python
print(my_dict['name']) # Output: John
```
### Dictionary Methods:
- `keys()`: Returns a list of all the keys.
- `values()`: Returns a list of all the values.
- `items()`: Returns a list of key-value tuples.
- `get(key, default)`: Returns the value for the specified key. If the key doesn't exist, it returns the default value.
Remember that Python's documentation is a great resource for detailed information on these data structures: https://docs.python.org/3/tutorial/datastructures.html
Keep in mind that the above information is just a brief overview of lists, tuples, and dictionaries in Python. There's a lot more you can do with these data structures, so feel free to explore and experiment!
Comments
Post a Comment