How to Upload Code to Raspberry Pi

In this post, I'll requite y'all a quick overview of what a Python program is, what Python programs can exist used for, and how to write and run a unproblematic Python programme on the Raspberry Pi.

PCBWay Ad

What is a Python Program?

Python is a very useful programming language that has an easy to read syntax, and allows programmers to use fewer lines of code than would exist possible in languages such as assembly, C, or Coffee.

The Python programming language actually started as a scripting language for Linux. Python programs are similar to crush scripts in that the files contain a series of commands that the computer executes from elevation to bottom.

Compare a "hello earth" program written in C to the same program written in Python:

Hello World Program in Python vs C Programming

Dissimilar C programs, Python programs don't demand to exist compiled before running them. Notwithstanding, you lot will need to install the Python interpreter on your computer to run them. The Python interpreter is a plan that reads Python files and executes the lawmaking.

Information technology is possible to run Python programs without the Python interpreter installed though. Programs similar Py2exe or Pyinstaller will package your Python code into stand-alone executable programs.

What Tin can a Python Program Do?

Like shell scripts, Python can automate tasks similar batch renaming and moving large amounts of files. It tin can be used just like a command line with IDLE, Python's REPL (read, eval, print, loop) role. However, there are more useful things you can practise with Python. For example, you tin can use Python to program things similar:

  • Web applications
  • Desktop applications and utilities
  • Special GUIs
  • Small databases
  • 2D games

Python also has a large collection of libraries, which speeds up the development process. There are libraries for everything you can retrieve of – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific calculating.

Many (merely not all) of the things you can do in C can be washed in Python. Python is mostly slower at computations than C, but its ease of utilize makes Python an platonic language for prototyping programs and designing applications that aren't computationally intensive.

How to Write and Run a Programme in Python

We'll only comprehend the nuts of writing and executing a Python programme here, but a great tutorial covering everything a programmer needs to know virtually Python is the volumeLearning Python 5th Ed. (O'Reilly) by Mark Lutz.

JLPCB Ad

Installing and Updating Python

Python 2 and Python iii come up pre-installed on Raspbian operating systems, merely to install Python on another Linux Os or to update it, simply run one of these commands at the command prompt:

sudo apt-become install python3

Installs or updates Python 3.

sudo apt-get install python

Installs or updates Python 2.

Opening the Python REPL

To access the Python REPL (where you tin can enter Python commands just like the control line) enter python  or python3  depending on which version you want to employ:

Python and python3 REPL on the Raspberry Pi

Enter Ctrl-D to exit the REPL.

Writing a Python Program

To demonstrate creating and executing a Python program, we'll brand a simple "hi world" program. To brainstorm, open the Nano text editor and create a new file named hi-globe.py by entering this at the command prompt:

sudo nano hello-world.py

Enter this lawmaking into Nano, and then press Ctrl-Ten and Y to exit and relieve the file:

            #!/usr/bin/python  impress "Hi, World!";          

All Python program files volition need to be saved with a ".py" extension. Y'all can write the program in any text editor such as Notepad or Notepad++, only be certain to save the file with a ".py" extension.

Running a Python Plan

To run the program without making it executable, navigate to the location where you saved your file, and enter this at the control prompt:

python how-do-you-do-earth.py

Make a Python File Executable

Making a Python programme executable allows y'all to run the program without inbound python earlier the file name. You lot tin can brand a file executable past entering this at the command prompt:

chmod +ten file-name.py

Now to run the program, all you demand to enter is:

./file-name.py

Hither are some additional resources that will help you make the well-nigh out of programming in Python:

  • Complete list of Python syntax
  • The Python Package Alphabetize (PyPi)
  • Installing Python packages on the Raspberry Pi

Hopefully you found this post useful. If y'all accept whatsoever questions, feel gratuitous to exit a comment beneath. If you lot know anyone else that would relish this article, please share it! You tin also go updates when ever nosotros postal service a new article by subscribing!

gunterretul1992.blogspot.com

Source: https://www.circuitbasics.com/how-to-write-and-run-a-python-program-on-the-raspberry-pi/

0 Response to "How to Upload Code to Raspberry Pi"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel