In today's lab, we will be getting familiar with Python and build a web server using Flask.
Declaring and using variables in Python
Printing statements with both numerical and string values
Adding comments
Declaring and calling functions that take arguments and return values
Creating new directories and subdirectories
Cloning files using commands
Writing boilerplate code and routes
Running apps on the Raspberry Pi
An open source programming language that runs on all major platforms
Has tons of packages you can add onto your projects
Commonly used for:
Data Science
Backend for web applications
Game development
Hardware (like the Raspberry Pi!)
To launch the shell editor for Python in the terminal, use the command:
$ python3
Then, you should see:
pi@raspberrypi:~ $ python3
Python 3.5.3 (default, Jul 9 2020, 13:00:10)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Unlike other programming languages, there is no keyword in Python to declare variables. Instead, a variable is created when you assign a value to it.
myAge = 16
The value within the variable can be reassigned using the assignment (=) operator and concatenated with other variables using the (+) operator.
In Python, functions are decclared using the keyword def followed by the function name, any parameters, and a colon (:). See example below:
def say_hello(name):
return "Hello " + name
It is important to note the indentation. Any code that belongs within a function needs to be indented for Python to recognize it as being a part of that function.
There are a multitude of modules that can be imported and used in a Python script. To import a module, simply use the keyword import followed by the module name:
import calendar
Then, you should be able to use all of the functions that are included in the module:
>>> import calendar
>>> cal = calendar.month(2021, 5)
>>> print(cal)
May 2021
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
>>>
In Python, libraries are a collection of modules. Similar to importing modules, libraries are imported using the from keyword and the import keyword. See example below:
from math import pi
def get_area_of_circle(r):
area = r*r* pi
return area
A web framework that provides programmers with tools, libraries, and technologies to build web applications
Written using Python
We will use it to build our servers on the backend that will communicate with our HTML/CSS/JS on our frontend