Advertisement
Guest User

6510565127

a guest
May 22nd, 2019
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.91 KB | None | 0 0
  1. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
  2. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
  3. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
  4.  
  5. # "turn on" or "activate" a module so that you can call its functions
  6. import operator
  7. import re
  8. import json
  9. import urllib.request
  10. import math
  11. import csv
  12.  
  13. # round a floating-point number upward
  14. print(math.ceil(3.4))
  15.  
  16. # import a single function from a module
  17. from math import log10
  18. print(log10(2000))
  19.  
  20. # reading a string, an integer, and a floating-point number;
  21. # remember to give a prompt (can be passed as param to input, or use separate print statement)
  22. my_string = input()
  23. my_int = int(input())
  24. my_float = float(input())
  25.  
  26. # scientific notation
  27. EARTH_MASS = 5.97E24
  28.  
  29. # exponentiation
  30. volume_of_spherical_cow = (4.0/3.0)*math.pi*(radius ** 3)
  31.  
  32. # printing a floating point number with 2 decimal places, then an int with commas
  33. print(f'Rent is ${rent:.2f} for {size:,} sq ft... Gosh darn high!')
  34.  
  35. # splitting a single string into a list of substrings (e.g., by semi-colons)
  36. my_list = my_string.split(';')
  37.  
  38. # stripping leading and trailing whitespace
  39. my_string.strip()
  40.  
  41. # define a function and return a result
  42. def my_function(x, y, z):
  43.     """
  44.    Remember to describe your function's purpose.
  45.    """
  46.     return x + y + z
  47.  
  48.  
  49. # special characters
  50. print("The tab:\t--nice, huh?")
  51. print("The newline:\n--nice, huh?")
  52.  
  53. # replacing one substring's occurrences with another substring
  54. print("spaces to semicolons".replace(' ', ';'))
  55.  
  56. # taking a substring from position x up to but not including y
  57. my_string[x:y]
  58.  
  59. # taking a sublist from position x up to but not including y
  60. my_list[x:y]
  61.  
  62. # raising an exception and then catching it
  63. try:
  64.     if some_value < 0:
  65.         raise Exception('Ummmmm, well, this is Embarrassing')
  66.     else:
  67.         print("all is well")
  68. except:
  69.     print("an exception happened!")
  70.  
  71. # bailing out of your program entirely
  72. exit()
  73.  
  74. # iterating from 0 up to but not including N, in steps of 2
  75. for i in range(0, N, 2):
  76.     pass # special command that does nothing, useful for empty loops
  77.  
  78. # iterate over a list
  79. for eachitem in my_list:
  80.     print(eachitem)
  81.  
  82. # finding the location of a substring inside of a longer string
  83. # (-1 if not found)
  84. my_string.find(desired_substring)
  85.  
  86. # grab substring using regular expression
  87. import re
  88. my_string = 'grab the substring from abcdefgh Donald ijklmnopqrst'
  89. match_info = re.search('abcdefgh ([A-Za-z]+) ijklmnopqrs', my_string)
  90. if match_info:
  91.     part_inside_parentheses = match_info.group(1)
  92.  
  93. # reference for regular expressions
  94. #  [0-9] or \d  -- digit
  95. #  [a-zA-Z] -- any lowercase or uppercase character
  96. #  [a-z0-9_] -- any lowercase character, digit, or underscore
  97. #  .       -- special character representing any regular character
  98. #  ^       -- special character representing start of string
  99. #  $       -- special character representing end of string
  100. #  +       -- repeat whatever comes before, one or more times
  101. #  *       -- repeat whatever comes before, zero or more times
  102. #  ?       -- whatever comes before is optional (0 or more times)
  103. #  {x}     -- whatever comes before occurs exactly x times
  104. #  {x,y}   -- whatever comes before occurs x to y times
  105.  
  106.  
  107. # building up a list of 1 through 10
  108. my_list = []
  109. for i in range(1, 11):
  110.     my_list.append(i)
  111.  
  112. # get the length, total, minimum, and maximum of a list of numbers
  113. print("length: "+str(len(my_list)))
  114. print("total: "+str(sum(my_list)))
  115. print("minimum: "+str(min(my_list)))
  116. print("maximum: "+str(max(my_list)))
  117.  
  118. # sorting all the items in a list
  119. sorted_list = sorted(my_list)
  120.  
  121. # building up a dictionary
  122. all_about_me = {}
  123. all_about_me["name"] = "Sir Launcelot of Camelot"
  124. all_about_me["quest"] = "To seek the Holy Grail"
  125. all_about_me["favorite color"] = "Blue"
  126.  
  127. # a list of dictionaries
  128. cars = [
  129.     {"buyer": "Jane Edwards", "price": 6000, "make": "Honda"},
  130.     {"buyer": "Ahmed Moustef", "price": 9000, "make": "Honda"},
  131.     {"buyer": "Mike McGinney", "price": 2000, "make": "Ford"},
  132.     {"buyer": "Jeffrey Pouch", "price": 4000, "make": "Honda"},
  133.     {"buyer": "Beatrice Duff", "price": 6000, "make": "GM"}
  134. ]
  135.  
  136. # getting the value of a dictionary, with a default in case the key is missing
  137. the_value = the_dictionary.get(the_key, the_default)
  138. price = car.get("price", 0)
  139.  
  140. # computing a total for each category, from a list of dictionaries
  141. totals = {}
  142. for car in cars:
  143.     category = car["make"]
  144.     price = car["price"]
  145.     totals[category] = totals.get(category, 0) + price
  146.  
  147. # finding which entry corresponds to the maximum
  148. max_value = None
  149. max_buyer = None
  150. for car in cars:
  151.     entry = car["buyer"]
  152.     price = car["price"]
  153.     if max_value is None or price > max_value:
  154.         max_value = price
  155.         max_buyer = entry
  156.  
  157. # a dictionary of lists
  158. attendees = {
  159.     "CS 201": ["Judy", "Bobby", "Kesha"],
  160.     "CS 160": ["Randy", "Dandy", "Marsha"],
  161.     "CS 361": ["Poppy", "Moppy", "Pasha"]
  162. }
  163.  
  164. # iterating over a dictionary, printing it out
  165. for the_key in attendees.keys():
  166.     print(the_key+':'+str(attendees[the_key]))
  167.  
  168. # also can just get all the values directly (though unclear which value goes with which key)
  169. print(attendees.values())
  170.  
  171. # iterating through the lines of a text file
  172. data_file = open(file_name)
  173. for line in data_file:
  174.     pass
  175.  
  176. # create a CSV reader from a file reference (or "handle") so that you can iterate over rows
  177. # https://docs.python.org/3/library/csv.html
  178. csv_reader = csv.reader(file_handle)
  179.  
  180. # can convert a CSV reader directly into a list of lists
  181. course_list = list(csv_reader)
  182.  
  183. # read from a URL into JSON
  184. json_string = urllib.request.urlopen(URL).read()
  185. resulting_dict_or_list = json.loads(json_string)
  186.  
  187.  
  188. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
  189. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
  190. # 6510565127 6510565127 6510565127 6510565127 6510565127 6510565127
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement