Tutorial 1 - Python - / variables, functions /


Exercise 1 - (types)

Assign the respective values 3, 5, 7.0 to three variables a, b, c. Do the operation a-b/c, and display the types of each of these variables :


a, b, c = 3, 5, 7.0

result = a-b/c

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)) )


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 = 1000000           # input number of seconds

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

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

…                         # 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