The document discusses Python membership operators like 'in' and 'not in' and provides an example of using them to check if a string is contained in another string. It also discusses various Python data types like lists, tuples, and dictionaries - including how to define, index, slice, and modify list elements as well as examples of tuples and dictionaries with key-value pairs.
The document discusses Python membership operators like 'in' and 'not in' and provides an example of using them to check if a string is contained in another string. It also discusses various Python data types like lists, tuples, and dictionaries - including how to define, index, slice, and modify list elements as well as examples of tuples and dictionaries with key-value pairs.
The document discusses Python membership operators like 'in' and 'not in' and provides an example of using them to check if a string is contained in another string. It also discusses various Python data types like lists, tuples, and dictionaries - including how to define, index, slice, and modify list elements as well as examples of tuples and dictionaries with key-value pairs.
a = "kajal" b = "sunil" c = "kajal"in a d = "sunil" not in b print (c) print (d) List,Tuple Dictionary Lists
❑ Like a String, List also is, sequence data type.
❑ In a string we have only characters but a list consists of data of multiple data types ❑ It is an ordered set of values enclosed in square brackets []. ❑ We can use index in square brackets [] ❑ Values in the list are called elements or items. ❑ A list can be modified, i.e. it is mutable. List Examples i) L1 = [1,2,3,4] list of 4 integer elements.
ii) L2 = [“Delhi”, “Chennai”, “Mumbai”]
list of 3 string elements.
iii) L3 = [ ] empty list i.e. list with no element
iv) L4 = [“abc”, 10, 20]
list with different types of elements Modifying the values given in List Item List Slices Examples >>> L=[10,20,30,40,50] >>> print(L[1:4]) [20, 30, 40] #print elements 1st index to 3rd index >>> print(L[3:]) [40, 50]
#print elements from 3rd index onwards
List Slices Examples >>> print(L[:3]) [10, 20, 30]
#print elements 0th index to 2nd index
Tuples ▪ We saw earlier that a list is an ordered mutable collection. There’s also an ordered immutable collection.
▪ In Python these are called tuples and look very
similar to lists, but typically written with () instead of []: a_list = [1, 'two', 3.0] a_tuple = (1, 'two', 3.0) Similar to how we used list before, you can also create a tuple. The difference being that tuples are immutable Like lists, all of the common sequence operations are available. Example of Tuple: tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
print (tuple ) # Prints complete list
print (tuple[0]) # Prints first element of the
list
print (tuple[1:3]) # Prints elements starting
from 2nd till 3rd
print (tuple[2:]) # Prints elements starting
from 3rd element Dictionary
▪ Python's dictionaries are kind of hash
table type. They work like associative arrays and consist of key-value pairs. ▪ Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]) Example of Dictionary