Warning

This page is located in archive.

Since in the course, we will be trying to program the Raspberry Pi Pico in MicroPython, it is necessary to familiarize ourselves with at least the basics of the Python programming language.

A variable is a fundamental element of programming abstraction that provides a symbolic name for a location in memory occupied by binary data. The meaning of binary data is then represented in the program through a data type. Data type can describe binary data as mode complex entities, like integer of float numbers, strings, arrays, dictionaries, etc.

a = 10 # integer b = 3.14 # float c = "hello" # string d = [1, 2, 3] # list e = {"id": 10} # dictionary

print(a) print("a =", a) print("a =", a, "b =", b) print("a = {}".format(a)) print("a = {}, b = {}".format(a, b))

Loops can use two commands, **for** or **while**.

# loop using command for and function range for i in range(5, 10, 2): print(i)

Function **range(begin, end, step)** generally returns sequence of numbers from **begin** to **end-1** with **step**. If **begin** is not given, then its value is **0**, default **step** is **1**. In following example are ranges recasted to lists.

print(list(range(3))) print(list(range(3, 5))) print(list(range(3, 10, 3)))

Loops using commnad **for** can be also combined with lists.

values = ['one', 'two', 'three'] for i in values: print(i)

# loop using command while a = 5 while a > 1: print(a) a = a - 1 # note: infinite loop, common in embedded # while True: # # command

# branch a = 10 if a > 5: print("a is bigger than 5") else: print("it is not bigger")

# branches and loops can be combined for i in range(10): if i % 2: print(i)

Some functionalities can be distributed to **modules**. For example module **math** contains math functions.

# import of whole library import math print("sin(0.22) = {:.4f}, and cos(0.22) = {:.4f}".format(math.sin(0.22), math.cos(0.22)))

# import library with acronyme import math as m print("sin(0.22) = {:.4f}, and cos(0.22) = {:.4f}".format(m.sin(0.22), m.cos(0.22)))

# import just necessary functions from math import sin, cos print("sin(0.22) = {:.4f}, and cos(0.22) = {:.4f}".format(sin(0.22), cos(0.22)))

courses/be2m37mam/labs/00.txt · Last modified: 2023/12/14 11:27 by viteks