0% found this document useful (0 votes)
6 views7 pages

Basic_Operations_With_Examples_Python_Lists_Tuples_Sets_With_Dictionaries

The document provides an overview of basic operations in Python for lists, tuples, sets, and dictionaries, including examples for each operation. It covers concatenation, repetition, accessing elements, membership checks, and various methods for modifying lists, tuples, and sets. Additionally, it explains dictionary creation, value access, item addition/removal, and looping through key-value pairs.

Uploaded by

Shruti Shevade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Basic_Operations_With_Examples_Python_Lists_Tuples_Sets_With_Dictionaries

The document provides an overview of basic operations in Python for lists, tuples, sets, and dictionaries, including examples for each operation. It covers concatenation, repetition, accessing elements, membership checks, and various methods for modifying lists, tuples, and sets. Additionally, it explains dictionary creation, value access, item addition/removal, and looping through key-value pairs.

Uploaded by

Shruti Shevade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Basic Operations in Python: Lists, Tuples, and Sets (With Examples)

1. List Operations:

- Concatenation: Combine two lists using '+' operator.

Example:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

result = list1 + list2

print(result) # Output: [1, 2, 3, 4, 5, 6]

- Repetition: Repeat elements of a list using '*' operator.

Example:

my_list = [1, 2, 3]

result = my_list * 3

print(result) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

- Accessing Elements: Use indexing (e.g., list[0], list[-1]) to access list items.

Example:

my_list = [10, 20, 30, 40]

print(my_list[0]) # Output: 10

print(my_list[-1]) # Output: 40

- Slicing: Extract a sublist using list[start:end].

Example:

my_list = [10, 20, 30, 40, 50]

result = my_list[1:4]
print(result) # Output: [20, 30, 40]

- Membership: Check if an item exists using 'in' operator (e.g., 20 in list).

Example:

my_list = [10, 20, 30, 40]

print(20 in my_list) # Output: True

print(50 in my_list) # Output: False

- Appending: Add an element to the end of the list using append().

Example:

my_list = [10, 20, 30]

my_list.append(40)

print(my_list) # Output: [10, 20, 30, 40]

- Inserting: Insert an element at a specific position using insert().

Example:

my_list = [10, 20, 30]

my_list.insert(1, 15)

print(my_list) # Output: [10, 15, 20, 30]

- Removing Elements: Remove items using remove() or pop() method.

Example:

my_list = [10, 20, 30, 40]

my_list.remove(20)

print(my_list) # Output: [10, 30, 40]

removed_item = my_list.pop(1)

print(removed_item) # Output: 30
print(my_list) # Output: [10, 40]

- Sorting: Sort a list in ascending/descending order using sort() or sorted().

Example:

my_list = [40, 10, 30, 20]

my_list.sort()

print(my_list) # Output: [10, 20, 30, 40]

sorted_list = sorted(my_list, reverse=True)

print(sorted_list) # Output: [40, 30, 20, 10]

- Reversing: Reverse the list using reverse() or slicing [::-1].

Example:

my_list = [10, 20, 30, 40]

my_list.reverse()

print(my_list) # Output: [40, 30, 20, 10]

reversed_list = my_list[::-1]

print(reversed_list) # Output: [10, 20, 30, 40]

- Clearing: Remove all elements from a list using clear().

Example:

my_list = [10, 20, 30, 40]

my_list.clear()

print(my_list) # Output: []

2. Tuple Operations:

- Concatenation: Combine two tuples using '+' operator.


Example:

tuple1 = (1, 2, 3)

tuple2 = (4, 5, 6)

result = tuple1 + tuple2

print(result) # Output: (1, 2, 3, 4, 5, 6)

- Repetition: Repeat elements of a tuple using '*' operator.

Example:

my_tuple = (1, 2, 3)

result = my_tuple * 3

print(result) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

- Membership: Check if an item exists in the tuple using 'in' operator.

Example:

my_tuple = (10, 20, 30, 40)

print(20 in my_tuple) # Output: True

print(50 in my_tuple) # Output: False

- Counting Occurrences: Use count() method to count an item in a tuple.

Example:

my_tuple = (10, 20, 20, 30)

count = my_tuple.count(20)

print(count) # Output: 2

- Finding Index: Use index() method to find the first occurrence of an item.

Example:

my_tuple = (10, 20, 30, 40)


index = my_tuple.index(30)

print(index) # Output: 2

3. Set Operations:

- Adding Elements: Add elements to a set using add().

Example:

my_set = {10, 20, 30}

my_set.add(40)

print(my_set) # Output: {10, 20, 30, 40}

- Removing Elements: Remove elements using remove() or discard() method.

Example:

my_set = {10, 20, 30, 40}

my_set.remove(20)

print(my_set) # Output: {10, 30, 40}

my_set.discard(50) # Does not raise error even if 50 is not found

print(my_set) # Output: {10, 30, 40}

- Membership: Check if an item exists using 'in' operator.

Example:

my_set = {10, 20, 30, 40}

print(20 in my_set) # Output: True

print(50 in my_set) # Output: False

- Union: Combine two sets using union() or | operator.

Example:
set1 = {10, 20, 30}

set2 = {30, 40, 50}

result = set1.union(set2)

print(result) # Output: {10, 20, 30, 40, 50}

result = set1 | set2

print(result) # Output: {10, 20, 30, 40, 50}

- Intersection: Find common elements using intersection() or & operator.

Example:

set1 = {10, 20, 30}

set2 = {30, 40, 50}

result = set1.intersection(set2)

print(result) # Output: {30}

result = set1 & set2

print(result) # Output: {30}

- Difference: Find elements in one set but not another using difference() or - operator.

Example:

set1 = {10, 20, 30}

set2 = {30, 40, 50}

result = set1.difference(set2)

print(result) # Output: {10, 20}

result = set1 - set2

print(result) # Output: {10, 20}


4. Dictionary Operations:

- Creating a Dictionary:
Example:
my_dict = {"name": "Alice", "age": 25, "city": "Pune"}
print(my_dict)
# Output: {'name': 'Alice', 'age': 25, 'city': 'Pune'}

- Accessing Values:
Example:
print(my_dict["name"]) # Output: Alice
print(my_dict.get("age")) # Output: 25

- Adding or Updating Items:


Example:
my_dict["age"] = 26
my_dict["country"] = "India"
print(my_dict)
# Output: {'name': 'Alice', 'age': 26, 'city': 'Pune', 'country': 'India'}

- Removing Items:
Example:
my_dict.pop("city")
print(my_dict)
# Output: {'name': 'Alice', 'age': 26, 'country': 'India'}

del my_dict["age"]
print(my_dict)
# Output: {'name': 'Alice', 'country': 'India'}

- Checking Membership:
Example:
print("name" in my_dict) # Output: True
print("city" in my_dict) # Output: False

- Looping Through Dictionary:


Example:
for key, value in my_dict.items():
print(key, ":", value)
# Output:
# name : Alice
# country : India

- Getting Keys, Values, and Items:


Example:
print(my_dict.keys()) # Output: dict_keys(['name', 'country'])
print(my_dict.values()) # Output: dict_values(['Alice', 'India'])
print(my_dict.items()) # Output: dict_items([('name', 'Alice'), ('country', 'India')])

You might also like