[go: up one dir, main page]

0% found this document useful (0 votes)
15 views1 page

Abhis Work

The document summarizes common data structures in Python including lists, dictionaries, strings, and tuples. It provides examples of common methods used to manipulate each type of data structure, such as appending and removing elements from lists, getting and setting key-value pairs in dictionaries, finding and replacing substrings in strings, and accessing elements and getting lengths of tuples. These methods allow for creating, modifying, accessing, and iterating over data in these fundamental Python data types.
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)
15 views1 page

Abhis Work

The document summarizes common data structures in Python including lists, dictionaries, strings, and tuples. It provides examples of common methods used to manipulate each type of data structure, such as appending and removing elements from lists, getting and setting key-value pairs in dictionaries, finding and replacing substrings in strings, and accessing elements and getting lengths of tuples. These methods allow for creating, modifying, accessing, and iterating over data in these fundamental Python data types.
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/ 1

Data Structure Method Description Example

List append(x) Adds an element x to the end. fruits.append("mango")


insert(i, x) Inserts an element x at the specified index i. fruits.insert(1, "kiwi")
remove(x) Deletes the first occurrence of element x. fruits.remove("banana")
pop(i) Removes and returns the element at index (default -1 is last). first_apple = fruits.pop(0)
index(x) Finds the first index of element x. first_apple_index = fruits.index("apple")
sort() Sorts the list in ascending order. fruits.sort()
len() Returns the length of the list. number_of_fruits = len(fruits)
copy() Creates a shallow copy of the list. new_fruits = fruits.copy()
count(x) Counts the number of occurrences of element x. apple_count = fruits.count("apple")
reverse() Reverses the order of elements in-place. fruits.reverse()
Dictionary get(key) Retrieves the value associated with a key. age = person.get("age")
set(key, value) Adds or updates a key-value pair. person["age"] = 30
keys(), values(), and items() Returns a list of all keys, values, or both. all_keys = person.keys()
pop(key) Removes and returns a key-value pair. job = person.pop("occupation")
del key Deletes a specific key-value pair. del person["age"]
update(other_dict) Updates with key-value pairs from another dictionary. person.update({"occupation": "scientist"})
clear() Removes all key-value pairs. person.clear()
fromkeys(keys, value) Creates a new dictionary with specified keys and a default value. empty_dict = dict.fromkeys(["name", "age"], "unknown")
String upper() and lower() Change case for dramatic effect. message.upper() for uppercase
split() Divide into a list of substrings based on a delimiter. words = message.split(", ")
join(iterable) Combine elements back into a string with a separator. joined_sentence = ", ".join(words)
isdigit(): Checks if the string contains only digits. is_numeric = message.isdigit()
find(substr) and replace(old, new) Find and modify substrings. position_of_world = message.find("World")
strip() Removes whitespace from beginning and end. cleaned_message = message.strip()
startswith(prefix) Checks if the string starts with the prefix. starts_with_welcome = message.startswith("Welcome")
endswith(suffix) Checks if the string ends with the suffix. world_at_end = message.endswith("world!")
Tuple len() Discover the number of elements. number_of_players = len(player_positions)
index(x) Locate the first occurrence of an element x. bobs_index = player_positions.index("Bob")
count(x) Count the number of times an element x appears. twice_the_players = player_positions * 2
Unpacking Assign elements to multiple variables in one line. name, age = person
Operators + and * Combine or repeat tuples with ease. first_three = player_positions[:3]

You might also like