Class 11
Class 11
Class 11
2. What is Python's Pass Statement? The pass statement is also known as the null statement.
Operato Description
r
+ It is known as concatenation operator used to join the strings given either side of the operator.
* It is known as repetition operator. It concatenates the multiple copies of the same string.
[:] It is known as range slice operator. It is used to access the characters from the specified range.
In It is known as membership operator. It returns if a particular sub-string is present in the specified string.
not in It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is
not present in the specified string.
r/R It is used to specify the raw string. Raw strings are used in the cases where we need to print the actual
meaning of escape characters such as "C://python". To define any string as a raw string, the character r or R
is followed by the string.
% It is used to perform string formatting. It makes use of the format specifiers used in C programming like %d
or %f to map their values in python. We will discuss how formatting is done in python.
String Operators
Method Description
capitalize() It capitalizes the first character of the String. This function is deprecated in python3
center(width ,fillchar) It returns a space padded string with the original string centred with equal number of left an
right spaces.
count(string,begin,end) It counts the number of occurrences of a substring in a String between begin and end index.
decode(encoding = 'UTF8', Decodes the string using codec registered for encoding.
errors = 'strict')
encode() Encode S using the codec registered for encoding. Default encoding is 'utf-8'.
endswith(suffix ,begin=0,end=l It returns a Boolean value if the string terminates with given suffix between begin and end.
en(string))
expandtabs(tabsize = 8) It defines tabs in string to multiple spaces. The default space value is 8.
find(substring ,beginIndex, It returns the index value of the string where substring is found between begin index and en
endIndex) index.
format(value) It returns a formatted version of S, using the passed value.
index(subsring, beginIndex, It throws an exception if string is not found. It works same as find() method.
endIndex)
isalnum() It returns true if the characters in the string are alphanumeric i.e., alphabets or numbers an
there is at least 1 character. Otherwise, it returns false.
isalpha() It returns true if all the characters are alphabets and there is at least one character, otherwi
False.
isdecimal() It returns true if all the characters of the string are decimals.
islower() It returns true if the characters of a string are in lower case, otherwise false.
isprintable() It returns true if all the characters of s are printable or s is empty, false otherwise.
isupper() It returns false if characters of a string are in Upper case, otherwise False.
isspace() It returns true if the characters of a string are white-space, otherwise false.
istitle() It returns true if the string is titled properly and false otherwise. A title string is the one
which the first character is upper-case whereas the other characters are lower-case.
isupper() It returns true if all the characters of the string(if exists) is true otherwise it returns false.
ljust(width[,fillchar]) It returns the space padded strings with the original string left justified to the given width.
partition() It searches for the separator sep in S, and returns the part before it, the separator itself, and th
part after it. If the separator is not found, return S and two empty strings.
replace(old,new[,count]) It replaces the old sequence of characters with the new sequence. The max characters a
replaced if max is given.
rjust(width,[,fillchar]) Returns a space padded string having original string right justified to the number of characte
specified.
rstrip() It removes all trailing whitespace of a string and can also be used to remove particul
character from trailing.
rsplit(sep=None, maxsplit = -1) It is same as split() but it processes the string from the backward direction. It returns the list o
words in the string. If Separator is not specified then the string splits according to the whit
space.
split(str,num=string.count(str)) Splits the string according to the delimiter str. The string splits according to the space if th
delimiter is not provided. It returns the list of substring concatenated with the delimiter.
splitlines(num=string.count('\ It returns the list of strings at each line with newline removed.
n'))
startswith(str,beg=0,end=len(str It returns a Boolean value if the string starts with given str between begin and end.
))
translate(table,deletechars = It translates the string according to the translation table passed in the function .
'')
Python List
1. # a simple list
2. list1 = [1, 2, "Python", "Program", 15.9]
3. list2 = ["Amy", "Ryan", "Henry", "Emma"]
4.
5. # printing the list
6. print(list1)
7. print(list2)
8. # printing the type of list
9. print(type(list1))
10. print(type(list2))
11. # example
12. a = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6 ]
13. b = [ 1, 2, 5, "Ram", 3.50, "Rahul", 6 ]
14. a == b
15. list = [1,2,3,4,5,6,7]
16. print(list[0])
17. print(list[1])
18. print(list[2])
19. print(list[3])
20. # Slicing the elements
21. print(list[0:6])
22. # By default, the index value is 0 so its starts from the 0th element and go for index -1.
23. print(list[:])
24. print(list[2:5])
25. print(list[1:6:2])
26. # negative indexing example
27. list = [1,2,3,4,5]
28. print(list[-1])
29. print(list[-3:])
30. print(list[:-1])
31. print(list[-3:-1])
32. # updating list values
33. list = [1, 2, 3, 4, 5, 6]
34. print(list)
35. # It will assign value to the value to the second index
36. list[2] = 10
37. print(list)
38. # Adding multiple-element
39. list[1:3] = [89, 78]
40. print(list)
41. # It will add value at the end of the list
42. list[-1] = 25
43. print(list)
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
44.
a = ["apple", "banana", "cherry"]
b = ["Ford", "BMW", "Volvo"]
a.append(b)
Method Description
items() Returns a list containing a tuple for each key value pair
setdefault( Returns the value of the specified key. If the key does not exist: insert the ke
) specified value
index() Searches the tuple for a specified value and returns the position of where it was fo
Method Description
difference_update() Removes the items in this set that are also included in an
set
intersection_update() Removes the items in this set that are not present in othe
symmetric_difference_update( inserts the symmetric differences from this set and anothe
)
update() Update the set with another set, or any other iterable