Advertisement
tuomasvaltanen

Untitled

Sep 10th, 2024 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.86 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!\nAnother line!\n\n")
  8.  
  9. # empty print() also adds a single \n by default
  10. # sometimes \n is not enough, and you also need to add \r (carriage return)
  11. print()
  12.  
  13. # tabs can be used to align pieces of text horizontally
  14. # remember to have enough \t to match long words
  15. print("Total:\t\t350 €")
  16. print("Tax:\t\t120 €")
  17. print("Postage: \t 20 €")
  18.  
  19. # NEW FILE
  20.  
  21. # recap on how to get data from users (input)
  22. # and how to combine them into a calculation
  23.  
  24. # THIS IS THE TYPICAL STRUCTURE OF MOST OF THE EXERCISES
  25.  
  26. # PHASE 1: get user input values
  27.  
  28. # ask the salary from user and convert
  29. # the value from text to decimal (float)
  30. salary = input("Write your salary:\n")
  31. salary = float(salary)
  32.  
  33. # ask also the savings
  34. savings = input("How much savings do you have?\n")
  35. savings = float(savings)
  36.  
  37. # helper variable for increase percentage, +15%
  38. increase = 1.15
  39.  
  40. # it's okay also to combine input + float (or int) on the same line
  41. # savings = float(input("How much savings do you have?\n"))
  42.  
  43. # PHASE 2 - the actual calculation logic of the code
  44. # this part usually starts to grow longer and more complex
  45. # as we go further in the course
  46. total = (salary + savings) * increase
  47.  
  48. # PHASE 3 - print out the results for the user
  49. # use f-string to combine text and the number (total)
  50. print(f"Total money: {total} €")
  51.  
  52. # NEW FILE
  53.  
  54. import math
  55.  
  56. # make sure you don't have a file called
  57. # math.py in your project anywhere (confuses the import
  58. # otherwise
  59.  
  60. # try to print the pi-value from math
  61. # don't write your own pi values with less decimals like pi = 3.14
  62. # always use math.pi in your code
  63. print(math.pi)
  64.  
  65. # let's try some math
  66. radius = 13
  67.  
  68. # circumference of a circle, 2 * pi * radius
  69. border = 2 * math.pi * radius
  70. border = round(border, 2)
  71.  
  72. # only using round(border, 2) doesn't work
  73. # because you need to save the result into a variable
  74.  
  75. # print the result
  76. print(f"Circumference: {border} cm")
  77.  
  78. # NEW FILE
  79.  
  80. import math
  81.  
  82. # 3 to the power of 5
  83. number1 = math.pow(3, 5)
  84. print(number1)
  85.  
  86. # Python also support power itself
  87. number2 = 3 ** 5
  88. print(number2)
  89.  
  90. # don't do manual powering, like this:
  91. # side = 10
  92. # total = side * side * side
  93.  
  94. # square root
  95. number3 = 25
  96. root_value = math.sqrt(number3)
  97. print(root_value)
  98.  
  99. # for example, diagonal of a cube => d = sqrt 3 * side
  100. side = 14
  101. diagonal = math.sqrt(3) * side
  102. print(diagonal)
  103.  
  104. # NEW FILE
  105.  
  106. import random
  107.  
  108. # do not create random.py anywhere in the project
  109. # just like math -module, it will confuse the import
  110.  
  111. # generate a number between 1 and 10
  112. guess = random.randint(1, 10)
  113. print(guess)
  114.  
  115. # let's generate two random dice, basic dice 1-6
  116. # a tip: you can duplicate a code file by selecting it
  117. # and pressing Ctrl + D on Windows, and CMD + D on MacOS
  118. dice1 = random.randint(1, 6)
  119. dice2 = random.randint(1, 6)
  120.  
  121. print(dice1)
  122. print(dice2)
  123.  
  124. # NEW FILE
  125.  
  126. from datetime import date
  127.  
  128. # get the current date
  129. today = date.today()
  130. print(f"Today is: {today}")
  131.  
  132. # NEW FILE
  133.  
  134. from datetime import datetime
  135. # if we used the normal import, like:
  136. # import datetime
  137. # we would have to use it like this:
  138. # today = datetime.datetime.now()
  139.  
  140. # get current time and date
  141. today = datetime.now()
  142. print(today)
  143.  
  144. # if we want to format the timestamp into a prettier format for the user
  145. # we an use the strftime() -function
  146. # the format we want: day.month.year hours:minutes:seconds
  147. # %d = day, %m = month, %Y = year, %H = hour, %M = minute, %S = second
  148. # if you want to remove the extra zeroes in days and months:
  149. # in Windows: %#d and %#m
  150. # in Unix / Linux / MacOS %-d and %-m
  151. date_text = today.strftime("%d.%m.%Y %H:%M:%S")
  152. print(date_text)
  153.  
  154. # NEW FILE
  155.  
  156. from datetime import date, datetime, timedelta
  157.  
  158. # two timestamps
  159. first = date(2024, 9, 10)
  160. second = date(2024, 12, 31)
  161.  
  162. # calculate the difference
  163. delta = second - first
  164. days = delta.days
  165.  
  166. # print the result
  167. print(f"Days left this year: {days}")
  168.  
  169. # example, if we create a bill for a customer today
  170. # what is the due date in 2 weeks (14 days)
  171. today = datetime.now()
  172. today = today + timedelta(14)
  173.  
  174. date_text = today.strftime("%d.%m.%Y")
  175. print(date_text)
  176.  
  177. # NEW FILE, tips for the first advanced task
  178.  
  179. # replace this with input() etc.
  180. cents = 87
  181.  
  182. # the task is to find and divide the amount of
  183. # 50 cents, 20 cents, 10 cents, 5 cents, 2 cents and 1 cents
  184.  
  185. # with the CURRENT cents, find out how many full 50 cent coins we can have
  186. coins50 = cents // 50
  187.  
  188. # update cents, what is left over from previous calculation
  189. cents = cents % 50
  190.  
  191. # now with the 20 cents
  192. coins20 = cents // 20
  193.  
  194. # update the current cents
  195. cents = cents % 20
  196.  
  197. print(f"50 cent coins: {coins50}")
  198. print(f"20 cent coins: {coins20}")
  199. print(f"Cents left at the moment: {cents}")
  200.  
  201. # and so on continue alternating between the division
  202. # and the remainder until we reach 1 cent
  203.  
  204. # NEW FILE - an example where we combine different modules
  205.  
  206. # Compound interest, math experiment
  207. # https://www.freshbooks.com/wp-content/uploads/2024/07/Calculating-Compound-Interest.png
  208.  
  209. # compound interest, or (interest of interest) is a concept of return of investement
  210. # where interest starts to accumulate interest on itself as well
  211.  
  212. from datetime import date
  213. import math
  214.  
  215. # variables for the compound interest calculation
  216. start_money = 25000
  217. interest_rate = 7
  218.  
  219. # calculate the amount of days and years in this timespan
  220. start_date = date.today()
  221. end_date = date(2033, 12, 31)
  222.  
  223. # for how many years are we going to save money?
  224. delta = end_date - start_date
  225. days = delta.days
  226. years = days // 365
  227.  
  228. # compound interest
  229. # final amount of money = start money * (1 + interest_rate / 100) ^ years
  230. total_money = start_money * math.pow(1 + interest_rate / 100, years)
  231. new_money = total_money - start_money
  232. new_money = round(new_money, 2)
  233. print(f"With given parameters, we earned this much money {new_money} €")
  234.  
  235. # NEW FILE - another example of combining modules
  236.  
  237. # original half-life formula from here:
  238. # https://mathsisfun.com/algebra/exponential-growth.html
  239.  
  240. import math
  241. from datetime import datetime
  242.  
  243. # when we drank the last coffee
  244. start_time = datetime(2024, 9, 10, 9, 0, 0)
  245. end_time = datetime(2024, 9, 10, 20, 0, 0)
  246.  
  247. # how many hours ago?
  248. duration = end_time - start_time
  249. seconds = duration.total_seconds()
  250. minutes = seconds / 60
  251. hours = minutes // 60
  252. hours = int(hours)
  253.  
  254. # let's assume half-life of coffee is 5 hours
  255. half_life = 5
  256.  
  257. # let's assume our coffee cup is 300ml
  258. cup = 300
  259.  
  260. # do the calculations
  261. # half-life = cup * exp ^ (ln(0.5)/half_life)*hours
  262. logarithm = math.log(0.5) / half_life
  263. coffee_left = cup * math.exp(logarithm * hours)
  264. coffee_left = int(coffee_left)
  265.  
  266. # print out the results
  267. print(f"From the original {cup} ml of coffee I drank {hours} hours ago.")
  268. print(f"{coffee_left} ml of coffee is still left in my body.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement