Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. # indexing
  2. y = 'frog'
  3. print(y[1])
  4. sx = ['pig', 'cow', 'horse']
  5. print(sx[1])
  6.  
  7.  
  8. # slicing [start: end+1: step]
  9. x = 'computer'
  10. print(x[1:4]) # items 1 to 3
  11. print(x[1:6:2]) # items 1,3,5
  12. print(x[3:]) # items 3 to end
  13. print(x[:5]) # items 0 to 4
  14. print(x[-1]) # last item
  15. print(x[-3:]) # last 3 items
  16. print(x[:-2]) # all except last 2 items
  17.  
  18. # adding / concatenating
  19. # combine 2 sequences of the same type using
  20. io = 'horse' + 'shoe' # string
  21. print(io)
  22. sz = ['cow', 'horse'] + ['buffalo']
  23. print(sz)
  24. # multiplying
  25. ax = 'bug'*3
  26. print(ax)
  27.  
  28. # checking membership: test whether an item is in or not in a sequence
  29. xx ='bug'
  30. print('u' in xx)
  31. sxx = ['pig', 'cow', 'horse']
  32. print('cow' not in sxx)
  33.  
  34. # iterating through the items in a sequence
  35. xp = [7,8,3] # item
  36. for item in xp:
  37. print(item*2)
  38. # index & item
  39. for index, item in enumerate(xp):
  40. print(index, item)
  41.  
  42. # find the minim item (alpha or numeric types, but cannot mix types
  43. ty = 'bug'
  44. print(min(ty))
  45. sxh = ['pig', 'cow', 'horse']
  46. print(min(sxh)) # cow
  47.  
  48. # find the maximum item in sequence (alpha or numeric types, but cannot mix types
  49. tyy = 'bug'
  50. print(max(ty))
  51. sxhh = ['pig', 'cow', 'horse']
  52. print(max(sxh))
  53.  
  54. # sum (find the sum of items in a sequence+ entire seq must be a num type
  55. tyu = [5,7, 'bug']
  56. # print(sum(tyu)) # error
  57. tyuu = [5,6,7.0]
  58. print(sum(tyuu))
  59. print(sum(tyuu[-2:]))
  60.  
  61. # sorting (returns a new list of items in sorted order)/ does not change the original list
  62. gh = 'bug'
  63. print(sorted(gh))
  64. ghy = ['king kong', 'ghidorah', 'godzilla']
  65. print(sorted(ghy))
  66.  
  67. # count (item)
  68. yu = 'hippo'
  69. print(yu.count('p'))
  70. yuut = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
  71. print(yuut.count('godzilla'))
  72.  
  73. # index returns the index of the first occurrence of an item
  74. fj = 'hiphop'
  75. print(fj.index('p'))
  76. fjj = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
  77. print(fjj.index('godzilla'))
  78.  
  79. # unpacking: unpack n items of a seq into n variables
  80. unp = ['king kong', 'ghidorah', 'godzilla', 'godzilla']
  81. a,b,c, d= unp
  82. print(a,b,c,d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement