28
loading...
This website collects cookies to deliver better user experience
#creating empty values
cars = []
print(cars)
#adding elements to a list
cars = ["Audi",3.2,"Subaru",5,"Romeo",432.789]
print(cars)
Output:
[]
["Audi",3.2,"Subaru",5,"Romeo",432.789]
Methods | 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 list |
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 first item with the specified value |
reverse() | Reverses the order of the list |
sort() | Sorts the list |
cars = ["Audi",3.2,"Subaru",5,"Romeo",432.789]
print(cars)
# append
cars.append("Toyota")
print(cars)
#copy
cars_1 = cars.copy()
print(cars_1)
# clear
cars_1.clear()
print(cars_1)
# count
print(cars.count("Audi"))
# Extend
fruit = ["kiwi","cherry","grapes"]
city = ["morotro","iraq","texas","qalit"]
fruit.extend(city)
print(fruit)
# index
cars = ["Audi",3.2,"Subaru",5,"Toyota","Romeo",432.789]
print(cars.index("Toyota"))
#insert
fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)
#pop
fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)
output:
["Audi",3.2,"Subaru",5,"Romeo",432.789]
["Audi",3.2,"Subaru",5,"Romeo",432.789,"Toyota"]
["Audi",3.2,"Subaru",5,"Romeo",432.789]
[]
1
["kiwi","cherry","grapes","morotro","iraq","texas","qalit"]
4
['apple','orange','banana', 'cherry']
['apple','cherry']
realk = ("Texas","Estonia","Monaco")
print(realk)
# duplicate values
realk1 = ("Texas","Estonia","Monaco","Texas")
print(realk1)
# No parentheses
realk = 'Texas','Estonia','Monaco'
print(realk)
# Heterogeneous data
employee=(1, 'Steve', True, 25, 12000)
print(employee)
output:
('Texas','Estonia','Monaco')
('Texas','Estonia','Monaco','Texas')
('Texas','Estonia','Monaco')
(1, 'Steve', True, 25, 12000)
Operator |
---|
The + operator returns a tuple containing all the elements of the first and the second tuple object. |
t1=(1,2,3)
t2=(4,5,6)
t1+t2
(1, 2, 3, 4, 5, 6)
t2+(7,)
(4, 5, 6, 7)
Operator |
---|
The * operator Concatenates multiple copies of the same tuple. |
>>> t1=(1,2,3)
>>> t1*4
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
Operator |
---|
The [] operator Returns the item at the given index. A negative index counts the position from the right side. |
>>> t1=(1,2,3,4,5,6)
>>> t1[3]
4
>>> t1[-2]
5
Operator |
---|
The [:] operator returns the items in the range specified by two index operands separated by the : symbol. |
If the first operand is omitted, the range starts from zero. If the second operand is omitted, the range goes up to the end of the tuple. |
>>> t1=(1,2,3,4,5,6)
>>> t1[1:3]
(2, 3)
>>> t1[3:]
(4, 5, 6)
>>> t1[:3]
(1, 2, 3)
Operator |
---|
The in operator returns true if an item exists in the given tuple. |
>>> t1=(1,2,3,4,5,6)
>>> 5 in t1
True
>>> 10 in t1
False
Operator |
---|
The not in operator returns true if an item does not exist in the given tuple. |
>>> t1=(1,2,3,4,5,6)
>>> 4 not in t1
False
>>> 10 not in t1
True
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
members.clear()
print(members)
Output:
{}
Method | Description |
---|---|
copy() | Returns a copy of the dictionary |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
copy_members = members.copy()
print(copy_members)
Output:
{'sales':'Rhorda','Accounts':'Mustafa','Technical':'Naliaka'}
Method | Description |
---|---|
fromkeys() | Returns a dictionary with the specified keys and value.Can be used to make dictionary with the same values |
departmet = ("account","IT","Engineering")
members = ("Tityo")
dict_1 = dict.fromkeys(departmet,members)
print(dict_1)
Output:
{'account': 'Tityo', 'IT': 'Tityo', 'Engineering': 'Tityo'}
Method | Description |
---|---|
get() | Returns the value of the specified key |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Acc_1 = members.get("Accounts")
print(Acc_1)
Output:
Mustafa
Method | Description |
---|---|
items() | Returns a list containing a tuple for each key value pair.The view object contains the key-value pairs of the dictionary, as tuples in a list. |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Acc_1 = members.items()
print(Acc_1)
Output:
dict_items([('sales', 'Rhorda'), ('Accounts', 'Mustafa'), ('Technical', 'Naliaka')])
Method | Description |
---|---|
keys() | Returns a list containing the dictionary's keys.The view object contains the keys of the dictionary, as a list. |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Acc_1 = members.keys()
print(Acc_1)
Output:
dict_items(['sales','Accounts','Technical'])
Method | Description |
---|---|
pop() | Removes the element with the specified key |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Remove_member = members.pop("sales")
print(Remove_member)
Output:
{'Accounts':'Mustafa','Technical':'Naliaka'}
Method | Description |
---|---|
popitem() | Removes the last inserted key-value pair |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Remove_member = members.popitem()
print(Remove_member)
Output:
{'Accounts':'Mustafa','Accounts': 'Mustafa'}
Method | Description |
---|---|
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
Remove_member = members.setdefault("sales","phagra")
print(Remove_member)
Output:
Rhorda
Method | Description |
---|---|
update() | Updates the dictionary with the specified key-value pairs.The specified items can be a dictionary, or an iterable object with key value pairs. |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
New_member = members.update({"Engineering":"phagra"})
print(New_member)
Output:
{'sales':'Rhorda','Accounts':'Mustafa','Technical':'Naliaka','Engineering':'phagra'}
Method | Description |
---|---|
values() | Returns a list of all the values in the dictionary.The view object contains the values of the dictionary, as a list.The view object will reflect any changes done to the dictionary |
members = {
"sales": "Rhorda",
"Accounts": "Mustafa",
"Technical": "Naliaka"
}
member_values = members.values()
print(member_values)
Output:
dict_values(['Rhorda','Mustafa','Naliaka'])
Method | Description |
---|---|
add() | Adds an element to the set |
x = {"facebook", "instagram", "whatsapp"}
x.add("twitter")
print(x)
Output:
{'facebook', 'instagram', 'whatsapp','twitter'}
Method | Description |
---|---|
clear() | Removes all the elements from the set |
x = {"facebook", "instagram", "whatsapp"}
x.clear()
print(x)
Output:
set()
Method | Description |
---|---|
copy() | Returns a copy of the set |
x = {"facebook", "instagram", "whatsapp"}
y = x.copy()
print(y)
Output:
{'facebook','instagram', 'whatsapp'}
Method | Description |
---|---|
difference() | Returns a set containing the difference between two or more sets |
Method | Description |
---|---|
difference_update() | Removes the items in this set that are also included in another, specified set |
Method | Description |
---|---|
discard() | Remove the specified item |
Method | Description |
---|---|
intersection() | Returns a set, that is the intersection of two other sets |
Method | Description |
---|---|
intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
Method | Description |
---|---|
isdisjoint() | Returns whether two sets have a intersection or not |
Method | Description |
---|---|
issubset() | Returns whether another set contains this set or not |
Method | Description |
---|---|
issuperset() | Returns whether this set contains another set or not |
Method | Description |
---|---|
pop() | Removes an element from the set randomly |
x = {"facebook", "instagram", "whatsapp"}
x.pop()
print(x)
Output:
{'instagram','whatsapp'}
Method | Description |
---|---|
remove() | Removes the specified element |
x = {"facebook", "instagram", "whatsapp"}
x.remove("facebook")
print(x)
Output:
{'instagram','whatsapp'}
Method | Description |
---|---|
symmetric_difference() | Returns a set with the symmetric differences of two sets |
x = {"facebook", "instagram", "whatsapp"}
y = {"google", "microsoft", "facebook"}
x.symmetric_difference(y)
print(x)
Output:
{'instagram','whatsapp','google','microsoft'}
Method | Description |
---|---|
symmetric_difference_update() | inserts the symmetric differences from this set and another |
x = {"facebook", "instagram", "whatsapp"}
y = {"google", "microsoft", "facebook"}
x.symmetric_difference_update(y)
print(x)
Output:
{'instagram','whatsapp','google','microsoft'}
Method | Description |
---|---|
union() | Return a set containing the union of sets |
x = {"facebook", "instagram", "whatsapp"}
y = {"google", "microsoft", "apple"}
w = {"google","twitch","whatsapp"}
x.union(y,w)
print(x)
Output:
{'facebook','instagram','microsoft','apple','whatsapp','google','twitch'}
Method | Description |
---|---|
update() | Update the set with the union of this set and others |
x = {"facebook", "instagram", "whatsapp"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
Output:
{'facebook','instagram','whatsapp','google','microsoft','apple'}