cfabio

List.py

Jan 4th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. spam = ['ciao', 'a', 'b']
  2. spam[-2] #element a
  3.  
  4. spam[1:3] #element 1 e 2
  5.  
  6. spam[1:] #tutti gli element a parte lo 0
  7. spam[:1} #solo l'elemento 0
  8.  
  9. del spam[1]
  10.  
  11. len(spam) #number of elements
  12.  
  13. [1,2,3] + [3,4] #[1, 2, 3, 3, 4}
  14. [1, 2} * 3 #[3, 6]
  15.  
  16. list('Hello') #crea una lista H, e, l, l, o
  17.  
  18. 'ciao' in spam   #evaluates to True or False wether or not 'ciao' is present in the container
  19.  
  20. ##############################################
  21.  
  22. print(list(range(100)))     #list 0:99
  23.  
  24. spam = ['a', 'b', 'c']
  25.  
  26. #iterate through elements of a list
  27. for i in range(len(spam)) :
  28.  print('index ' + str(i) + ' value ' + spam[i])
  29.  
  30. #assignment for list
  31. size, color, name = spam
  32. size, color, name = 'skinny', 'black', 'quiet'
  33.  
  34. #swap environment variable values
  35. a = 'aaa'
  36. b = 'bbb'
  37. a, b = b, a
  38.  
  39. ##############################################
  40.  
  41. spam.index['a']
  42.  
  43. spam.append['b']
  44. spam.insert(1, 'bla')
  45.  
  46. spam.remove('bla')
  47. del spam[0]
  48.  
  49. spam.sort()
  50. spam.sort(reverse=True)
  51. spam.sort(key=str.lower)  #case insensitive sorting
  52.  
  53.  
  54. #strings: similar to list
  55. ##when strings get assigned they get copied. This is not true for list!! When the
  56. ##list gets assigned we are only copying the reference!!
  57. #This is true also when calling functions!! List (mutable data) as a parameter
  58. #is taken by reference. String is instead copied
  59.  
  60. #To force the copy of a mutable object
  61. import copy
  62. copy.deepcopy(spam)
Advertisement
Add Comment
Please, Sign In to add comment