tuomasvaltanen

Untitled

Sep 20th, 2021 (edited)
608
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Lecture 2, math-, random- and datetime-modules!
  2. print("Welcome!")
  3.  
  4. # in Windows sometimes, \n does not work, use \r instead (carriage return)
  5. print("Today we'll go through\n\n\nsome modules!")
  6.  
  7. # using tabs
  8. bill = 499
  9. print(f"Total:\t\t\t {bill}")
  10. print(f"Grand total:\t {bill}")
  11.  
  12. # NEW FILE
  13.  
  14. # get two values from user and sum them up
  15. number1 = input("First number:\n")
  16. number1 = int(number1)
  17.  
  18. number2 = input("Second number:\n")
  19. number2 = int(number2)
  20.  
  21. total = number1 + number2
  22. print(f"Total: {total}")
  23.  
  24. # NEW FILE
  25.  
  26. import math
  27.  
  28. print(math.pi)
  29.  
  30. number = input("Give a decimal number:\n")
  31. number = float(number)
  32.  
  33. number = round(number, 2)
  34.  
  35. print(number)
  36.  
  37. # NEW FILE
  38.  
  39. import math
  40.  
  41. # powers, 4 to power of 6
  42. number1 = math.pow(4, 6)
  43. print(number1)
  44.  
  45. # python 3, this works too
  46. number2 = 4 ** 6
  47. print(number2)
  48.  
  49. # square roots
  50. number3 = 9
  51. square = math.sqrt(number3)
  52. print(square)
  53.  
  54. # circumference = 2 * pi * r
  55. radius = 5
  56. border = 2 * math.pi * radius
  57. print(border)
  58.  
  59. # cube diagonal
  60. # d = √3 x
  61. side = 15
  62. diagonal = math.sqrt(3) * side
  63. print(diagonal)
  64.  
  65. # NEW FILE
  66.  
  67. import random
  68.  
  69. # generate a random number between 1-20
  70. guess = random.randint(1, 20)
  71. print(guess)
  72. print()
  73.  
  74. # two standard dice, 1-6
  75. dice1 = random.randint(1, 6)
  76. dice2 = random.randint(1, 6)
  77.  
  78. # print dice values
  79. print(dice1)
  80. print(dice2)
  81.  
  82. # total value of two dice
  83. print(dice1 + dice2)
  84.  
  85. # NEW FILE
  86. # date example
  87.  
  88. from datetime import date
  89.  
  90. today = date.today()
  91. print(today)
  92.  
  93.  
  94. # NEW FILE
  95. # datetime example
  96.  
  97. from datetime import datetime
  98.  
  99. today = datetime.now()
  100. print(today)
  101.  
  102. # %d = day, %m = month, %Y = year, %H = hour, %M = minute, %S = second
  103. date_text = today.strftime("%d.%m.%Y %H:%M:%S")
  104. print(date_text)
  105.  
  106.  
  107. # NEW FILE
  108. # the floating point number problem with small decimals
  109.  
  110. from decimal import Decimal
  111.  
  112. # this is a somewhat tedious feature of the float data type
  113. number1 = float(0.1)
  114. number2 = float(0.2)
  115.  
  116. print(f"Normal float/decimal numbers: {number1} + {number2} =")
  117. print(number1 + number2)
  118.  
  119. number3 = Decimal("0.1")
  120. number4 = Decimal("0.2")
  121.  
  122. print(number3 + number4)
  123.  
RAW Paste Data