In [1]:
from IPython.core.display import HTML
def css_styling():
styles = open("styles/custom.css", "r").read()
return HTML(styles)
css_styling()
Out[1]:
In [1]:
# create a string
str = ' the quick brown fox jumps over the lazy dog. '
str
Out[1]:
In [2]:
# this is an empty string
empty_str = ''
In [3]:
# strip whitespaces from the beginning and ending of the string
str2=str.strip()
print str2
print str
In [5]:
# this capitalizes the 1st letter of the string
str2.capitalize()
Out[5]:
In [6]:
# count the number of occurrences for the string o
str.count('o')
Out[6]:
In [7]:
# check if a string ends with a certain character
str2.endswith('.')
Out[7]:
In [53]:
# check if a substring exists in the string
'jum' in str
Out[53]:
In [9]:
# find the index of the first occurrence
str.find('fox')
Out[9]:
In [10]:
# let's see what character is at index 19
str[19]
Out[10]:
Note: strings are immutable while lists are not. In other words immutability does not allow for in-place modification of the object.
In [11]:
S = 'shrubbery'
S[1]='c'
In [55]:
S = 'shrubbery'
L = list(S)
L
Out[55]:
In [56]:
S[0]
Out[56]:
In [61]:
L[1] = 'c'
print L
S1 = ''.join(L)
print 'this is S1:', S1
print S
In [57]:
for x in S:
print x
In [60]:
# another way of changing the string
S = S[0] + 'c' + S[2:] # string concatenation
S
Out[60]:
In [15]:
#
line = 'aaa,bbb,cccc c,dd'
line1 = line.split(',')
print line
print line1
In [16]:
dir(S)
Out[16]:
In [17]:
help(S.split)
In [18]:
ord(S[0])
Out[18]:
In [10]:
# empty dictionary
D = {}
In [11]:
D['food'] = 'Spam'
D['quantity'] = 4
D['color']='pink'
D
Out[11]:
In [12]:
# another way to declare a dictionary
D = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}
print D
D[0] = 'ZERO'
print D
In [13]:
D['somthing'] = 'another'
In [65]:
D[0]
Out[65]:
In [14]:
print D
In [23]:
# print the value associated with the key 'food'
D['food']
Out[23]:
In [24]:
# there is no restrictions on the key type
D = {1:'1', 2:'2'}
print D
In [25]:
bob1 = dict(name='Bob', job='dev', age=40)
print bob1
In [26]:
bob2 = dict(zip(['name', 'job', 'age'], ['Bob', 'dev', 40]))
print bob2
In [6]:
# nesting - no restrictions on value
rec = {'name': {'first': ['bob', 'mob'], 'last': 'Smith'},
'jobs': ['dev', 'mgr'],
'age': 40.5}
print rec
In [7]:
# access the first element
rec['name']
Out[7]:
In [8]:
rec['name']['first']
Out[8]:
In [9]:
rec['name']['first'][0]
Out[9]:
In [ ]:
# empty tuple
T = ()
In [37]:
# definition
T= (1, 2, 3, 4)
print T
In [39]:
# get the first element of T just like you would with lists
T[0]
Out[39]:
In [43]:
# add two tuples to make a larger tuple
T+(5,6)
Out[43]:
In [47]:
# you can't just one element to the typle unless that element is a tuple
T + 7
In [48]:
# here's how you add the 7 to the tuple
T + (7,)
Out[48]:
In [49]:
# here's how you add a string to the tuple
T + ('abc',)
Out[49]:
In [32]:
!dir
In [33]:
f = open('test','r')
In [34]:
lines = f.readlines()
lines
Out[34]:
In [50]:
f.close()
In [35]:
print lines[0]
In [36]:
for line in lines:
print line
In [ ]: