0% found this document useful (0 votes)
0 views5 pages

1-June

The document provides a Python implementation of a stack using lists, detailing operations such as push, pop, and display. It also explains aliasing and cloning of list objects, demonstrating how to create references and copies of lists. Additionally, it covers mathematical operations on lists, comparison of list objects, and membership operators, along with methods to clear and delete lists.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

1-June

The document provides a Python implementation of a stack using lists, detailing operations such as push, pop, and display. It also explains aliasing and cloning of list objects, demonstrating how to create references and copies of lists. Additionally, it covers mathematical operations on lists, comparison of list objects, and membership operators, along with methods to clear and delete lists.

Uploaded by

Sandeep sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

'''

# Assignment
# WAP to implement "STACK" using list
# PUSH ( Add new ele at the end ) : append()
# POP ( remove last ele ) : pop()

# Stack = [10,20,30,40,50,60,70]
stack = [] # EMpty List
while True : # infinite loop
print(" 1 PUSH ")
print(" 2 POP ")
print(" 3 SHOW ")
print(" 0 Exit ")
print(" Enter Your Choice ")
ch = int(input()) # 1
if ch == 1:
#------ # append()
#------
elif ch == 2: # pop()
#-------
#-------
elif ch == 3:
#------- # reverse list ( slicing )
#-------
elif ch == 0:
break
else:
print("Invalid Choice ")
'''
'''
# Aliasing and Cloning of List objects

Aliase : Reference which is sharing another object (Nick Name)


Cloning : meand ceating different objects

# Aliasing and Cloning


L1 = [10,3,'Hello',78.29]
id(L1)
2306342174208
L2 = L1 # Aliase
id(L2)
2306342174208
L1
[10, 3, 'Hello', 78.29]
L1[1]=3333 # Update 1st element
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 3333, 'Hello', 78.29]
L3 = L2
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 3333, 'Hello', 78.29]
L3
[10, 3333, 'Hello', 78.29]
id(L1)
2306342174208
id(L2)
2306342174208
id(L3)
2306342174208
# cloning ( Different Object )
L1
[10, 3333, 'Hello', 78.29]
# Method 1 ( using slicing )
L2 = L1[::]
id(L1)
2306342174208
id(L2) # different Add
2306303893376
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 3333, 'Hello', 78.29]
L2[1]=4444
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 4444, 'Hello', 78.29]
# Method 2 ( using copy() Method )
L1
[10, 3333, 'Hello', 78.29]
L3 = L1.copy() # Cloning ( diff Object )
L1
[10, 3333, 'Hello', 78.29]
L3
[10, 3333, 'Hello', 78.29]
id(L1)
2306342174208
id(L3)
2306348070208
L3[1]=5555
L1
[10, 3333, 'Hello', 78.29]
L3
[10, 5555, 'Hello', 78.29]
'''
Using Mathematical operators for List Objects:

1: '+' Operator ( concat Two list objects )


2. '*' operator ( repetaion list object )

# '+' operator
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 4444, 'Hello', 78.29]
L3 = L1 + L2
L3
[10, 3333, 'Hello', 78.29, 10, 4444, 'Hello', 78.29]
L3 = [1,2,3] + [11,22,33,44,55]
L3
[1, 2, 3, 11, 22, 33, 44, 55]
L3 = [1,2,3] + "Hello" # Error

# TypeError: can only concatenate list (not "str") to list


L3 = [1,2,3] +["Hello"]
L3
[1, 2, 3, 'Hello']
L3 = [1,2,3] + 120
# TypeError: can only concatenate list (not "int") to list
# '*' ( repetation OPerator )
L1
[10, 3333, 'Hello', 78.29]
L2 = L1 * 3
L2
[10, 3333, 'Hello', 78.29, 10, 3333, 'Hello', 78.29, 10, 3333, 'Hello', 78.29]
L1
[10, 3333, 'Hello', 78.29]
L2
[10, 3333, 'Hello', 78.29, 10, 3333, 'Hello', 78.29, 10, 3333, 'Hello', 78.29]
L3 = L1 * L2 # Error

# TypeError: can't multiply sequence by non-int of type 'list'


L3 = L1 * 12.35 # Error

# TypeError: can't multiply sequence by non-int of type 'float

# Comparing list Object


# in python we can compare two list onject using
# relational operators
# == , != , > , < , <= , <=
# '==' and '!=' compare all values of list object ( Same as str )
# '>','<','>=','<=' compare FIRST element of List object
# comparing list objects
L1=[1,2,3,4]
L2=[5,6,7,8]
L3=[1,2,3,4]
l5=['Abc','Def']
L5=['Abc','Def']
L6=['Ghi','Lmn']
l1 == L2
Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
l1 == L2
NameError: name 'l1' is not defined. Did you mean: 'L1'?
L1 == L2
False
L1 == L3
True
L1 != L2
True
L1 != L3
False
L5 == L6
False
L5 != L6
True
L1
[1, 2, 3, 4]
L2
[5, 6, 7, 8]
L3
[1, 2, 3, 4]
L4
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
L4
NameError: name 'L4' is not defined
L1 > L2 # Comparing 1st ele only
False
L2 > L1
True
L1 <= L2 # Comparing 1st ele only
True
L1 > L3
False
L1 >= L3
True
L3 <= L1
True
L1
[1, 2, 3, 4]
L5
['Abc', 'Def']
L1 == L5
False
L5 > L1

# TypeError: '>' not supported between instances of 'str' and 'int'


123 == "Hello"
False
123 > "Hello"

# TypeError: '>' not supported between instances of 'int' and 'str'


123 != "Hello"
True
123 >= "Hello"

# TypeError: '>=' not supported between instances of 'int' and 'str'


123 == "123"
False
123 == int("123")
True

# Membership operators
# 'in' and 'not in' operator ( Check element in SEQUENCE )
77 in L1
False
4 in L1
True
77 not in L1
True
44 in L1
False

# clear() function :-> to make empty list object


# but list object will be not destroyed
# to permanently remove any object use 'del' keyword

L1 = [1,2,3,4]
L1.clear() # make empty L1 object
L1
[]
id(L1)
2306342174208

del L1
L1
# Error undefined 'L1'

You might also like