Jovian
⭐️
Sign In

Python List is a ordered collection and allows duplicate members. Lists are mutable, and hence, they can be altered even after their creation.Items in the list need not be of the same data type.

In [20]:
# Creating a List of 5 numbers
List = [1,2,3,4,5] 
print(List) 
# Accesing elements in a list 
print("Third Element :",List[2])
# Adding elements to the list
List.append(6)
print("\n List after append() :" ,List)
# print the last element of list 
print("\n Last Element of the list :",List[-1]) 
# Removing elements from List using remove()
List.remove(6) 
# Slicing operation in list
List_segment = List[2:5] 
print("\nSlicing elements in a range 2-4: ") 
print(List_segment)
[1, 2, 3, 4, 5] Third Element : 3 List after append() : [1, 2, 3, 4, 5, 6] Last Element of the list : 6 Slicing elements in a range 2-4: [3, 4, 5]

A dictionary is an unordered collection, changeable and indexed. Dictionaries have keys and values and are written with curly braces {}.Keys in the dictionary are unique .

In [6]:
# Creating a Dictionary  
Dict = {1: 'The', 2: 'workshop', 3: 'for'} 
print("\nDictionary with the use of Integer Keys: ") 
print(Dict) 
# Adding elements to the dictionary 
Dict[4] = "WiDS"
print("\nDictionary after adding 4 th element: ") 
print(Dict) 
# accessing a element using key 
print("Accessing a element using key:") 
print(Dict[1]) 

Dictionary with the use of Integer Keys: {1: 'The', 2: 'workshop', 3: 'for'} Dictionary after adding 3 elements: {1: 'The', 2: 'workshop', 3: 'for', 4: 'WiDS'} Accessing a element using key: The

A tuple is a ordered collection and are immutable unlike lists. Tuples have comma-seperated values and are written within square brackets .

In [21]:
#Creatting a Tuple of 5 numbers
Tuple1 = ('1','2','3','4','5') 
print("Tuple :",Tuple1)
#Accessing Tuple with Indexing 
print("\nFirst element of Tuple: ") 
print(Tuple1[1]) 
# Concatenaton of tuples 
Tuple2 = (0,1,2,3) 
Tuple3 = (4,5,6,7)   
Tuple4 = Tuple1 + Tuple2 
print("Tuple after concatenation :",Tuple4)
# Deleting a Tuple  
Tuple5 = (0, 1, 2, 3, 4) 
del Tuple5   
 
Tuple : ('1', '2', '3', '4', '5') First element of Tuple: 2 Tuple after concatenation : ('1', '2', '3', '4', '5', 0, 1, 2, 3)

Python Strings can be creted by using either single or double quotation marks. Character data type is a string wth a length of one.Strings can be accessed using square brackets.

In [23]:
# Creating a String  
String1 = 'This is WiDS workshop'
print("Printing the string: ") 
print(String1)
# Printing First character 
print("\nFirst character of String is: ") 
print(String1[0]) 
# Printing 3rd to 12th character 
print("\nSlicing characters from 3-12: ") 
print(String1[3:12])
# Escaping Sequencing of a String 
String1 = 'This is \"WiDS workshop \"'
print("\nEscape sequencing of a string: ") 
print(String1)  
# Formatting of Strings 
String1 = "{} {} {}".format('This', 'is', 'WiDS Workshop') 
print("\nFormatting of String: ") 
print(String1) 

Printing the string: This is WiDS workshop First character of String is: T Slicing characters from 3-12: s is WiDS Escape sequencing of a string: This is "WiDS workshop " Formatting of String: This is WiDS Workshop
In [ ]: