Tutorial 1 - Python - / variables, functions /


Exercise 1 - (types)


Do the following script.


test1 = 10

print ("Type of test1: " + str(type(test1)) )

test2 = 10.0

print ("Type of test2: " + str(type(test2)) )


print ("*" * test1)


#####


a, b, c, result = 3, 5, 7, 0


print ("Type of a: " + str(type(a)) )

print ("Type of b: " + str(type(b)) )

print ("Type of c: " + str(type(c)) )

print ("Type of result: " + str(type(result)) )


result = a-b/c


print ("*" * test1)

print("result: "+ str(result) )

print ("Type of result: " + str(type(result)) )



Can you explain the different types obtained ?



Exercise 2 - (arithmetic operations)

Considering a variable containing a number of seconds (put a high value for the example).

Write a program that convert theses seconds into the equivalent in years, months, days, minutes and seconds.

Tips : Use operator « / » to make divisions. And operator « % » (modulo) to get the remainder of a division (x=4%3 gives « 1 »).


s = 50000000           # input number of seconds

y = int ( s / (365*3600*24) )      # corresponding number of years

s = s % ( ...)             # leaving seconds after subtracting the previous "years equivalent"

m = …                         # same for leavings months (m), days (d), minutes (mins), seconds (secs)

...

print ("The input amount of seconds corresponds to :" )

print (str(y) + " years " + str(m) + " months " + str(d) + " days " + str(mins) + " minutes " + str(secs) + " seconds")




Exercise 3 - (function)

Define a surfCircle(R) function. This function must return the surface (the area) of a circle with radius R has been provided as an argument. For example, executing the instruction :

print (surfCircle(2.5))

must give the result 19.635

Note: to get the pi value : (1) use « pi = 3.14 » ; or (2) add « from math import * » at the top of your script, that provide automatically the value of “pi” variable with a high accuracy