Monday, October 21, 2019

Learning Python- Day 6


DAY 6

dict1={
   
"Abhi":"Noida",
   
"Nitin":"Gr. Noida"
    
}
print("Dict is",dict1)
run the function CTL+shift+fn+F10
Dict is {'Abhi': 'Noida', 'Nitin': 'Gr. Noida'}

In the above program, Abhi is the key and Noida is the value. Key and value will always be together in dictionary. The key is analogous to the index, they are like addresses but they don’t have to be
integers. They are usually characters; the values are
similar to the elements in a list and contain information.
To create a dictionary, we use curly brackets. The keys are the first elements; they must
be immutable and unique. Each key is followed by a value separated
by a colon. The values can be immutable, mutable and duplicates.
Each key and value pair is separated by a comma.

To add new value to the dictionary

dict1[
"Rohit"]="Delhi"
dict1["Tom"]="Pune"
dict1["John"]="Agra"
dict1["Joy"]="Jaipur"

Program:
dict1={
   
"Abhi":"Noida",
   
"Nitin":"Gr. Noida"
    
}
dict1[
"Rohit"]="Delhi"
dict1["Tom"]="Pune"
dict1["John"]="Agra"
dict1["Joy"]="Jaipur"
print(dict1)
run the function CTL+shift+fn+F10
{'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Rohit': 'Delhi', 'Tom': 'Pune', 'John': 'Agra', 'Joy': 'Jaipur'}



dict2=dict1.copy()
print("Dict2 is ",dict2)
run the function CTL+shift+fn+F10
Dict2 is  {'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Rohit': 'Delhi', 'Tom': 'Pune', 'John': 'Agra', 'Joy': 'Jaipur'}

print(dict2["Tom"])
run the function CTL+shift+fn+F10
Pune

print(dict2.keys())
run the function CTL+shift+fn+F10

dict_keys(['Abhi', 'Nitin', 'Rohit', 'Tom', 'John', 'Joy'])

print(dict2.values())
run the function CTL+shift+fn+F10
dict_values(['Noida', 'Gr. Noida', 'Delhi', 'Pune', 'Agra', 'Jaipur'])

print(dict2.items())
run the function CTL+shift+fn+F10
dict_items([('Abhi', 'Noida'), ('Nitin', 'Gr. Noida'), ('Rohit', 'Delhi'), ('Tom', 'Pune'), ('John', 'Agra'),

print(dict2.get("Nitin"))
run the function CTL+shift+fn+F10
Gr. Noida

print(dict2.popitem())
run the function CTL+shift+fn+F10
('Joy', 'Jaipur')

print(dict2.pop("Rohit"))
run the function CTL+shift+fn+F10
Delhi

print(dict2)
run the function CTL+shift+fn+F10

{'Abhi': 'Noida', 'Nitin': 'Gr. Noida', 'Tom': 'Pune', 'John': 'Agra'}

print("Nitin" in dict2)
print("Joy" in dict2)
print("Joy" not in dict2)

dict2.clear()
print(dict2)

run the function CTL+shift+fn+F10

{}

del dict2
#print(dict2)
run the function CTL+shift+fn+F10

   print(dict2)
NameError: name 'dict2' is not defined



for i in dict1.items():
    print(i)
run the function CTL+shift+fn+F10

('Abhi', 'Noida')
('Nitin', 'Gr. Noida')
('Rohit', 'Delhi')
('Tom', 'Pune')
('John', 'Agra')
('Joy', 'Jaipur')

for i in dict1.keys():
    print(i)
run the function CTL+shift+fn+F10


Abhi
Nitin
Rohit
Tom
John
Joy


for i in dict1.values():
    print(i)
run the function CTL+shift+fn+F10
Noida
Gr. Noida
Delhi
Pune
Agra
Jaipur

for i,j in dict1.items():
    print(i,j)
run the function CTL+shift+fn+F10
Abhi Noida
Nitin Gr. Noida
Rohit Delhi
Tom Pune
John Agra
Joy Jaipur

SETS


Let’s cover sets; they are also a type of collection.Sets are a type of collection. This means that like lists and tuples, you can input different python types. Unlike lists and tuples they are unordered.This means sets do not record element position. Sets only have unique elements. This means there is only one of a particular element in a set.To define a set, you use curly brackets You place the elements of a set within the curly brackets. You notice there are duplicate items. When the actual set is created, duplicate items will not be present.
You can convert a list to a set by using the function set; this is called type-casting. You simply use the list as the input to the function set. The result will be a list converted to a set.

s1={10,20,30,40,50,10}
print(s1)
run the function CTL+shift+fn+F10
{40, 10, 50, 20, 30}

s1.add(
60)
run the function CTL+shift+fn+F10
{40, 10, 50, 20, 60, 30}

s1.update([
70,80,90])
run the function CTL+shift+fn+F10

{70, 40, 10, 80, 50, 20, 90, 30}


print(len(s1))
run the function CTL+shift+fn+F10
8
print(max(s1))
run the function CTL+shift+fn+F10
90
print(min(s1))
run the function CTL+shift+fn+F10
10

s1.remove(
10)
print(s1)
run the function CTL+shift+fn+F10
{70, 40, 80, 50, 20, 90, 30}

#s1.remove(100)
s1.discard(1000)

print(s1.pop())

print(100 in s1)
print(100 not in s1)

s2={
10,20,30,40,50}
s3={
40,50,60,70,80}

s4=s2.union(s3)
print(s4)

s5=s2.intersection(s3)
print(s5)

s6=s2.difference(s3)
print(s6)

s7=s2.symmetric_difference(s3)
print(s7)

print(s2,s3)

#s2.intersection_update(s3)
#s2.difference_update(s3)
s2.symmetric_difference_update(s3)
print(s2)


s8={
10,20,30,40}
s9={
30,40}
s10={
50,60,70}

print(s9.issubset(s8))
print(s8.issuperset(s9))
print(s8.isdisjoint(s10))

Learning Python- Day 7

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...