Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # indexing
- y = 'frog'
- print(y[1])
- sx = ['pig', 'cow', 'horse']
- print(sx[1])
- # slicing [start: end+1: step]
- x = 'computer'
- print(x[1:4]) # items 1 to 3
- print(x[1:6:2]) # items 1,3,5
- print(x[3:]) # items 3 to end
- print(x[:5]) # items 0 to 4
- print(x[-1]) # last item
- print(x[-3:]) # last 3 items
- print(x[:-2]) # all except last 2 items
- # adding / concatenating
- # combine 2 sequences of the same type using
- io = 'horse' + 'shoe' # string
- print(io)
- sz = ['cow', 'horse'] + ['buffalo']
- print(sz)
- # multiplying
- ax = 'bug'*3
- print(ax)
- # checking membership: test whether an item is in or not in a sequence
- xx ='bug'
- print('u' in xx)
- sxx = ['pig', 'cow', 'horse']
- print('cow' not in sxx)
- # iterating through the items in a sequence
- xp = [7,8,3] # item
- for item in xp:
- print(item*2)
- # index & item
- for index, item in enumerate(xp):
- print(index, item)
- # find the minim item (alpha or numeric types, but cannot mix types
- ty = 'bug'
- print(min(ty))
- sxh = ['pig', 'cow', 'horse']
- print(min(sxh)) # cow
- # find the maximum item in sequence (alpha or numeric types, but cannot mix types
- tyy = 'bug'
- print(max(ty))
- sxhh = ['pig', 'cow', 'horse']
- print(max(sxh))
- # sum (find the sum of items in a sequence+ entire seq must be a num type
- tyu = [5,7, 'bug']
- # print(sum(tyu)) # error
- tyuu = [5,6,7.0]
- print(sum(tyuu))
- print(sum(tyuu[-2:]))
- # sorting (returns a new list of items in sorted order)/ does not change the original list
- gh = 'bug'
- print(sorted(gh))
- ghy = ['king kong', 'ghidorah', 'godzilla']
- print(sorted(ghy))
- # count (item)
- yu = 'hippo'
- print(yu.count('p'))
- yuut = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
- print(yuut.count('godzilla'))
- # index returns the index of the first occurrence of an item
- fj = 'hiphop'
- print(fj.index('p'))
- fjj = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
- print(fjj.index('godzilla'))
- # unpacking: unpack n items of a seq into n variables
- unp = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
- a,b,c, d= unp
- print(a,b,c,d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement