Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # math-, random- and datetime modules today!
- print("Welcome!")
- # NEW FILE
- # adding new lines within text
- print("Some text here.\n\nAnother line!")
- # an empty print() also prints a single \n by default
- print()
- # tabs can be used to align pieces of text horizontally
- # remember to have enough \t to match long words
- print("Total:\t\t350 €")
- print("Postage:\t40 €")
- # NEW FILE
- # this kind of a structure is quite clear
- # for the exercise sets etc.
- # PART 1: ask all needed values from the user
- # ask the values from the user
- salary = input("Give your salary:\n")
- salary = float(salary)
- savings = input("How much savings do you have?\n")
- savings = float(savings)
- # PART 2 - the actual calculation logic of the code
- # combine salary and savings, add 25%
- total = (salary + savings) * 1.25
- # PART 3 - print out the results
- print(f"Total money: {total} €")
- # NEW FILE
- import math
- # avoid hardcoded Pi-values (like 3.14)
- # always use math.pi
- print(math.pi)
- print()
- # radius = 13
- radius = input("Give a radius:\n")
- radius = int(radius)
- # circumference of the circle
- # 2 * pi * radius
- border = 2 * math.pi * radius
- # round the border to two decimals
- # remember to save to variable
- # only round(border, 2) does not work in code
- border = round(border, 2)
- print(f"Circumference: {border} m")
- # NEW FILE
- import math
- # 3 to the power of 5
- number1 = math.pow(3, 5)
- print(number1)
- # ... or Python
- number2 = 3 ** 5
- print(number2)
- number3 = 25
- root_value = math.sqrt(number3)
- print(root_value)
- # length of a body diagonal of a cube = sqrt 3 * side
- side = 14
- diagonal = math.sqrt(3) * side
- print(diagonal)
- # NEW FILE
- import random
- # generate a random number between 1 and 10
- guess = random.randint(1, 10)
- print(guess)
- print()
- # let's generate two random dice
- # basic dice 1-6
- dice1 = random.randint(1, 6)
- dice2 = random.randint(1, 6)
- print(dice1)
- print(dice2)
- # NEW FILE
- from datetime import datetime
- # if we used import datetime, our code would need to be
- # today = datetime.datetime.now()
- today = datetime.now()
- # print(today)
- # formatting logic for date
- # %d = day, %m = month, %Y = year, %H = hour, %M = minutes, %S = second
- # removing the extra zeroes in days and months:
- # in Windows: %#d and %#m
- # in Unix / Linux / MacOS %-d and %-m
- date_text = today.strftime("%d.%m.%Y %H:%M:%S")
- print(date_text)
- # NEW FILE
- from datetime import date, datetime, timedelta
- first = date(2023, 9, 25)
- second = date(2023, 12, 31)
- delta = second - first
- days = delta.days
- # end result
- print(f"Days left for this year: {days}")
- # if we create a bill today, what is the due date in 2 weeks?
- # e.g. = 14 days
- today = datetime.now()
- today = today + timedelta(14)
- date_text = today.strftime("%d.%m.%Y")
- print(date_text)
- # NEW FILE
- # careful with float-numbers, with small values, you might get weird decimals
- # use either round() or Decimal-module to fix this
- number1 = 0.1
- number2 = 0.2
- print(number1 + number2)
- # NEW FILE
- from datetime import date
- import math
- # variables for the compound interest calculation
- start_money = 25000
- interest_rate = 0.07
- # calculate the amount of days and years in this timespan
- start_date = date.today()
- end_date = date(2033, 12, 31)
- # for how many years are we going to save money?
- delta = end_date - start_date
- days = delta.days
- years = days // 365
- # compound interest
- # final amount of money = start money * (1 + interest_rate) ^ years
- total_money = start_money * math.pow(1 + interest_rate, years)
- new_money = total_money - start_money
- new_money = round(new_money, 2)
- print(f"With given parameters, we earned this much money {new_money} €")
- # NEW FILE
- # https://mathsisfun.com/algebra/exponential-growth.html
- import math
- from datetime import datetime
- # when we drank the last coffee
- start_time = datetime(2023, 9 , 25, 9, 0, 0)
- end_time = datetime(2023, 9, 25, 22, 0, 0)
- # how many hours ago?
- duration = end_time - start_time
- seconds = duration.total_seconds()
- minutes = seconds / 60
- hours = minutes // 60
- hours = int(hours)
- # let's assume half-life of coffee is 5 hours
- half_life = 5
- # let's assume our coffee cup is 300ml
- cup = 300
- # half-life = cup * exp ^ (ln(0.5)/half_life)*hours
- logarithm = math.log(0.5) / half_life
- coffee_left = cup * math.exp(logarithm * hours)
- coffee_left = int(coffee_left)
- print(f"From the original {cup} ml of coffee I drank {hours} hours ago.")
- print(f"{coffee_left} ml of coffee is still left in my body.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement