Saturday, 13 February 2021

Dictionary and Stack in Python

 

Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>> D={}

>>> D

{}

>>> type(D)

<class 'dict'>

>>> D1=dict()

>>> D1

{}

>>> type(D1)

<class 'dict'>

>>> D[1]="Ram"

>>> D

{1: 'Ram'}

>>> D[2]="Raj"

>>> D[5]="Sam"

>>> D["id"]=112

>>> D

{1: 'Ram', 2: 'Raj', 5: 'Sam', 'id': 112}

>>> D1={1:'Ram',2:'Raj'}

>>> D1

{1: 'Ram', 2: 'Raj'}

>>> D[5]

'Sam'

>>> D['id']

112

>>> D[4]

Traceback (most recent call last):

  File "<pyshell#16>", line 1, in <module>

    D[4]

KeyError: 4

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1

8

3

id

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Ram

Raj

Sam

122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1 : Ram

8 : Raj

3 : Sam

id : 122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

{1: 'Ram', 8: 'Raj', 3: 'Sam', 'id': 122}

>>> a=1:"Ram"

SyntaxError: invalid syntax

>>> a=1

>>> a="Ram"

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

{1: 'Ram', 8: 'Raj', 3: 'Sam', 'id': 122}

1 = Ram

8 = Raj

3 = Sam

id = 122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1 = Ram

8 = Raj

3 = Sam

id = 122

{1: 'Ram', 8: 'Raj', 3: 'Sam', 'id': 122, 5: 'John'}

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1 = Ram

8 = Raj

3 = Sam

id = 122

{1: 'Ram', 8: 'Raj', 3: 'Sam', 'id': 122, 5: 'John'}

{1: 'Amit', 8: 'Raj', 3: 'Sam', 'id': 122, 5: 'John'}

>>> d1={1:10,2:20,3:30}

>>> d2={4:40,5:50}

>>> d1

{1: 10, 2: 20, 3: 30}

>>> d2

{4: 40, 5: 50}

>>> d1+d2

Traceback (most recent call last):

  File "<pyshell#24>", line 1, in <module>

    d1+d2

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

>>> d1.update(d2)

>>> d1

{1: 10, 2: 20, 3: 30, 4: 40, 5: 50}

>>> d2

{4: 40, 5: 50}

>>> d3={1:40,2:50,3:70}

>>> d1.update(d3)

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> del d1[3]

>>> d1

{1: 40, 2: 50, 4: 40, 5: 50}

>>> del d1

>>> d1

Traceback (most recent call last):

  File "<pyshell#34>", line 1, in <module>

    d1

NameError: name 'd1' is not defined

>>> d1={1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1.pop(3)

70

>>> d1

{1: 40, 2: 50, 4: 40, 5: 50}

>>> 1 in d1

True

>>> 5 in d1

True

>>> 3 in d1

False

>>> 40 in d1

False

>>> 40 not in d1

True

>>> d1

{1: 40, 2: 50, 4: 40, 5: 50}

>>> len(d1)

4

>>> d.clear()

Traceback (most recent call last):

  File "<pyshell#46>", line 1, in <module>

    d.clear()

NameError: name 'd' is not defined

>>> d1.clear()

>>> d1

{}

>>> d1={1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1[2]

50

>>> d1.get(2)

50

>>> d1[9]

Traceback (most recent call last):

  File "<pyshell#53>", line 1, in <module>

    d1[9]

KeyError: 9

>>> d1.get(9)

>>> d1.get(9,"This key is not available")

'This key is not available'

>>> d1.get(2,"This key is not available")

50

>>> print(d1.get(9))

None

>>> d1.items()

dict_items([(1, 40), (2, 50), (3, 70), (4, 40), (5, 50)])

>>> d1.keys()

dict_keys([1, 2, 3, 4, 5])

>>> d1.values()

dict_values([40, 50, 70, 40, 50])

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1 = Ram

8 = Raj

3 = Sam

id = 122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1 = Ram

8 = Raj

3 = Sam

id = 122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

1

8

3

id

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Ram

Raj

Sam

122

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

(1, 'Ram')

(8, 'Raj')

(3, 'Sam')

('id', 122)

>>> d1

Traceback (most recent call last):

  File "<pyshell#61>", line 1, in <module>

    d1

NameError: name 'd1' is not defined

>>> d1={1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d2

Traceback (most recent call last):

  File "<pyshell#64>", line 1, in <module>

    d2

NameError: name 'd2' is not defined

>>> d2=d1.copy()

>>> d2

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d2[1:4]

Traceback (most recent call last):

  File "<pyshell#67>", line 1, in <module>

    d2[1:4]

TypeError: unhashable type: 'slice'

>>> k=[1,2,3,4]

>>> d3=d.fromkeys(k,10)

Traceback (most recent call last):

  File "<pyshell#69>", line 1, in <module>

    d3=d.fromkeys(k,10)

NameError: name 'd' is not defined

>>> d3=dict.fromkeys(k,10)

>>> d3

{1: 10, 2: 10, 3: 10, 4: 10}

>>> d4=dict.fromkeys(k)

>>> d4'

SyntaxError: EOL while scanning string literal

>>> d4

{1: None, 2: None, 3: None, 4: None}

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> k=[4,8,2,"ram"]

>>> d4=dict.fromkeys(k,30)

>>> d4

{4: 30, 8: 30, 2: 30, 'ram': 30}

>>> d1

{1: 40, 2: 50, 3: 70, 4: 40, 5: 50}

>>> d1.pop(2)

50

>>> d1

{1: 40, 3: 70, 4: 40, 5: 50}

>>> d1.popitem()

(5, 50)

>>> d1

{1: 40, 3: 70, 4: 40}

>>> min(d1)

1

>>> max(d1)

4

>>> d4

{4: 30, 8: 30, 2: 30, 'ram': 30}

>>> min(d4)

Traceback (most recent call last):

  File "<pyshell#87>", line 1, in <module>

    min(d4)

TypeError: '<' not supported between instances of 'str' and 'int'

>>> sorted(d4)

Traceback (most recent call last):

  File "<pyshell#88>", line 1, in <module>

    sorted(d4)

TypeError: '<' not supported between instances of 'str' and 'int'

>>> k=[3,7,2,1,9]

>>> d1=dict.fromkeys(k,30)

>>> d1

{3: 30, 7: 30, 2: 30, 1: 30, 9: 30}

>>> sorted(d1)

[1, 2, 3, 7, 9]

>>> sorted(d1.keys())

[1, 2, 3, 7, 9]

>>> sorted(d1.values())

[30, 30, 30, 30, 30]

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py", line 5, in <module>

    if i%2==0:

TypeError: unsupported operand type(s) for %: 'tuple' and 'int'

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

8

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py", line 5, in <module>

    if i%2==0:

TypeError: not all arguments converted during string formatting

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

8

4

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice1string formatti

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py", line 19, in <module>

    choice = int(input("Enter Your Choice"))

ValueError: invalid literal for int() with base 10: '1string formatti'

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 101

Enter the name of emp: Ram

Enter the salary of emp: 34553

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 101

Enter the name of emp: ram

Enter the salary of emp: 34432

Press 'y' for continue and 'n' for exity

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 102

Enter the name of emp: raj

Enter the salary of emp: 2342

Press 'y' for continue and 'n' for exity

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 103

Enter the name of emp: raman

Enter the salary of emp: 2323

Press 'y' for continue and 'n' for exitn

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 101

Enter the name of emp: Ram

Enter the salary of emp: 23445

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: Raj

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py", line 22, in <module>

    eid=int(input("Enter the id of emp: "))

ValueError: invalid literal for int() with base 10: 'Raj'

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 101

Enter the name of emp: Ram

Enter the salary of emp: 23422

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 102

Enter the name of emp: Raj

Enter the salary of emp: 34222

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 103

Enter the name of emp: Sam

Enter the salary of emp: 34532

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 2

(103, 'Sam', 34532.0)

(102, 'Raj', 34222.0)

(101, 'Ram', 23422.0)

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 3

Deleted Element:  (103, 'Sam', 34532.0)

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 2

(102, 'Raj', 34222.0)

(101, 'Ram', 23422.0)

Press 'y' for continue and 'n' for exit: n

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 101

Enter the name of emp: ram

Enter the salary of emp: 234

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 1

Enter the id of emp: 102

Enter the name of emp: raj

Enter the salary of emp: 34212

Press 'y' for continue and 'n' for exit: y

Press 1. for add the details of employee: 

Press 2. for display the details of employee: 

Press 3. for delete the details of employee: 

Enter Your Choice: 2

[(102, 'raj', 34212.0)]

Press 'y' for continue and 'n' for exit: 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

This is a function

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/13feb.py

This is a function

13

>>> 



'''

D={1:'Ram',8:'Raj',3:'Sam','id':122}

print(D)

for i in D.items():

    #print(i,"=",D[i])

    print(i)


D[5]="John"

print(D)

D[1]="Amit"

print(D)



Emp=[]

c='y'

while c=='y' or c=='Y':

    print("Press 1. for add the details of employee: ")

    print("Press 2. for display the details of employee: ")

    print("Press 3. for delete the details of employee: ")

    choice = int(input("Enter Your Choice: "))

    if choice ==1:

        eid=int(input("Enter the id of emp: "))

        ename=input("Enter the name of emp: ")

        salary=float(input("Enter the salary of emp: "))

        e=(eid,ename,salary)

        Emp.append(e)

    elif choice==2:

        L=len(Emp)

        while L>0:

            print(Emp[L-1])

            L=L-1

        

    elif choice==3:

        if(Emp==[]):

            print("Empty Stack")

        else:

            print("Deleted Element: ",Emp.pop())

    else:

        print("Wrong Choice")

    c=input("Press 'y' for continue and 'n' for exit: ")

'''


def show():

    print("This is a function")


show()



def sum(a,b):

    print(a+b)


sum(5,8)


No comments:

Post a Comment