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!\nAnother line!\n\n")
- # empty print() also adds a single \n by default
- # sometimes \n is not enough, and you also need to add \r (carriage return)
- 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("Tax:\t\t120 €")
- print("Postage: \t 20 €")
- # NEW FILE
- # recap on how to get data from users (input)
- # and how to combine them into a calculation
- # THIS IS THE TYPICAL STRUCTURE OF MOST OF THE EXERCISES
- # PHASE 1: get user input values
- # ask the salary from user and convert
- # the value from text to decimal (float)
- salary = input("Write your salary:\n")
- salary = float(salary)
- # ask also the savings
- savings = input("How much savings do you have?\n")
- savings = float(savings)
- # helper variable for increase percentage, +15%
- increase = 1.15
- # it's okay also to combine input + float (or int) on the same line
- # savings = float(input("How much savings do you have?\n"))
- # PHASE 2 - the actual calculation logic of the code
- # this part usually starts to grow longer and more complex
- # as we go further in the course
- total = (salary + savings) * increase
- # PHASE 3 - print out the results for the user
- # use f-string to combine text and the number (total)
- print(f"Total money: {total} €")
- # NEW FILE
- import math
- # make sure you don't have a file called
- # math.py in your project anywhere (confuses the import
- # otherwise
- # try to print the pi-value from math
- # don't write your own pi values with less decimals like pi = 3.14
- # always use math.pi in your code
- print(math.pi)
- # let's try some math
- radius = 13
- # circumference of a circle, 2 * pi * radius
- border = 2 * math.pi * radius
- border = round(border, 2)
- # only using round(border, 2) doesn't work
- # because you need to save the result into a variable
- # print the result
- print(f"Circumference: {border} cm")
- # NEW FILE
- import math
- # 3 to the power of 5
- number1 = math.pow(3, 5)
- print(number1)
- # Python also support power itself
- number2 = 3 ** 5
- print(number2)
- # don't do manual powering, like this:
- # side = 10
- # total = side * side * side
- # square root
- number3 = 25
- root_value = math.sqrt(number3)
- print(root_value)
- # for example, diagonal of a cube => d = sqrt 3 * side
- side = 14
- diagonal = math.sqrt(3) * side
- print(diagonal)
- # NEW FILE
- import random
- # do not create random.py anywhere in the project
- # just like math -module, it will confuse the import
- # generate a number between 1 and 10
- guess = random.randint(1, 10)
- print(guess)
- # let's generate two random dice, basic dice 1-6
- # a tip: you can duplicate a code file by selecting it
- # and pressing Ctrl + D on Windows, and CMD + D on MacOS
- dice1 = random.randint(1, 6)
- dice2 = random.randint(1, 6)
- print(dice1)
- print(dice2)
- # NEW FILE
- from datetime import date
- # get the current date
- today = date.today()
- print(f"Today is: {today}")
- # NEW FILE
- from datetime import datetime
- # if we used the normal import, like:
- # import datetime
- # we would have to use it like this:
- # today = datetime.datetime.now()
- # get current time and date
- today = datetime.now()
- print(today)
- # if we want to format the timestamp into a prettier format for the user
- # we an use the strftime() -function
- # the format we want: day.month.year hours:minutes:seconds
- # %d = day, %m = month, %Y = year, %H = hour, %M = minute, %S = second
- # if you want to remove 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
- # two timestamps
- first = date(2024, 9, 10)
- second = date(2024, 12, 31)
- # calculate the difference
- delta = second - first
- days = delta.days
- # print the result
- print(f"Days left this year: {days}")
- # example, if we create a bill for a customer today
- # what is the due date in 2 weeks (14 days)
- today = datetime.now()
- today = today + timedelta(14)
- date_text = today.strftime("%d.%m.%Y")
- print(date_text)
- # NEW FILE, tips for the first advanced task
- # replace this with input() etc.
- cents = 87
- # the task is to find and divide the amount of
- # 50 cents, 20 cents, 10 cents, 5 cents, 2 cents and 1 cents
- # with the CURRENT cents, find out how many full 50 cent coins we can have
- coins50 = cents // 50
- # update cents, what is left over from previous calculation
- cents = cents % 50
- # now with the 20 cents
- coins20 = cents // 20
- # update the current cents
- cents = cents % 20
- print(f"50 cent coins: {coins50}")
- print(f"20 cent coins: {coins20}")
- print(f"Cents left at the moment: {cents}")
- # and so on continue alternating between the division
- # and the remainder until we reach 1 cent
- # NEW FILE - an example where we combine different modules
- # Compound interest, math experiment
- # https://www.freshbooks.com/wp-content/uploads/2024/07/Calculating-Compound-Interest.png
- # compound interest, or (interest of interest) is a concept of return of investement
- # where interest starts to accumulate interest on itself as well
- from datetime import date
- import math
- # variables for the compound interest calculation
- start_money = 25000
- interest_rate = 7
- # 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 / 100) ^ years
- total_money = start_money * math.pow(1 + interest_rate / 100, 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 - another example of combining modules
- # original half-life formula from here:
- # https://mathsisfun.com/algebra/exponential-growth.html
- import math
- from datetime import datetime
- # when we drank the last coffee
- start_time = datetime(2024, 9, 10, 9, 0, 0)
- end_time = datetime(2024, 9, 10, 20, 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
- # do the calculations
- # 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 out the results
- 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