You are on page 1of 3

# input the list as string print(f[2:11:3]) Tuple2 = ('python', 'geek')

string = input("Enter elements of a list print(f[::-1]) Tuple3 = (Tuple1, Tuple2)


(Space-Separated): ") [1, 1, 2, 3, 5, 8]
[2, 3, 5, 8] print("\nTuple with nested tuples: ")
lst = string.split() # split the strings [2, 8]
print(Tuple3)
and store it to a list [8, 5, 3, 2, 1, 1]
# Creating an empty Tuple # Creating a Tuple
print('The list is:', lst) # printing the
list Tuple1 = () # with repetition
# input size of the list print("Initial empty Tuple: ") Tuple1 = ('Geeks',) * 3
n = int(input("Enter the size of list : ")) print(Tuple1) print("\nTuple with repetition: ")
# store integrs in a list using map, split # Creating a Tuple print(Tuple1)
and strip functions
# with the use of string
lst = list(map(int, input("Enter the
integer elements of list(Space- Tuple1 = ('Geeks', 'For') # Creating a Tuple
Separated): ").strip().split()))[:n] print("\nTuple with the use of String: # with the use of loop
print('The list is:', lst) # printing the ")
Tuple1 = ('Geeks')
list print(Tuple1)
n=5
# Creating a List # Creating a Tuple with
print("\nTuple with a loop")
List = [] # the use of list
for i in range(int(n)):
print("Initial blank List: ") list1 = [1, 2, 4, 5, 6]
Tuple1 = (Tuple1,)
print(List) print("\nTuple using List: ")
print(Tuple1)
# Addition of Elements print(tuple(list1))
del Tuple1
# in the List # Creating a Tuple
# Creating a Dictionary
List.append(1) # with the use of built-in function # with Integer Keys
Dict = {1: 'Geeks', 2:
List.append(2) Tuple1 = tuple('Geeks') 'For', 3: 'Geeks'}
print("\nDictionary with
List.append(4) print("\nTuple with the use of the use of Integer Keys:
print("\nList after Addition of Three function: ") ")
print(Dict)
elements: ") print(Tuple1)
print(List) # Creating a Dictionary
# Creating a Tuple # with Mixed keys
mylist.reverse() Dict = {'Name': 'Geeks',
# with Mixed Datatype 1: [1, 2, 3, 4]}
print(mylist) Tuple1 = (5, 'Welcome', 7, 'Geeks') print("\nDictionary with
the use of Mixed Keys: ")
List.pop(2) print("\nTuple with Mixed Datatypes: print(Dict)

print("\nList after popping a specific ")


Dictionary with the use
element: ") print(Tuple1) of Integer Keys:
{1: 'Geeks', 2: 'For',
print(List) 3: 'Geeks'}
f = [1, 1, 2, 3, 5, 8] # Creating a Tuple Dictionary with the use
print(f) of Mixed Keys:
# with nested tuples
{'Name': 'Geeks', 1: [1,
print(f[2:7]) Tuple1 = (0, 1, 2, 3) 2, 3, 4]}
Dictionary methods
clear() - Remove all the
elements from the
dictionary

copy() - Returns a copy


of the dictionary
p = Person('Nikhil')
get() - Returns the value
of specified key p.say_hi()

items() - Returns a list


containing a tuple for Similaritie
Differences
each key value pair s
Functions
Methods that
keys() - Returns a list that can be
cannot be
containing dictionary's used for
used for
keys both lists
tuples:
and tuples:
pop() - Remove the append(),
element with specified len(),
insert(),
key max(),
remove(),
min(),
pop(),
sum(),
popitem() - Removes the clear(),
any(),
last inserted key-value sort(),
all(),
pair reverse()
sorted()
we generally
update() - Updates
use 'tuples'
dictionary with specified Methods for
key-value pairs that can be heterogeneou
used for s
values() - Returns a list both lists (different)
of all the values of and tuples: data types
dictionary and 'lists'
count(), for
Class Definition Syntax: Index() homogeneous
(similar)
class ClassName: data types.
# Statement-1 Iterating
. Tuples can through a
. be stored 'tuple' is
. in lists. faster than
# Statement-N in a 'list'.
'Lists' are
class Person: Lists can mutable
be stored whereas
# init method or in tuples. 'tuples' are
constructor immutable.
Tuples that
def __init__(self, Both contain
name): 'tuples' immutable
and 'lists' elements can
self.name = name can be be used as a
nested. key for a
# Sample Method dictionary.

def say_hi(self):

print('Hello, my
name is', self.name)
class Employee:
def __init__(self, name):
self.name = name

def __repr__(self):
return self.name

john = Employee('John')
print(john) # John

# Dog class
class Dog:
# Method of the class
def bark(self):
print("Ham-Ham")

# Create a new instance


charlie = Dog()

# Call the method


charlie.bark()
# This will output "Ham-Ham"

You might also like