Professional Documents
Culture Documents
Structures and Base Libraries in Python
Structures and Base Libraries in Python
String Functions
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
HELLO
https://docs.python.org/2/library/string.html
Concatenation
>>> '1' + '2'
Python for Data Science
'12'
'Hi there.'
Replication
>>> '12'*2
Python for Data Science
'1212'
'11222'
Strip
strip(s[, chars])
Return a copy of the string with
>>> s= ' Extras \n'
Python for Data Science
>>> s = '***10***'
>>> s.strip('*')
'10'
https://docs.python.org/2/library/string.html
Split
split(s[, sep[, maxsplit]])
Return a list of the words of
Python for Data Science
>>> s = 'Jane,Doe,Cars,5'
>>> s.split(',')
https://docs.python.org/2/library/string.html
Slicing
H E L L O
>>> word = 'Hello' 0 1 2 3 4
Python for Data Science
>>> word[1:3]
'el'
>>> word[4:7]
'o'
Slicing
H E L L O
>>> word = 'Hello' 0 1 2 3 4
Python for Data Science
>>> word[1:3]
-5 -4 -3 -2 -1
'el'
'ell'
word[1:4]
Substring Testing
find(sub[, start [, end ]])
Returns the lowest index in the
>>> word = 'Hello'
Python for Data Science
True
>>> word.find('el')
https://docs.python.org/2/library/string.html
Convert to Number
>>> word = '1234'
Python for Data Science
>>> int(word)
1234
>>> float(word)
1234.0
>>> word = 'Hi'
>>> int(word)
< Error >
https://docs.python.org/2/library/functions.html#int
String Formatting
>>> statement = 'We love {} {}.'
Python for Data Science
>>> statement.format('data','analysis')
'We love data analysis.'
>>> statement = 'We love {0} {1}.'
>>> statement.format('data','analysis')
'We love data analysis.'
>>> statement = 'We love {1} {0}.'
>>> statement.format('analysis','data')
'We love data analysis.'
https://docs.python.org/2/library/functions.html#int
Python for Data Science
Lists in python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
>>> list 0 1 2
[11, 22, 33]
>>> list[1]
22
>>> list[3]
>>> list[1]=95
0 1 2
>>> print(list[1])
95
Appending to a List
>>> list = [11,22,33] 11 22 33 44
Python for Data Science
>>> list.append(44) 0 1 2 3
>>> list
>>> list.pop(2)
0 1 2 3
33
Deleting from a List
>>> list = [11,22,33,44] 11 22 44
Python for Data Science
>>> list.pop(2)
0 1 2 3
33
>>> list
>>> list.remove(33)
0 1 2 3
Removing from a List
>>> list = [11,22,33,44] 11 22 44
Python for Data Science
>>> list.remove(33)
0 1 2 3
>>> list
>>> list.extend(list2)
>>> list
[1, 2, 3, 4, 5, 6]
>>> list = [1, 2, 3]
>>> list2 = [4, 5, 6] Usually not
>>> list.append(list2) what you
>>> list
want
[1, 2, 3, [4, 5, 6]]
Zipping Lists
>>> list = [1, 2, 3]
Python for Data Science
https://docs.python.org/3/tutorial/datastructures.html#more-on-lists
Python for Data Science
>>> x[1] = 42
>>> print(y)
python
>>> x = [10,20,30]
Python for Data Science
y
python
>>> x = [10,20,30]
>>> y = x
Python for Data Science
>>> x[1] = 42
y
python
>>> x = [10,20,30]
>>> y = x
Python for Data Science
>>> x[1] = 42
y
python
>>> x = [10,20,30]
>>> y = x
Python for Data Science
>>> x[1] = 42
>>> print(y)
[10, 42, 30]
y
python
>>> x = [10,20,30]
>>> y = list(x)
Python for Data Science
>>> x[1] = 42
>>> print(y)
[10, 20, 30]
Tuples in python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
>>> tuple1[1]
'Civic'
>>> len(tuple1)
4
Iterating over a tuple
>>> tuple1 = ('Honda','Civic',4,2017)
Python for Data Science
Honda
Civic
4
2017
Tuples are IMMUTABLE
>>> tuple1 = ('Honda','Civic',4,2017)
Python for Data Science
>>> tuple1[3]=2018
Dictionaries in python
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
Key Value
'A12367' 'David Wu'
'A27691' 'Maria Sanchez'
'A16947' 'Tim Williams'
'A21934' 'Sarah Jones'
Dictionary Examples
Key Value
Python for Data Science
Ghostbusters 2016
Ghostbusters 1984
http://www.imdb.com/
Movie Ratings Dictionary
Key Value
Python for Data Science
('Ghostbusters',2016) 5.4
('Ghostbusters',1984) 7.8
('Cars',2006) 7.1
… …
http://www.imdb.com/
Dictionary Basics
Python for Data Science
>>> dict[('Ghostbusters',2016)]
5.4
>>> len(dict)
2
Adding to a Dictionary
Python for Data Science
>>> dict
>>> dict
{('Cars', 2006): 7.1,
('Ghostbusters', 1984): 7.8}
>>> del dict[('Cars', 2006)]
Iterating over a dictionary
>>> dict = {('Ghostbusters', 2016): 5.4,
Python for Data Science
('Ghostbusters', 2016)
('Cars', 2006)
('Ghostbusters', 1984)
Iterating over a dictionary
>>> dict = {('Ghostbusters', 2016): 5.4,
Python for Data Science
5.4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size
during iteration
Selective removal
>>> dict = {('Ghostbusters', 2016): 5.4,
('Ghostbusters', 1984): 7.8, ('Cars',
Python for Data Science
2006):7.1}
>>> to_remove = [];
>>> for i in dict:
… if(i[1] < 2000):
… to_remove.append(i)
>>> for i in to_remove:
... dict.pop(i)
...
>>> dict
{('Ghostbusters', 2016): 5.4,
('Cars', 2006): 2.1}
Python for Data Science
1,4,9,16,25,36,49,64,81,100
List comprehension
i = 1,2,3,4,5,6,7,8,9,10
Python for Data Science
[0, 1, 2, 3, 4, 5]
List comprehension
Python for Data Science
[0, 1, 2, 3, 4, 5]
List comprehension
Python for Data Science
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
List comprehension
Python for Data Science
http://effbot.org/librarybook/random.htm
Dictionary comprehension
Python for Data Science
Sets
Dr. Ilkay Altintas and Dr. Leo Porter
Twitter: #UCSDpython4DS
By the end of this video, you should be able to:
Python for Data Science
• Unordered
• Unique (no duplicates)
• Support set operations (e.g., union, intersection)
Set Basics: Create and Add
Python for Data Science
{'blue'}
Sets – Quick operations
Python for Data Science
https://docs.python.org/3/library/stdtypes.html#set