Advertisement
aramideoluwatosin

week2

Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from __future__ import division
  4. a=7/3
  5. b=round(a, 4)
  6. c=9
  7. d=10
  8. c+=1
  9. d-=1
  10. print (a)
  11. print(b)
  12. print(c)
  13. print(d)
  14.  
  15. new_file=open("file.txt")
  16. # default mode is a read
  17. open_new_file=new_file.read()
  18. print (open_new_file)
  19. #real world application
  20. new_file.close()
  21.  
  22.  
  23. #reading file line by line
  24. f_readline=open("file.txt")
  25. open_f_readline=f_readline.readline()
  26. print(open_f_readline)
  27.  
  28. #reading all the lines in one goal: it will return a list that have all the lines of the file
  29. f_readlines=open("file.txt")
  30. open_f_readlines=f_readlines.readlines()
  31. print(open_f_readlines)
  32.  
  33.  
  34.  
  35. #contest management way, python automatically close the file handle.
  36. with open("file.txt") as f:
  37. output=f.readline()
  38. print (output)
  39.  
  40.  
  41.  
  42.  
  43. #write operations
  44.  
  45. v=open('new_file1.txt', mode='w')
  46. v.write('something special\n')
  47. v.flush()
  48.  
  49. #write operation
  50. with open('new_file.txt', mode='w') as m:
  51. m_output=m.write('something special\n')
  52.  
  53. #ppend operation
  54. z=open('new_file1.txt', mode='a')
  55. z.write('something special\n')
  56. z.flush()
  57.  
  58.  
  59. ########################################
  60. ############LISTS######################
  61. #######################################
  62.  
  63. my_list=[]
  64.  
  65. my_list.append('tosin')
  66. print (my_list)
  67. my_list.append('dayo')
  68. print( (my_list) + [1,2,3])
  69.  
  70. my_new_list=[1,2,3,'tosin','dayo']
  71.  
  72. x=my_new_list.extend([10,'dayo'])
  73. print(x)
  74. #remove the last element
  75. y=x.pop()
  76. #removes the last on the list
  77. print(y)
  78.  
  79. #remove from the first index
  80. my_new_list.pop(0)
  81. #OR
  82. del my_new_list[2]
  83.  
  84.  
  85. #counts number of times 'tosin' is in the list
  86. my_list.count('tosin')
  87.  
  88.  
  89. #index where hello first appeared
  90. my_list.index('tosin')
  91.  
  92.  
  93.  
  94. # https://www.tutorialspoint.com/python/python_lists.htm
  95.  
  96.  
  97. my_list=[1,2,3,'amazon','google']
  98. new_list=['UN','hertz']
  99. my_list.append(new_list)
  100.  
  101.  
  102. for i in range(0,len(my_list)):
  103. print i
  104.  
  105. for i in range(0,len(my_list)):
  106. print (str(i) +' is equal to : ' + str(my_list[i]))
  107.  
  108.  
  109.  
  110. ##########TUPLE############
  111. txt = 'but soft what light in yonder window breaks'
  112. words = txt.split()
  113. t = list()
  114. for word in words:
  115. t.append((len(word), word))
  116.  
  117. t.sort(reverse=True)
  118.  
  119. res = list()
  120. for length, word in t:
  121. res.append(word)
  122.  
  123. print res
  124.  
  125. d = {'a':10, 'b':1, 'c':22}
  126. l = list()
  127. for key, val in d.items() :
  128. l.append( (val, key) )
  129. datacenter1 = {'spines': ['r1', 'r2', 'r3', 'r4']}
  130. datacenter1['leafs']=['l1', 'l2', 'l3', 'l4']
  131.  
  132. for i,j in datacenter1.items():
  133. print(i,j)
  134. l
  135.  
  136. m = [ 'have', 'fun' ]
  137. x, y = m
  138. x
  139. y
  140.  
  141.  
  142. d = {'a':10, 'b':1, 'c':22}
  143. t=d.items()
  144. print t
  145.  
  146.  
  147.  
  148. ipv6='2001:0db8:85a3:0000:0000:8a2e:0370:7334'
  149. x=ipv6.split(':')
  150. y=(':').join(x)
  151.  
  152.  
  153. ******SYS>ARGV*****
  154.  
  155. #!/usr/bin/enc python
  156. from __future__ import print_function
  157. import sys
  158.  
  159. print(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement