Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # math-, random- and datetime-modules today!
- print("Welcome!")
- # NEW FILE
- # math-, random- and datetime-modules today!
- # adding new lines among text
- print("Here's some text\n\n\nAnother line")
- # printing without text also prints a new line by default
- print()
- # you can use tabs to position texts on screen
- print("Total:\t\t350€")
- print("Total cost:\t199€")
- # NEW FILE
- # first phase, ask all needed variables from user with input()
- salary = input("Give your salary:\n")
- salary = float(salary)
- savings = input("How much savings do you have:\n")
- savings = float(savings)
- # second phase, do the calculations
- # our total money is salary + savings, increased by 25%
- total = (salary + savings) * 1.25
- # third phase, print out the results
- print(f"Total money: {total} €")
- # NEW FILE
- import math
- print(math.pi)
- # border of circle = 2 * pi * radius
- radius = input("Give radius:\n")
- radius = int(radius)
- border = 2 * math.pi * radius
- print(f"Border: {border}")
- # NEW FILE
- import math
- print(math.pi)
- # border of circle = 2 * pi * radius
- radius = input("Give radius:\n")
- radius = int(radius)
- # computer border and round to two decimals
- border = 2 * math.pi * radius
- print(f"Border: {border}")
- border = round(border, 2)
- print(f"Border: {border}")
- # NEW FILE
- import math
- # 5 to the power of 8, two ways
- number1 = 5 ** 8
- number2 = math.pow(5, 8)
- print(number1)
- print(number2)
- # square root of 25 is 5
- number3 = 25
- root_value = math.sqrt(number3)
- print(root_value)
- # programmers often have to convert mathematical
- # equations into actual code, example:
- # diagonal of a cube = square root of 3 * side
- side = 15
- diagonal = math.sqrt(3) * side
- # NEW FILE
- import random
- # random number between 1 and 10
- guess = random.randint(1, 10)
- print(guess)
- print()
- # let's generate two random dice
- dice1 = random.randint(1, 6)
- dice2 = random.randint(1, 6)
- # print the dice values
- print(dice1)
- print(dice2)
- # UUSI TIEDOSTO
- from datetime import date
- # get current date, YEAR-MONTH-DAY
- today = date.today()
- print(f"Today is {today}")
- # NEW FILE
- from datetime import datetime
- today = datetime.now()
- print(today)
- # formatting logic for date:
- # %d = day, %m = month, %Y = year, %H = hour, %M = minutes, %S = seconds
- date_text = today.strftime("%d.%m.%Y %H:%M:%S")
- print(date_text)
- # NEW FILE
- from datetime import date, datetime, timedelta
- # how many days are left this year
- today = date.today()
- future = date(2022, 12, 31)
- delta = future - today
- days = delta.days
- print(f"Days left this year: {days}")
- # if we borrow a book today, and the return is after 14 days,
- # when is the actual return date?
- today = datetime.now()
- today = today + timedelta(14)
- print(today)
- # NEW FILE, BIGGER EXAMPLE
- from datetime import date
- import math
- # compound interest formula
- # future money = start money * math.pow(1 + interest rate, number of years)
- # start money and interest rate (decimal)
- start_money = 25000
- interest_rate = 0.07
- # how many years between two dates
- start_date = date.today()
- end_date = date(2032, 12, 31)
- delta = end_date - start_date
- days = delta.days
- years = days // 365
- # compound interest
- total_money = start_money * math.pow(1 + interest_rate, years)
- # actual money earnt while saving
- new_money = total_money - start_money
- new_money = round(new_money, 2)
- # all done, print out results
- print("With the given parameters, we earnt this much money:")
- print(f"{new_money} €")
- # ANOTHER EXAMPLE, HOW MUCH TIME IT TAKES TO HALVE THE AMOUNT OF COFFEE IN YOUR BODY
- import math
- from datetime import datetime
- start_time = datetime(2022, 9, 21, 13, 45, 0)
- end_time = datetime(2022, 9, 21, 23, 45, 0)
- duration = end_time - start_time
- seconds = duration.total_seconds()
- minutes = seconds / 60
- hours = minutes / 60
- hours = int(hours)
- # 300 ml
- cup = 300
- # coffee half-life 4 hours, found from Google
- half_life = 4
- # https://www.mathsisfun.com/algebra/exponential-growth.html
- # 9 hours => cup * exp ** (log(0.5) / half_life) * hours
- logarithm = math.log(0.5) / half_life
- coffee_left = cup * math.exp(logarithm * hours)
- coffee_left = round(coffee_left)
- # calculate the amount of coffee you have in your body, after a certain time
- print(f"Coffee left: {coffee_left} ml")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement