LIST
In Python, a list is a built-in data type that represents an ordered sequence of elements. Lists are versatile and can contain elements of different data types, including numbers, strings, and even other lists. Here are some basic operations you can perform on lists:
1. Creating a List:
pythonmy_list = [1, 2, 3, "apple", "banana"]
2. Accessing Elements:
You can access elements in a list using their indices. Indices start from 0.
pythonfirst_element = my_list[0] # Access the first element
second_element = my_list[1] # Access the second element
last_element = my_list[-1] # Access the last element
3. Slicing:
You can extract a subset of a list using slicing.
pythonsubset = my_list[1:4] # Extract elements from index 1 to 3 (excluding 4)
4. Appending Elements:
You can add elements to the end of a list using the append()
method.
pythonmy_list.append("orange")
5. Inserting Elements:
You can insert an element at a specific position using the insert()
method.
pythonmy_list.insert(1, "grape") # Insert "grape" at index 1
6. Removing Elements:
You can remove elements by value using the remove()
method or by index using the pop()
method.
pythonmy_list.remove("banana") # Remove the element with the value "banana"
popped_element = my_list.pop(2) # Remove and return the element at index 2
7. Updating Elements:
You can update the value of an element by assigning a new value.
pythonmy_list[0] = 100 # Update the value at index 0 to 100
8. Length of a List:
You can get the length of a list using the len()
function.
pythonlength = len(my_list)
9. Concatenating Lists:
You can concatenate two lists using the +
operator.
pythonnew_list = my_list + [5, 6, 7]
10. Checking Membership:
You can check if an element is present in a list using the in
keyword.
pythonis_present = "apple" in my_list
11. Iterating Over a List:
You can iterate over the elements of a list using a for
loop.
pythonfor item in my_list:
print(item)
These are some of the basic operations you can perform on lists in Python. Lists are widely used for storing and manipulating data in various programming scenarios.
Dictionary
In Python, a dictionary is a built-in data type that represents an unordered collection of key-value pairs. Each key in a dictionary must be unique, and it is associated with a specific value. Dictionaries are defined by curly braces {}
. Here are some basic operations you can perform on dictionaries:
1. Creating a Dictionary:
pythonmy_dict = {"name": "John", "age": 25, "city": "New York"}
2. Accessing Values:
You can access the value associated with a key using square brackets []
.
pythonname = my_dict["name"]
age = my_dict["age"]
3. Updating Values:
You can update the value associated with a key.
pythonmy_dict["age"] = 26
4. Adding New Key-Value Pairs:
You can add new key-value pairs to a dictionary.
pythonmy_dict["occupation"] = "Engineer"
5. Removing Key-Value Pairs:
You can remove a key-value pair using the del
keyword or the pop()
method.
pythondel my_dict["city"] # Remove the key-value pair with key "city"
removed_value = my_dict.pop("age") # Remove and return the value for the key "age"
6. Checking Membership:
You can check if a key is present in a dictionary using the in
keyword.
pythonis_present = "age" in my_dict
7. Getting Keys and Values:
You can get a list of all keys or values using the keys()
and values()
methods, respectively.
pythonall_keys = my_dict.keys() all_values = my_dict.values()
8. Getting Key-Value Pairs:
You can get a list of all key-value pairs using the items()
method.
pythonall_items = my_dict.items()
9. Iterating Over a Dictionary:
You can iterate over the keys, values, or items of a dictionary using a for
loop.
python# Iterate over keys
for key in my_dict:
print(key)
# Iterate over values
for value in my_dict.values():
print(value)
# Iterate over key-value pairs
for key, value in my_dict.items():
print(key, value)
10. Length of a Dictionary:
You can get the number of key-value pairs in a dictionary using the len()
function.
pythonlength = len(my_dict)
Dictionaries are commonly used for representing structured data and are suitable for scenarios where you need to quickly look up values based on unique keys.
Comments
Post a Comment