Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Python modules and timing | ||||||||
Line: 8 to 8 | ||||||||
| ||||||||
Added: | ||||||||
> > | ||||||||
import utime # import time would work as well for i in range(5): print("Hello World") utime.sleep(1) # delay execution be 1 s, utime.sleep_ms(1000) would do the same. sleep_ms is not available in CPythonThe other option is to import just the function to be used: | ||||||||
Added: | ||||||||
> > | from utime import sleep_ms for i in range(5): print("Hello World!") sleep_ms(1000) # note that utime in front of the sleep call has been omittedIn i similar way, you may use the functions of the math library: In order to calculate sin(30 degrees) you need access to the sin function. The sin function however takes its parameter in radians such that you need the function radians, converting values from degrees to radians, in addition. This is how you would go about: from math import sin,radians print(sin(radians(30))) Let's try these. | |||||||
-- ![]() |
Line: 1 to 1 | ||||||||
---|---|---|---|---|---|---|---|---|
Added: | ||||||||
> > |
Python modules and timingPython libraries are arranged as modules, containing a number of functions and/or classes. These modules must be imported before being used. You can either import the complete module, or you can import individual functions or classes to be used. As an example, let's study the time module. In MicrroPython the module is called utime (for micro-time) and it contains functions allowing us to delay execution for a certain amaount of time.
import utime # import time would work as well for i in range(5): print("Hello World") utime.sleep(1) # delay execution be 1 s, utime.sleep_ms(1000) would do the same. sleep_ms is not available in CPythonThe other option is to import just the function to be used: -- ![]() Comments |