Thursday, February 29, 2024

 

A little bit of math for Python


So far we have introduced the computer to two types of numbers:
float: numbers with a decimal
int:    whole numbers

Copy this code and have a go with trying the different mathematical symbols.

adding = 4 + 3
print(adding)

subtraction = 8 - 9
print(subtraction)

multiplication = 4 * 32
print(multiplication)

division = 50 / 5
print(division)

# a number raised to the power of some number
# in this example we raise 5 to the power of 2
squared = 5**2
print(squared)

# remainder of a division
modulo = 15 % 4
print(modulo)

# whole number division
divisor = 15 // 2
print(divisor)

Lets get into coding again :

How many seconds are in a year?

tSeconds = int(input("1 min in Sec=:"))
tMinutes = int(input("1 hour in Minutes=:"))
tHours = int(input("1 Day in Hours=:" ))
#tDays = int(input("1 year in Days:"))
tYear = int(input("which year it is"))
# lets check for the leap year
if tYear % 4 == 0:
tDays = 366
Totalsecs = tDays*tHours*tMinutes*tSeconds
print("total seconds in year =",Totalsecs)
else:
tDays = 365
Totalsecs = tDays*tHours*tMinutes*tSeconds
print("total seconds in year =",Totalsecs)

No comments:

Post a Comment

  2D Dictionaries Remember that dictionaries are very similar to lists, except that they store data as key:value pairs. The value is what it...