tuomasvaltanen

Untitled

Sep 6th, 2022 (edited)
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. # Luento 2 - math-, random- ja datetime-moduulit ja ym.
  2.  
  3. # rivinvaihto merkitään \n, ja niitä voi olla useampi peräkkäin
  4. print("Tässä on esimerkkiteksti\n\nTämä tulee toiselle riville")
  5. print()
  6. print("Lisää tekstiä")
  7.  
  8. # \t on sarkain, voidaan asemoida samalle tasolle sisältöä
  9. # huom: pitkien sanojen kohdalla joutuu kompensoimaan muita rivejä
  10. # jotta sarkain osuu kohdalle
  11. # myös tabulate-moduulia voi käyttää tällaisissa tarkoituksissa!
  12. # Googlella löytyy esimerkkejä
  13. print("Lasku:\t\t350€")
  14. print("Korko:\t\t5%")
  15. print("Päivämäärä:\t6.9.2022")
  16.  
  17. # UUSI TIEDOSTO
  18.  
  19. # Vaihe 1: pyydetään käyttäjältä muuttujia
  20. savings = input("Kuinka paljon sinulla on säästöjä?\n")
  21. savings = int(savings)
  22.  
  23. salary = input("Kuinka paljoin sait tässä kuussa palkkaa?\n")
  24. salary = int(salary)
  25.  
  26. # Vaihe 2: laskutoimitukset ym
  27. increase = 1.05
  28.  
  29. # Vaihe 3: yhteenlaskut ja tulostus
  30. total = (savings + salary) * increase
  31. print(f"Summa: {total}")
  32.  
  33. # UUSI TIEDOSTO
  34.  
  35. import math
  36.  
  37. print(math.pi)
  38.  
  39. # 2 * pi * säde
  40.  
  41. radius = 13
  42.  
  43. border = 2 * math.pi * radius
  44.  
  45. border = round(border, 2)
  46. print(f"Ympärysmitta: {border} cm")
  47.  
  48. # UUSI TIEDOSTO
  49.  
  50. import math
  51.  
  52. print(math.pi)
  53.  
  54. # 2 * pi * säde
  55.  
  56. radius = input("Anna säde:\n")
  57. radius = float(radius)
  58.  
  59. border = 2 * math.pi * radius
  60.  
  61. border = round(border, 2)
  62. print(f"Ympärysmitta: {border} cm")
  63.  
  64. # UUSI TIEDOSTO
  65.  
  66. import math
  67.  
  68. # ns. perinteinen tapa math-moduulilla, potenssilasku
  69. power = math.pow(5, 7)
  70. print(power)
  71.  
  72. # Python 3:ssa voi tehdä myös näin
  73. number = 5 ** 7
  74. print(number)
  75.  
  76. # neliöjuuri, eli square root
  77. test = math.sqrt(9)
  78. print(test)
  79.  
  80. # kuutio kulmasta kulmaan
  81. # d = s √3
  82. # käännetään matemaattinen kaava koodimuotoon
  83. # eli d = radius * math.sqrt(3)
  84. radius = 12
  85. d = radius * math.sqrt(3)
  86. print(d)
  87.  
  88. # UUSI TIEDOSTO
  89.  
  90. import random
  91.  
  92. # arvotaan numero 4 ja 10 väliltä
  93. number = random.randint(4, 10)
  94. print(number)
  95.  
  96. print()
  97.  
  98. # arvotaan kaksi noppaa (1-6)
  99. dice1 = random.randint(1, 6)
  100. dice2 = random.randint(1, 6)
  101.  
  102. print(dice1)
  103. print(dice2)
  104.  
  105. # UUSI TIEDOSTO
  106.  
  107. from datetime import date
  108.  
  109. # UTC -aikaleimaformaatti (timestamp)
  110. today = date.today()
  111. print(today)
  112.  
  113. # UUSI TIEDOSTO
  114.  
  115. from datetime import datetime
  116.  
  117. today = datetime.now()
  118. # print(today)
  119.  
  120. # %d = päivä, %m = kuukausi, %Y = vuosi, %H = tunti, %M = minuutti, %S = sekunti
  121. text = today.strftime("%d.%m.%Y %H:%M:%S")
  122.  
  123. print(text)
  124.  
  125. # UUSI TIEDOSTO
  126.  
  127. from datetime import date, datetime, timedelta
  128.  
  129. # kuinka monta päivää on vuoden loppuun?
  130. first = date(2022, 9, 6)
  131. second = date(2022, 12, 31)
  132. delta = second - first
  133. days = delta.days
  134.  
  135. print(f"Päiviä: {days}")
  136.  
  137. print()
  138.  
  139. # esim. kirjastosta lainataan tänään kirja, milloin on palautuspäivä
  140. # jos laina-aika on 3 viikkoa? (eli 21 vuorokautta)
  141. loan_date = datetime.now()
  142. return_date = loan_date + timedelta(21)
  143. print(return_date)
Advertisement
Add Comment
Please, Sign In to add comment