Difference: ModulesAndTiming (2 vs. 3)

Revision 32022-10-15 - UliRaich

Line: 1 to 1
 
META TOPICPARENT name="MinimalLectures"

Python modules and timing

Line: 27 to 27
 This is how you would go about:
from math import sin,radians
print(sin(radians(30)))
Added:
>
>

Writing your own functions

 
Changed:
<
<

Let's try these.


>
>
It is easily possible to write your own functions and to create your own modules.

Imagine you want to create a sin function which takes angles in degrees, instead of radians. Let's call the function sin_deg(degrees). It will take its parameter degrees, convert the value to radians and return the sin result, which was calculated with the sin function from the math library. You can define a function with the keyword def. The function body is again indented, like we saw in case of the conditions or the loops:

from math import sin,radians
def sin_deg(degrees):
   rad = radians(degrees)
   return sin(rad)

We can call this function as follows:

angle = 30              # use 30 degrees as parameter value
result = sin_deg(angle) # calculates the sin of "angle" degrees using our own sin_deg function
print("sin(",angle,") = ",result)

Let's try these ExerciseSheets#SecondSession
Thereafter we will learn how to access hardware in Hardware Access GPIO

  -- Uli Raich - 2022-10-15
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback