Advertisement
tuomasvaltanen

Untitled

Sep 21st, 2022 (edited)
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. # math-, random- and datetime-modules today!
  2. print("Welcome!")
  3.  
  4. # NEW FILE
  5.  
  6. # math-, random- and datetime-modules today!
  7.  
  8. # adding new lines among text
  9. print("Here's some text\n\n\nAnother line")
  10.  
  11. # printing without text also prints a new line by default
  12. print()
  13.  
  14. # you can use tabs to position texts on screen
  15. print("Total:\t\t350€")
  16. print("Total cost:\t199€")
  17.  
  18. # NEW FILE
  19.  
  20. # first phase, ask all needed variables from user with input()
  21. salary = input("Give your salary:\n")
  22. salary = float(salary)
  23.  
  24. savings = input("How much savings do you have:\n")
  25. savings = float(savings)
  26.  
  27. # second phase, do the calculations
  28.  
  29. # our total money is salary + savings, increased by 25%
  30. total = (salary + savings) * 1.25
  31.  
  32. # third phase, print out the results
  33. print(f"Total money: {total} €")
  34.  
  35. # NEW FILE
  36.  
  37. import math
  38.  
  39. print(math.pi)
  40.  
  41. # border of circle = 2 * pi * radius
  42. radius = input("Give radius:\n")
  43. radius = int(radius)
  44.  
  45. border = 2 * math.pi * radius
  46. print(f"Border: {border}")
  47.  
  48. # NEW FILE
  49.  
  50. import math
  51.  
  52. print(math.pi)
  53.  
  54. # border of circle = 2 * pi * radius
  55. radius = input("Give radius:\n")
  56. radius = int(radius)
  57.  
  58. # computer border and round to two decimals
  59. border = 2 * math.pi * radius
  60. print(f"Border: {border}")
  61.  
  62. border = round(border, 2)
  63. print(f"Border: {border}")
  64.  
  65. # NEW FILE
  66.  
  67. import math
  68.  
  69. # 5 to the power of 8, two ways
  70. number1 = 5 ** 8
  71. number2 = math.pow(5, 8)
  72.  
  73. print(number1)
  74. print(number2)
  75.  
  76. # square root of 25 is 5
  77. number3 = 25
  78. root_value = math.sqrt(number3)
  79. print(root_value)
  80.  
  81. # programmers often have to convert mathematical
  82. # equations into actual code, example:
  83. # diagonal of a cube = square root of 3 * side
  84. side = 15
  85. diagonal = math.sqrt(3) * side
  86.  
  87. # NEW FILE
  88.  
  89. import random
  90.  
  91. # random number between 1 and 10
  92. guess = random.randint(1, 10)
  93. print(guess)
  94. print()
  95.  
  96. # let's generate two random dice
  97. dice1 = random.randint(1, 6)
  98. dice2 = random.randint(1, 6)
  99.  
  100. # print the dice values
  101. print(dice1)
  102. print(dice2)
  103.  
  104. # UUSI TIEDOSTO
  105.  
  106. from datetime import date
  107.  
  108. # get current date, YEAR-MONTH-DAY
  109. today = date.today()
  110.  
  111. print(f"Today is {today}")
  112.  
  113. # NEW FILE
  114.  
  115. from datetime import datetime
  116.  
  117. today = datetime.now()
  118. print(today)
  119.  
  120. # formatting logic for date:
  121. # %d = day, %m = month, %Y = year, %H = hour, %M = minutes, %S = seconds
  122. date_text = today.strftime("%d.%m.%Y %H:%M:%S")
  123.  
  124. print(date_text)
  125.  
  126. # NEW FILE
  127.  
  128. from datetime import date, datetime, timedelta
  129.  
  130. # how many days are left this year
  131. today = date.today()
  132. future = date(2022, 12, 31)
  133.  
  134. delta = future - today
  135. days = delta.days
  136.  
  137. print(f"Days left this year: {days}")
  138.  
  139. # if we borrow a book today, and the return is after 14 days,
  140. # when is the actual return date?
  141. today = datetime.now()
  142. today = today + timedelta(14)
  143. print(today)
  144.  
  145. # NEW FILE, BIGGER EXAMPLE
  146.  
  147. from datetime import date
  148. import math
  149.  
  150. # compound interest formula
  151. # future money = start money * math.pow(1 + interest rate, number of years)
  152.  
  153. # start money and interest rate (decimal)
  154. start_money = 25000
  155. interest_rate = 0.07
  156.  
  157. # how many years between two dates
  158. start_date = date.today()
  159. end_date = date(2032, 12, 31)
  160. delta = end_date - start_date
  161. days = delta.days
  162. years = days // 365
  163.  
  164. # compound interest
  165. total_money = start_money * math.pow(1 + interest_rate, years)
  166.  
  167. # actual money earnt while saving
  168. new_money = total_money - start_money
  169. new_money = round(new_money, 2)
  170.  
  171. # all done, print out results
  172. print("With the given parameters, we earnt this much money:")
  173. print(f"{new_money} €")
  174.  
  175. # ANOTHER EXAMPLE, HOW MUCH TIME IT TAKES TO HALVE THE AMOUNT OF COFFEE IN YOUR BODY
  176.  
  177. import math
  178. from datetime import datetime
  179.  
  180. start_time = datetime(2022, 9, 21, 13, 45, 0)
  181. end_time = datetime(2022, 9, 21, 23, 45, 0)
  182. duration = end_time - start_time
  183. seconds = duration.total_seconds()
  184. minutes = seconds / 60
  185. hours = minutes / 60
  186.  
  187. hours = int(hours)
  188.  
  189. # 300 ml
  190. cup = 300
  191.  
  192. # coffee half-life 4 hours, found from Google
  193. half_life = 4
  194.  
  195. # https://www.mathsisfun.com/algebra/exponential-growth.html
  196. # 9 hours => cup * exp ** (log(0.5) / half_life) * hours
  197. logarithm = math.log(0.5) / half_life
  198. coffee_left = cup * math.exp(logarithm * hours)
  199.  
  200. coffee_left = round(coffee_left)
  201.  
  202. # calculate the amount of coffee you have in your body, after a certain time
  203. print(f"Coffee left: {coffee_left} ml")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement