Day 3¶
1. List¶
Till now we are using list to store data and plot them. Now today we will learn about it extensively. List is a data structure, means a way to store and access data. So today we will learn how to store and access data in list data structure.
- a list is a collection of data in a finite sequence.
- it can hold values of multiple datatype, i.e., one entry can be integer, other can float or string.
1.1 Creating lists¶
A list is created using a square bracket as follows,
a = [] # an empty list
b = [1,2,3,4,5] # list of numbers
c = ["sandeep", "suman", 2020, 7] # mix datatypes
d = [["sandeep", "suman"], 2020, []] # list inside list
print(c)
['sandeep', 'suman', 2020, 7]
1.2 Accessing data inside list¶
Each entry has an index or position in a list, which can be used to access them. As counting starts in python from 0
, So the first index is 0
and second index is 1
and so on.
c = ['sandeep', 'suman', 2020, 7]
print(c[0]) # to get the 0th element (count with 0)
print(c[2]) # to get the 2nd element (count with 0)
print(c[-1]) # count from last
sandeep
2020
7
1.3 Basic list operations¶
-
Length: The length of a list can be computed as follows
len(c)
4
-
Addition: Two or more lists can be added with
+
operation as followsa = [1, 2] b = ["a", "b"] c = a + b print(c)
[1, 2, 'a', 'b']
-
Multiplication: Repetition can be achieved with
*
operation as followsa = ['Hi'] * 5 print(a)
['Hi', 'Hi', 'Hi', 'Hi', 'Hi']
-
Slicing: A part of of list starting with
i
index and end beforej
can be obtained byList[i:j]
, for examplec = ["sandeep", "suman", 2020, 7] print(c[1:3])
['suman', 2020]
1.4 List as Stack¶
Stack is a data structure based on LIFO(Last in first out).
-
Append: The
append
method on a list will add element in the end of the list.L = [] L.append(1) # 1 will be added to L L.append(2) # 2 will be added to L print(L)
[1, 2]
-
Pop: The
pop
method on list will remove the element from the last.L = [1, 2] L.pop() print(L)
[1]
The method
pop
also takes addition argument to remove element from any given index.L = [1, 2] L.pop(0) # removes the element with index 0 print(L)
[2]
1.5 Other method on list¶
-
Index: This method is used to get the index of an element.
L = ["apple", "mango", "orange"] i = L.index("mango") print(i)
1
-
Remove: This method is used to remove an element in the list.
L = ["apple", "mango", "orange"] L.remove("mango") print(L)
['apple', 'orange']
-
Reverse: This method will reverse the elements of the given list.
L = ["apple", "mango", "orange"] L.reverse() print(L)
['orange', 'mango', 'apple']
-
Sort: This method is used to sort the elements of a list.
L = [4, 6, 2, 7, 1, 3] L.sort() print(L)
[1, 2, 3, 4, 6, 7]
Tip
The maximum element of a list can be obtained using sort taking in the last element. i.e.,
L = [4, 6, 2, 7, 1, 3]
L.sort()
max = L[-1]
print(max)
7
1.6 Membership and Iterating of a List¶
-
Like the membership in the set. We can tell if something is inside a list or not as follows
L = ["apple", "mango", "orange"] print("apple" in L) print("guava" in L)
True False
-
Often in programming we want look for each element of a list one by one from start to finish. The process is generally known as
iteration
in programming.A simple program to iterate a list is as follows
fruits = ["apple", "mango", "orange"] for i in fruits: # semicolon is used to make code block print(i) # 4 space is used here !important
apple mango orange
2. Strings¶
String is one the basic datatypes of the python language. There are many ways to define a string but we use the most common method by writing a sentence or paragraph inside single('
) or double("
) quotes.
mystring = "python is awesome"
mystring = 'python is awesome'
print(mystring)
python is awesome
Warning
Even numbers in single or double quotes behaves as an string.
temp = "38.7"
print(type(temp))
<class 'str'>
2.1 Accessing data inside list¶
String behaves very much similar to the list. You can think that it is a list where each character has as index. We can obtain a character from its position or index. As counting starts in pyton from 0
, So the first index is 0
and second index is 1
and so on.
mystring = "python is awesome"
fifth = mystring[4] # fifth character from start
last_fifth = mystring[-4] # fifth character from last
print(fifth, last_fifth)
o s
2.2 Basic list operations¶
-
Length: The length of a string can be computed as follows
print(len(mystring))
17
-
Addition: Two or more string can be added with
+
operation as followsa = "Sandeep " b = "Suman" c = a + b print(c)
Sandeep Suman
-
Multiplication: Repetition can be achieved with
*
operation as followsa = 'Hi!' * 5 print(a)
Hi!Hi!Hi!Hi!Hi!
-
Slicing: A part of of string starting with
i
index and end beforej
can be obtained byList[i:j]
, for examplec = "sandeep" print(c[1:4]) # start from 1st and end before 4th (counting starts with 0) print(c[4:]) # everything after 4th (counting starts with 0) print(c[:4]) # to get the substring from start and end before 4th (counting starts with 0)
and eep sand
-
Extended Slicing: The slicing in string is more flexible as it accepts more than two arguments. For example
X[1:10:2]
will get every other item in the substringX[1:10]
univ = "TilkaManjhiBhagalpurUniversity" s1 = univ[1:20:2] # get even position elements in substring univ[1:20] s2 = univ[::2] # get even position elements s3 = univ[::-1] # reverse the string print(s)
ikMnhBaapr TlaajihgluUiest ytisrevinUruplagahBihjnaMakliT
2.3 String Methods¶
Some of the string method is the following
-
Capitalize: This method is used make first letter capital.
msg = "Welcome to FaculTy DevelopMent prograMME." print(msg.capitalize())
Welcome to faculty development programme.
-
Lower: It will make every letter small.
msg = "Welcome to FaculTy DevelopMent prograMME." print(msg.lower())
welcome to faculty development programme.
-
Replace: It will one substring with another.
msg = "Welcome to FaculTy DevelopMent prograMME." print(msg.replace("M", "X")) # replace all M with X
Welcome to FaculTy DevelopXent prograXXE.
To replace only one instance, we can pass one more argument.
msg = "Welcome to FaculTy DevelopMent prograMME." print(msg.replace("M", "X", 1)) # replace the first M with X
Welcome to FaculTy DevelopXent prograMME.
-
Find: It will find one string in another string.
msg = "Welcome to FaculTy DevelpMent prograMME." print(msg.find("to")) # finds the position of "to"
8
You can think of this as the following
msg = "Welcome to FaculTy DevelpMent prograMME." position = msg.find("to") # position of to original = msg[:8] + "to" + msg[8+2:] # the original msg print(original)
Welcome to FaculTy DevelpMent prograMME.
-
Split: The
split
method will convert a string to list of substringmsg = "Welcome to FaculTy DevelpMent prograMME." L = msg.split() # default method will split at each whitespace print(L)
['Welcome', 'to', 'FaculTy', 'DevelpMent', 'prograMME.']
The
split
method is much more flexible. We can pass an argument to tell about the marker for split.msg = "Amar,Akbar,Anthony" L = msg.split(",") # split each part with "," print(L)
['Amar', 'Akbar', 'Anthony']
-
Join: The join method works opposite of split. We can make a string using the split method.
L = ['Amar', 'Akbar', 'Anthony'] msg = " ".join(L) # the string in between will go before "." print(msg)
Amar Akbar Anthony
If we want to join with
,
in all the elements of the list.L = ['Amar', 'Akbar', 'Anthony'] msg = ",".join(L) print(msg)
Amar,Akbar,Anthony
Ex: Try to join the list L with comma and space. Ex: Try to join in smaller latter.
Tip
There are huge number of list methods in python, and python programming is considered to be very powerful in list manipulation. You can google search to do any kind of list manipulation.
2.4 Membership and Iterating a String¶
-
Membership tells if a string is a part of larger string or not.
print("T" in "TMBU") print("math" in "mathematics") print("ss" in "sandeep")
True True False
-
The iteration in string work similar to list as follows
univ = "TMBU" for i in univ: print(i)
T M B U
Ex: Write a program to check if the word 'orange' is present in the "This is orange juice".
Ex: Convert the date written in the format "10/06/2020" to "10-06-2020".
Ex: Convert "Tilka MaJHi BHaGalPUR UNIVersiTy" to "Tilka Manjhi Bhagalpur University".