Raspberry Pi uses python for programming advantages and programming details
The Raspberry Pi is a very inexpensive, palm-sized fully programmable computer. Although the Raspberry Pi is small, its potential is unlimited. You can create a very cool project on the Raspberry Pi just like you would with a regular desktop computer. For example, you can build your own home cloud storage server with a Raspberry Pi.
What language is the Raspberry Pi programming?
The Raspberry Pi uses python for programming. One of the core ideas of the Raspberry Pi project is the use of the Python programming language. Python allows Raspberry Pi owners to extend their projects to incredible scale.
Python is an interpreted, object-oriented, cross-platform programming language. Good reliability, clear syntax and ease of use make it one of the most popular programming languages. Python is an elegant, powerful language.
The Raspberry Pi provides a cheap and incredible development platform for Python programming. Python is considered a "teaching" language because it is easy to learn, but it does not mean that Python is weak.
With the Raspberry Pi and Python, the only thing that limits you is the imagination. You can write games in Python and run them on a Raspberry Pi controlled console. You can write programs to control the robots connected to the Raspberry Pi. Or you can send your Raspberry Pi to an altitude of 39000 kilometers to shoot incredible photos like Dave Akerman.
The advantages of the python language are as follows:
1. Simple: Python is a language that represents simple ideas. Reading a good Python program feels like reading English. It allows you to focus on solving problems rather than figuring out the language itself.
2, easy to learn: Python is extremely easy to get started, because Python has extremely simple documentation.
3, fast: Python's bottom layer is written in C language, many standard libraries and third-party libraries are also written in C, running very fast.
4, free, open source: Python is one of FLOSS (free / open source software). The user is free to publish a copy of the software, read its source code, make changes to it, and use part of it for new free software. FLOSS is based on the concept of sharing knowledge with a group.
5, high-level language: write programs in Python without having to consider the underlying details such as how to manage the memory used by your program.
6. Portability: Due to its open source nature, Python has been ported to many platforms (it has been modified to work on different platforms). These platforms include Linux, Windows, FreeBSD, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acom RISC OS, VxWorks, PlayStaTIon, Sharp Zaurus, Windows CE, PocketPC, Symbian, and Google's Android-based android platform.
7. Explanatory: A program written in a compiled language such as C or C++ can be converted from a source file (ie C or C++) to a language used by your computer (binary code, ie 0 and 1). This process is done by the compiler and with different tags and options.
When you run the program, the connect/redirector software copies your program from the hard disk to memory and runs. Programs written in the Python language do not need to be compiled into binary code. You can run the program directly from the source code.
Inside the computer, the Python interpreter converts the source code into an intermediate form called bytecode, which is then translated into the machine language used by the computer and runs. This makes using Python easier. It also makes Python programs easier to port.
8. Object Orientation: Python supports both process-oriented programming and object-oriented programming. In a "process-oriented" language, programs are built from procedures or functions that are just reusable code. In an "object-oriented" language, programs are built from objects that combine data and functionality.
9. Extensibility: If you need a piece of critical code to run faster or if you want some algorithms to be undisclosed, you can write some programs in C or C++ and then use them in a Python program.
10. Embeddability: Python can be embedded in C/C++ programs to provide scripting functionality to program users.
Rich libraries: The Python standard library is really huge. It can help with a variety of tasks, including regular expressions, document generation, unit testing, threads, databases, web browsers, CGI, FTP, email, XML, XML-RPC, HTML, WAV files, password systems, GUIs ( Graphical user interface), Tk and other system related operations. This is called Python's "full-featured" philosophy. In addition to the standard library, there are many other high-quality libraries, such as wxPython, Twisted, and Python image libraries.
11, the specification of the code: Python uses a forced indentation to make the code more readable. Programs written in the Python language do not need to be compiled into binary code.
Raspberry pie python programming detailed
When you create a Python program, you must first exit from the compiler's environment and open a text editor such as emacs or the Raspberry Pi's Leafpad. After creating the program, save its extension of ".py". After that, you can run the program by entering the following command:
In many programming languages, Python's syntax is also very different. Python uses spaces or indents to separate different blocks of code. Other languages, such as C, use curly braces to distinguish between different blocks of code, such as if statements; Python uses colons and indents to define a block of code.
The code format in C is as follows:
You may be paying attention to two details about Python programming. First, the role of parentheses in the if statement is not obvious. In Python, parentheses are not required, but in most cases, using parentheses is a good programming practice, because the addition of parentheses improves the readability of the code. You will also find that most other programming languages ​​end with a semicolon at the end of each line of code, which is not the case with Python. This may take some effort to adapt, but it can avoid the problem of compiling failure because the semicolon is misplaced somewhere or forgot to add a semicolon. In Python, the end of each line of code is the end of the statement—it's as simple as that.
You have seen the form of a statement, such as:
As mentioned before, there is no need to declare in advance that x is an integer variable in Python, and y is a character variable - Python can distinguish itself. These statements are called assignments, which assign the value to the right of the equal sign to the variable to the left of the equal sign. There are various naming conventions in different programming languages, but the best advice I can give you is to choose one of the rules and stick to it. If you like the Pascal language rules (ThisIsAVariable), then use this rule. Use this rule if you prefer the hump rule (thisIsAVariable). But be sure to be consistent, and you will thank you for your persistence in the future. In any case, regardless of whether the variable is a number, a character, a list, or something else, the assignment is simply to assign a value to a variable. This is the simplest of the programming functions.
1, if test
The programming function to be introduced next is the if statement, and its associated elif and else functions. As you might expect, if you perform a test, then choose a test-based result. The most basic if statement is as follows:
'1' has the same effect as "true" in Boolean variables, so the above statement will always output "true".
When you type an if statement in a Python terminal or IDLE and end with a colon, the next prompt is always an ellipsis (...), which means that Python is waiting for an indented block. If you have already done an indentation, press Enter to end the wait. If you are writing a program in a text editor, be sure to indent when you need to indent.
From here on, I will write the code in the format of a text editor and write the output as a format after running the script.
This is a more complicated program that uses elif and else, as follows:
Obviously, this code will eventually output "Spam is a wonderful thing!". When the program is executed, the computer first determines the first if, and if the statement being judged is correct, it immediately executes the code in the subsequent indented block. If it is not correct, skip the indented block to find the elif and judge the correctness of its statement. Similarly, if the elif statement is correct or not, the computer will execute the program in the indented block. If it is not correct, it will skip the indented block and look for the next elif or else statement.
There are three points to note here: First, if the contents of an if statement are wrong, the contents of the indented block will not be executed, and the computer will jump directly to the next unindented code. .
Second, like other languages, Python uses double equal signs to determine equality. The single equal sign is used for assignment, and the double equal sign is used for judgment. The reason I mention this is because each programmer (I definitely refer to every programmer) sometimes uses a single equal sign in the if statement to make judgments, so their program will get a lot of strange results. You will make the same mistake, but I hope to give you a shot in advance.
Third, Python ignores blank lines, spaces (except, of course, in the context of interactive and indented blocks) and comments. This is important because you can freely tag your code to improve their readability to other programmers, even if you read your code yourself later.
In Python, comments usually start with "#" and the program ignores everything after #.
The readability of the code is a very important factor, I hope you can recall this sentence regularly. You want to try to debug the previously written code or program it as follows:
Although it's not fun, you can clearly understand the content of the second writing method. After reading the similar hundreds of lines without spaces, blank lines or comments, your eyes will thank you for believing me. Let's see what happens to the second last line after using a space:
You can use spaces as you like.
Regarding the if part, the last thing I want to say is about the Boolean operator. In a test that judges right or wrong, X and Y correctly mean that both X and Y are correct. X or Y correct means X or Y is correct, and not X means X is wrong. Boolean operations are used in Python using keywords instead of &&, ||, in C or C++! Operator. Learn these operators well and they will be very handy.
2, cycle
Usually, the program is executed once from start to finish. However, some specific statements may cause the order of program execution to jump from one point to another. These control-flow statements include if(then) statements and loops.
The simplest loop statement might be a piece of code that executes many times, for example:
You can also use a for loop to traverse a string, or a list:
Although the syntax of the for loop in Python is somewhat different from that in C or Java, once you get used to it, using this syntax is handy.
The second loop statement is a while statement. This statement determines a state and continues to execute the program in the indent box as long as the state is correct, for example:
It may be different from what you think. This code will never output "10" because the x output will not be incremented. During the 10th cycle, the compiler increments to 10 after the output "9". At this point, the while condition is no longer true, so the code in the indented box will not be executed.
The while statement is useful if you are waiting for a specific event to occur, such as a button press or a user pressing "Q" to exit. Let us look at the following example:
There are two points worth noting in this code: First, in the Python 2.x version, the raw_input command is used to get an input from the user, while in Python 3.x, the command is changed to a simple input; Second, remember to use the break command, this command will jump out of the current loop. So in this case, the part of the loop in the while will loop forever, but when the test var == 'q' returns true, the program will exit the current loop and end the program.
3, function
Functions allow programmers to write code that is reused. It can greatly improve work efficiency. Often, if you find that some features in your code need to be executed many times, this feature is likely to need to be rewritten as a function.
Suppose you have written a simple program to calculate the area and perimeter of a rectangle. The user enters the height and width of the rectangle, after which the program performs the corresponding calculations. The easiest way to do this is to write a function with parameters whose parameters are the height and width of the rectangle. The function then returns the area and perimeter of the rectangle to the main program. To implement this function, we write with the def assignment statement. The def assignment statement is a method we define a function with the syntax def function name (parameter 1, parameter 2):
This little program requires you to provide some parameters and return the result of the calculation. Probably this is not the best example (you can calculate the result with less code), but it is a good illustration of the idea of ​​code reuse. With this function, you understand that at any point in the program, as long as you need to calculate the area or perimeter, call the AreaPerimeter function and assign the parameters "height" and "width".
One thing to note here is that the raw_input function returns a string, even if you type a number, the value returned is a string type. This explains why the height and width variables must be int converted before the calculation in the AreaPerimeter function.
If you are familiar with other languages, you will find that Python functions differ from methods in other languages ​​in terms of methods, functions, and steps. For example, in Python, all functions are called by call-by-reference. There is no need for too specialized terminology. In simple terms, this means that when you pass a parameter to a function, you simply pass a pointer to a variable instead of passing a value. This approach makes Python's memory management more convenient. For example, when you pass list parameters over and over in a function, you don't need to copy the contents of the entire list. Specifically, when a function takes a list as a parameter, you pass only the position of the first element of the list in memory, and then the function finds the remaining items based on the position of the first element.
Another interesting aspect of the function is that they are all executable statements. This means that a function can actually be declared and called in an if statement. Although not very common, such definitions and calls are legal (and sometimes very useful). Def statements can be nested inside a loop, nested in other def statements, or even nested in lists and dictionaries.
We'll review the function parts as we work on specific projects; now, just need to know about their existence and know that they are useful for every program you write yourself.
4, object and object-oriented programming
The last important thing in this chapter is its ability to execute object-oriented code. Object-oriented programming (OOP) is a more advanced topic and may not be covered by this book. But I think this is a very important topic. Don't understate it and pass it by.
OOP is an example of program data being divided into objects and functions (or methods). An object is a data structure, usually a combination of a set of data types, including integers, characters, or other data types. Objects are usually part of a class, associated with methods in a class, and manipulated by methods.
Perhaps the easiest way to explain this part is to use the shape example. In this example, a shape is a class of an object. There are values ​​in the class, such as name and numberOfSides. This class also has related functions such as findArea (calculated area) or findPerimeter (calculation perimeter).
The shape class has many subclasses, and the content of the subclass description is more specific. A square is a shape object whose shapeType value is equal to square and the numberOfSides value is equal to 4. Its findArea function accepts the numberOfSides value and takes the square of the value as the return value. At the same time, a triangle object also has different name, shapeType, and numberOfSides values ​​and different findArea methods.
This example not only briefly introduces the concept of objects, but also describes the concept of inheritance - an integral part of OOP. The triangle object inherits the name, numberOfSides, and findArea parts from its parent class shape class (although these parts have different values ​​or implementation methods). If an object inherits from the shape class, it also inherits those parts. Even if it doesn't need those parts, it will still contain these parts. It may add some other parts (for example, a circle object may have a radius value), but it will also contain those parts of its parent class.
If you use these classes in programming, Python is easier to understand than C++ or Java. Whether the property is an object or a method, you can name it according to the next syntax structure: object.attribute. If you have a circle object called holyGrail, its radius value is represented by holyGrail.radius. A square called unexplodedScotman whose function of calculating the area is defined by unexplodedScotsman.findArea.
As mentioned earlier, the content of OOP is beyond the scope of this book. But concepts like functions are very useful, especially in very long and complex programs. As you learn Python, feel free to explore. You'll find that Python is also a feature-rich language that even allows you to perform other advanced program tasks.
Sfx Power Supply,Sfx 250W Power Supply,Sfx Pc 150W Power Supply,Psu 150W 200W Power Supply
Boluo Xurong Electronics Co., Ltd. , https://www.greenleaf-pc.com