training day4
SETS
fruits = {"apple","banana","cherries"}
for x in fruits:
print(x)
To check whether the item is there or not in the set use :
print("banana" in fruits)
It will tell only true or false but not index because it is unindexed.
To add any single item
fruits.add("orange")
If we want to add multiple items
fruits.update(["mango","orange"])
If there is no item in the set, we want to remove it will give error
fruits.remove("grapes")
To remove such kind of errors we use discard function
fruits.discard("Grapes")
ouput :
{"apple","banana","cherries"}
DICTIONARIES
It's basic syntax is {"key":"value"}
Example : details = {"name":"John",
"age":20,
"address" : "xyz"}
To get specified field
Example : print(detail["age"])
To change any detail
Example : detail ["age"]=25
To add any item
Example : detail["color"] = "red"
print(detail)
To remove any item
Example : detail.pop["age"]
print (detail)
LOOPS in dictionaries6
for x in detail:
print(x)
This will print only keys.
for x in details:
print(detail[x])
This will print only values.
for x in detail.values():
print(x)
This will print only values.
for x,y in detail.items():
print(x,y)
This will print key as well as values.
Program to convert integer into words
dic = {"1":"one","2":"Two","3":"Three","4":"Four","5":"five","6":"six","7":"seven","8":"eight","9":"nine"}y = input("Enter combination of integers")
s = " "
for x in y:
z = (dic.get(x))
s += z + " "
print(s)
Comments
Post a Comment