Raspberry PI Programming Guide
Posted: 07 Oct 2023, 04:21
To learn programming, it's best to start by trying to modify some simple programs and gradually understand how they work. All the programs you'll need for this book are available for download, so you can work on them even without programming experience. However, it's useful to have an idea of how things are structured.
Hello, World
Typically, the first program you write in a new language simply displays the words "Hello, world" on the screen. To get acquainted with it, open the nano text editor and enter the following command:
$ nano hello.py
The ".py" extension indicates that the file contains a Python program. Next, enter the following text into the nano editor and save the file:
print('Hello, World')
Then run the program:
$ python hello.py
Hello, World
Indentation and Whitespace
Programmers are accustomed to formatting code in a specific way to make it more readable. In the C language for Arduino, code blocks inside functions or statements are written with indentation to show which code belongs to which function or command. In Python, this formatting style isn't just a matter of preference; it's a requirement.
In Python, there are no curly braces { or } to indicate the beginning and end of a code block. Instead, indentation is used to show which lines of code are grouped together.
Consider this code example:
while True:
GPIO.output(control_pin, False)
time.sleep(5)
GPIO.output(control_pin, True)
time.sleep(2)
print("Complete")
This is a `while` loop (similar in structure to a loop in the C language for Arduino, as described in the "Arduino Programming Guide" forum). In this case, the condition is always `True`, so the loop runs indefinitely. At the end of the first line, there is a colon (:), indicating that a code block follows. In Python, the colon is analogous to { in C for Arduino, except that there's no closing symbol.
Lines within a code block should be indented relative to the previous line. As long as you stick to the formatting, the specific number of spaces between lines of code doesn't matter. Most programmers use four spaces for each level of indentation.
At the end of a code block, no special symbol is used; you simply stop indenting. Notice that in the provided code example, the last `print` command is outside the loop. Since the `while` loop is infinite, the last line will never be executed.
Variables
Variables in Python are similar to variables in C for Arduino but differ in that you don't need to specify whether a variable is of type `int`, `float`, or any other type the first time you use it. You simply assign a value to the variable, and nothing (except common sense) stops you from assigning an integer value to a variable one moment and a string value the next. For example, the following code is valid but not recommended:
a = 123.45
a = "message"
When working with strings in Python, you can use either double quotes (as in the example) or single quotes.
Hello, World
Typically, the first program you write in a new language simply displays the words "Hello, world" on the screen. To get acquainted with it, open the nano text editor and enter the following command:
$ nano hello.py
The ".py" extension indicates that the file contains a Python program. Next, enter the following text into the nano editor and save the file:
print('Hello, World')
Then run the program:
$ python hello.py
Hello, World
Indentation and Whitespace
Programmers are accustomed to formatting code in a specific way to make it more readable. In the C language for Arduino, code blocks inside functions or statements are written with indentation to show which code belongs to which function or command. In Python, this formatting style isn't just a matter of preference; it's a requirement.
In Python, there are no curly braces { or } to indicate the beginning and end of a code block. Instead, indentation is used to show which lines of code are grouped together.
Consider this code example:
while True:
GPIO.output(control_pin, False)
time.sleep(5)
GPIO.output(control_pin, True)
time.sleep(2)
print("Complete")
This is a `while` loop (similar in structure to a loop in the C language for Arduino, as described in the "Arduino Programming Guide" forum). In this case, the condition is always `True`, so the loop runs indefinitely. At the end of the first line, there is a colon (:), indicating that a code block follows. In Python, the colon is analogous to { in C for Arduino, except that there's no closing symbol.
Lines within a code block should be indented relative to the previous line. As long as you stick to the formatting, the specific number of spaces between lines of code doesn't matter. Most programmers use four spaces for each level of indentation.
At the end of a code block, no special symbol is used; you simply stop indenting. Notice that in the provided code example, the last `print` command is outside the loop. Since the `while` loop is infinite, the last line will never be executed.
Variables
Variables in Python are similar to variables in C for Arduino but differ in that you don't need to specify whether a variable is of type `int`, `float`, or any other type the first time you use it. You simply assign a value to the variable, and nothing (except common sense) stops you from assigning an integer value to a variable one moment and a string value the next. For example, the following code is valid but not recommended:
a = 123.45
a = "message"
When working with strings in Python, you can use either double quotes (as in the example) or single quotes.