Advertisement
tuomasvaltanen

Untitled

Sep 25th, 2023 (edited)
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. # math-, random- and datetime modules today!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # adding new lines within text
  7. print("Some text here.\n\nAnother line!")
  8.  
  9. # an empty print() also prints a single \n by default
  10. print()
  11.  
  12. # tabs can be used to align pieces of text horizontally
  13. # remember to have enough \t to match long words
  14. print("Total:\t\t350 €")
  15. print("Postage:\t40 €")
  16.  
  17. # NEW FILE
  18.  
  19. # this kind of a structure is quite clear
  20. # for the exercise sets etc.
  21.  
  22. # PART 1: ask all needed values from the user
  23. # ask the values from the user
  24. salary = input("Give your salary:\n")
  25. salary = float(salary)
  26.  
  27. savings = input("How much savings do you have?\n")
  28. savings = float(savings)
  29.  
  30. # PART 2 - the actual calculation logic of the code
  31. # combine salary and savings, add 25%
  32. total = (salary + savings) * 1.25
  33.  
  34. # PART 3 - print out the results
  35. print(f"Total money: {total} €")
  36.  
  37. # NEW FILE
  38.  
  39. import math
  40.  
  41. # avoid hardcoded Pi-values  (like 3.14)
  42. # always use math.pi
  43. print(math.pi)
  44. print()
  45.  
  46. # radius = 13
  47. radius = input("Give a radius:\n")
  48. radius = int(radius)
  49.  
  50. # circumference of the circle
  51. # 2 * pi * radius
  52. border = 2 * math.pi * radius
  53.  
  54. # round the border to two decimals
  55. # remember to save to variable
  56. # only round(border, 2) does not work in code
  57. border = round(border, 2)
  58. print(f"Circumference: {border} m")
  59.  
  60. # NEW FILE
  61.  
  62. import math
  63.  
  64. # 3 to the power of 5
  65. number1 = math.pow(3, 5)
  66. print(number1)
  67.  
  68. # ... or Python
  69. number2 = 3 ** 5
  70. print(number2)
  71.  
  72. number3 = 25
  73. root_value = math.sqrt(number3)
  74. print(root_value)
  75.  
  76. # length of a body diagonal of a cube = sqrt 3 * side
  77. side = 14
  78. diagonal = math.sqrt(3) * side
  79. print(diagonal)
  80.  
  81. # NEW FILE
  82.  
  83. import random
  84.  
  85. # generate a random number between 1 and 10
  86. guess = random.randint(1, 10)
  87. print(guess)
  88. print()
  89.  
  90. # let's generate two random dice
  91. # basic dice 1-6
  92. dice1 = random.randint(1, 6)
  93. dice2 = random.randint(1, 6)
  94.  
  95. print(dice1)
  96. print(dice2)
  97.  
  98. # NEW FILE
  99.  
  100. from datetime import datetime
  101. # if we used import datetime, our code would need to be
  102. # today = datetime.datetime.now()
  103.  
  104. today = datetime.now()
  105.  
  106. # print(today)
  107. # formatting logic for date
  108. # %d = day, %m = month, %Y = year, %H = hour, %M = minutes, %S = second
  109. # removing the extra zeroes in days and months:
  110. # in Windows: %#d and %#m
  111. # in Unix / Linux / MacOS %-d and %-m
  112. date_text = today.strftime("%d.%m.%Y %H:%M:%S")
  113. print(date_text)
  114.  
  115. # NEW FILE
  116.  
  117. from datetime import date, datetime, timedelta
  118.  
  119. first = date(2023, 9, 25)
  120. second = date(2023, 12, 31)
  121.  
  122. delta = second - first
  123. days = delta.days
  124.  
  125. # end result
  126. print(f"Days left for this year: {days}")
  127.  
  128. # if we create a bill today, what is the due date in 2 weeks?
  129. # e.g. = 14 days
  130. today = datetime.now()
  131. today = today + timedelta(14)
  132.  
  133. date_text = today.strftime("%d.%m.%Y")
  134. print(date_text)
  135.  
  136. # NEW FILE
  137.  
  138. # careful with float-numbers, with small values, you might get weird decimals
  139. # use either round() or Decimal-module to fix this
  140. number1 = 0.1
  141. number2 = 0.2
  142.  
  143. print(number1 + number2)
  144.  
  145. # NEW FILE
  146.  
  147. from datetime import date
  148. import math
  149.  
  150. # variables for the compound interest calculation
  151. start_money = 25000
  152. interest_rate = 0.07
  153.  
  154. # calculate the amount of days and years in this timespan
  155. start_date = date.today()
  156. end_date = date(2033, 12, 31)
  157.  
  158. # for how many years are we going to save money?
  159. delta = end_date - start_date
  160. days = delta.days
  161. years = days // 365
  162.  
  163. # compound interest
  164. # final amount of money = start money * (1 + interest_rate) ^ years
  165. total_money = start_money * math.pow(1 + interest_rate, years)
  166. new_money = total_money - start_money
  167. new_money = round(new_money, 2)
  168. print(f"With given parameters, we earned this much money {new_money} €")
  169.  
  170. # NEW FILE
  171. # https://mathsisfun.com/algebra/exponential-growth.html
  172.  
  173. import math
  174. from datetime import datetime
  175.  
  176. # when we drank the last coffee
  177. start_time = datetime(2023, 9 , 25, 9, 0, 0)
  178. end_time = datetime(2023, 9, 25, 22, 0, 0)
  179.  
  180. # how many hours ago?
  181. duration = end_time - start_time
  182. seconds = duration.total_seconds()
  183. minutes = seconds / 60
  184. hours = minutes // 60
  185. hours = int(hours)
  186.  
  187. # let's assume half-life of coffee is 5 hours
  188. half_life = 5
  189.  
  190. # let's assume our coffee cup is 300ml
  191. cup = 300
  192.  
  193. # half-life = cup * exp ^ (ln(0.5)/half_life)*hours
  194. logarithm = math.log(0.5) / half_life
  195. coffee_left = cup * math.exp(logarithm * hours)
  196. coffee_left = int(coffee_left)
  197.  
  198. print(f"From the original {cup} ml of coffee I drank {hours} hours ago.")
  199. print(f"{coffee_left} ml of coffee is still left in my body.")
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement