CH-1 part-2 baics of python
CH-1 part-2 baics of python
lower() Converts all the uppercase letters in the string to lowercase str.lower()
islower() Returns True if all letters in the string are in lowercase str.islower()
upper() Converts lowercase letters in th string to uppercase str.upper()
isupper() Returns True if string is in uppercase str.isupper()
lstrip() or lstrip (char) Returns the string after removing the space(s) from the left of the str.lstrip()
string
rstrip() or rstrip (char) This method removes all the trailing whitespaces from the right of the str. rstrip()
string
STRING METHODS
Method Description Syntax
isspace() Returns True if string contains only whitespace characters. str.isspace()
istitle() This method returns true if string is properly titlecased. str.istitle()
join Returns a string in which the string elements have been joined by astring str.join()
(sequence) separator
swapcase() This method converts and returns all uppercase characters to lowercase str.swapcase()
and vice versa of the given string.
partition partition method is used to split using the specified separator amd str.partition()
(separator) return a tuple with three parts; substring before te separator; separator
itself; a substring sfter the separator
ord() This function returns the ASCII code of the character ord()
chr() This function retuns chracter represented by the inputted ASCII number. chr()
LISTS METHODS
Method Description
cmp(list1, list2) compares elements from both the lists
• 1.Bubble Sort
• 2. Insertion Sort
SORTING TECHNIQUES
• 1.Bubble Sort
bubble sort technique traverses the whole list by comparing its adjacent elements,
sorting them and swapping the elements until the whole list is sorted.
Algorithm
step1: start
step2: store the array element and store it in an array a[]
step3: Store length in a variable n
step4: for i=0 to n-1 , repeat step 5 and 6
step5: for j=0 to n-i-1, repeat step 4
step6: if a[j] > a[j+1] then swap elements
step7: Display the sorted list
step8: End
SORTING TECHNIQUES
• Example
list= [42, 29, 74, 11, 65, 58]
First Pass
[ , 74, 11, 65, 58] = [29, 42, 74, 11, 65, 58]
it will compare first two element i.e 42 and 29. it will swap since 29 > 42
[29, 42, 74, 11, 65, 58] It will compare 42 and 74. no swapping as 42 < 74
=[ 29, 42, 74, 11, 65, 58]
[29, 42, 74, 11, 65, 58] compare 74 and 11. It will swap since 11 < 74
[29, 42, 11, 74, 65, 58]compare 74 and 65. it will swap since 65 >74
[29, 42, 11, 65, 74, 58] compare 74 and 58. It will swap since 58 <74 list
will be [29, 42, 11, 65, 58, 74]
SORTING TECHNIQUES
Second Pass
[29, 42, 11, 65, 58, 74] no swapping as 29 < 42
[29 , 42 , 11, 65, 58, 74] 11 < 42 so swap [29, 11 ,42, 65, 58, 74]
[29, 11 ,42, 65, 58, 74] 42 < 65 no swapping [ 29, 11, 42, 65, 58, 74]
[ 29, 11, 42, 65, 58, 74] 58 < 65 so swap [29, 11, 42, 58, 65, 74]
Third Pass
[29, 11, 42, 58, 65, 74] 29 > 11 so swap [11, 29, 42, 58, 65, 74]
[11, 29, 42, 58, 65, 74] 29 < 42 no swapping [11, 29, 42, 58, 65, 74]
[11, 29, 42, 58, 65, 74] 42 < 58 no swapping [11, 29, 42, 58, 65, 74]
[11, 29, 42, 58, 65, 74]
SORTING TECHNIQUES
Fourth Pass
[11, 29, 42, 58, 65, 74] no swapping
[[11, 29, 42, 58, 65, 74] no swapping
Fifth Pass
[11, 29, 42, 58, 65, 74] no swapping
Here total number of items n is 6 so outer loop will executed n-1 times i,e
5 times. There will be 5 passes through the list
SORTING TECHNIQUES
def main():
list1=[42,29,74,11,65,58]
n= len(list1)
print( “Original list: “, list1)
for i in range(n-1):
for j in range(n-i-1):
if list1[j] > list1[j + 1]:
list[j] , list1[j + 1] = list1[j + 1] , list1[j]
print(“list after sorting is: ”, list1)
SORTING TECHNIQUES
2. Insertion Sort
This sorting technique starts from index 1 (not 0) and each index starting from index 1 is like a new element
that you have to place at the proper position in the sorted sub array on the left.
Algorithm
1: start
2: Enter the element in an array A, store the length in N
3: for i=1 to N repeat step 4 to 8 , else step 8
4: j=A.index(i)
5: while(A[j-1] > A[j] and j > 0) repeat step 6 to 7, else step 3
6: A[j-1], A[j] = A[j], A[j-1]
7: j=j-1
8: print the list
9: end
SORTING TECHNIQUES
Example:
suppose list is [70, 49, 31, 6, 65, 81, 68]
First Pass:
[70, , 31, 6, 65, 81, 68] [ , 31, 6, 65, 81, 68] it will swap since 70 > 49
(The second element 49 is copared with the element that appear before it i.e element 70.
Since 70 > 49, so swapping will take place and element is inserted at correct position.
After first pass, first two elements of an array will be sorted.)
Second Pass:
[ , , 6, 65, 81, 68] [ 6, 65, 81, 68] it will swap since 70 > 31
[ 6, 65, 81, 68] [ , 6, 65, 81, 68] it will further swap since 49 >31
(The third element will compare with 70. Since 70 > 31 , it will swap and list will be [49, 31,
70, 6, 65, 81, 68] The element 31 will be swap further since 49 > 31.)
SORTING TECHNIQUES
Example:
suppose list is [70, 49, 31, 6, 65, 81, 68]
Third Pass:
sorted element unsorted elements
[31, 49, 70, 6, 65, 81, 68]
[ , , 65, 81, 68] [ , 65, 81,68] it will swap since 70 > 6
[ , 65, 81,68] [ , 65, 81,68] it will swap since 49 > 6
[ , 65, 81,68] [ , 65, 81,68] it will swap since 31 > 6
( The Fourth element of an array i.e 6 is compared with the element that appear before it,
i.e 70 first, then with 49 and 31 and finally placed at its correct place in the sorted part of
the array.)
SORTING TECHNIQUES
Example:
suppose list is [70, 49, 31, 6, 65, 81, 68]
Fourth Pass:
sorted element unsorted elements
[6, 31, 49, 70, 65, 81, 68]
[ , , 81, 68] [ , 81,68] it will swap since 70 > 65
( The Fifth element of an array i.e 65 is compared with the element that appear before it,
i.e 70 first, since 70 > 65 , so it will swap it with 70 and further no more comarision will
take place because 65 is already placed at its correct position.)
SORTING TECHNIQUES
Example:
suppose list is [70, 49, 31, 6, 65, 81, 68]
Fifth Pass:
sorted element unsorted elements
[6, 31, 49, 65, 70, 81, 68]
[ , , 68] [ ,68] no swapping will take place
since 70 < 81
Sixth Pass:
[ , ] [ ] it will swap since 81 > 68
[ ] [ ] it will swap since 70 > 68
(Now list is sorted. The total no. of items i 7 so outer loop will be executed n-1 times, i.e 6
times.)
SORTING TECHNIQUES
Code:
#Insertion sort
a=[70, 49, 31, 6, 65, 81, 68]
print(“Original list: “, a)
for i in a:
j=a.index(i)
while > 0:
if a[j-1] > a[j] :
a[j-1], a[j] = a[j], a[j-1]
else:
break
j= j-1
print(“List after sorting: “, a)