0% found this document useful (0 votes)
49 views

List Methods: Example

The format() method formats strings by inserting values into placeholders. It takes arguments, formats them, and inserts them into a string where {} are located. List methods allow you to modify lists. Common list methods include append() to add elements, clear() to remove all elements, count() to count elements, and sort() to sort the list. Set methods allow you to modify sets. Common set methods include add() to add elements, discard() to remove elements, intersection() to get the intersection of sets, and symmetric_difference() to get the symmetric difference of sets. Dictionary methods allow you to modify dictionaries. Common dictionary methods include clear() to remove all elements, items() to get a list

Uploaded by

Pranav Bajaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

List Methods: Example

The format() method formats strings by inserting values into placeholders. It takes arguments, formats them, and inserts them into a string where {} are located. List methods allow you to modify lists. Common list methods include append() to add elements, clear() to remove all elements, count() to count elements, and sort() to sort the list. Set methods allow you to modify sets. Common set methods include add() to add elements, discard() to remove elements, intersection() to get the intersection of sets, and symmetric_difference() to get the symmetric difference of sets. Dictionary methods allow you to modify dictionaries. Common dictionary methods include clear() to remove all elements, items() to get a list

Uploaded by

Pranav Bajaj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The format() method takes the passed arguments, formats them, and

places them in the string where the placeholders {} are:

Example
Use the format() method to insert numbers into strings:

age = 36
txt = "My name is John, and I am {}"
print(txt.format(age))

List Methods
Python has a set of built-in methods that you can use on lists.

Method Description

append() Adds an element at the end of the list

clear() Removes all the elements from the list

copy() Returns a copy of the list

count() Returns the number of elements with the specified value

extend() Add the elements of a list (or any iterable), to the end of the current l

index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position

pop() Removes the element at the specified position

remove() Removes the item with the specified value

reverse() Reverses the order of the list

sort() Sorts the list

Set Methods
Python has a set of built-in methods that you can use on sets.

Method Description

add() Adds an element to the set

clear() Removes all the elements from t

copy() Returns a copy of the set

difference() Returns a set containing the diff


between two or more sets

difference_update() Removes the items in this set th

included in another, specified se

discard() Remove the specified item

intersection() Returns a set, that is the interse

other sets

intersection_update() Removes the items in this set th

present in other, specified set(s)

isdisjoint() Returns whether two sets have a

or not

issubset() Returns whether another set con

or not

issuperset() Returns whether this set contain

or not

pop() Removes an element from the se


remove() Removes the specified element

symmetric_difference() Returns a set with the symmetri

Differences of two sets

symmetric_difference_update() inserts the symmetric difference

this set and another

union() Return a set containing the unio

update() Update the set with the union of

others

Dictionary Methods
Python has a set of built-in methods that you can use on dictionaries.

Method Description

clear() Removes all the elements from the dictionary

copy() Returns a copy of the dictionary


fromkeys() Returns a dictionary with the specified keys and values

get() Returns the value of the specified key

items() Returns a list containing the a tuple for each key value pair

keys() Returns a list containing the dictionary's keys

pop() Removes the element with the specified key

popitem() Removes the last inserted key-value pair

setdefault() Returns the value of the specified key. If the key does not exist: in

the key, with the specified value

update() Updates the dictionary with the specified key-value pairs

values() Returns a list of all the values in the dictionary

You might also like