banner



How To Download Python On Raspberry Pi

In this post, I'll give you a quick overview of what a Python programme is, what Python programs can exist used for, and how to write and run a simple Python program 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 be possible in languages such every bit assembly, C, or Java.

The Python programming language really started equally a scripting linguistic communication for Linux. Python programs are similar to shell scripts in that the files contain a series of commands that the computer executes from top to bottom.

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

Hello World Program in Python vs C Programming

Different C programs, Python programs don't demand to exist compiled before running them. However, you will demand to install the Python interpreter on your computer to run them. The Python interpreter is a program that reads Python files and executes the code.

It is possible to run Python programs without the Python interpreter installed though. Programs like Py2exe or Pyinstaller will package your Python code into stand-lonely executable programs.

What Tin a Python Program Do?

Like trounce scripts, Python can automate tasks similar batch renaming and moving big amounts of files. It tin be used but like a command line with IDLE, Python's REPL (read, eval, impress, loop) function. However, there are more useful things you lot can do with Python. For instance, you can use Python to program things like:

  • Web applications
  • Desktop applications and utilities
  • Special GUIs
  • Small databases
  • second games

Python as well has a big drove of libraries, which speeds up the development process. There are libraries for everything you tin can think of – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific computing.

Many (but non all) of the things you tin practise in C can exist done in Python. Python is generally slower at computations than C, just its ease of use makes Python an ideal language for prototyping programs and designing applications that aren't computationally intensive.

How to Write and Run a Program in Python

We'll but comprehend the basics of writing and executing a Python program here, but a not bad tutorial covering everything a programmer needs to know about Python is the volumeLearning Python 5th Ed. (O'Reilly) by Mark Lutz.

JLPCB Ad

Installing and Updating Python

Python two and Python 3 come pre-installed on Raspbian operating systems, but to install Python on another Linux OS or to update information technology, just run one of these commands at the control prompt:

sudo apt-become install python3

Installs or updates Python 3.

sudo apt-go install python

Installs or updates Python 2.

Opening the Python REPL

To access the Python REPL (where you can enter Python commands merely like the command line) enter python  or python3  depending on which version you want to use:

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 programme, we'll make a uncomplicated "hello world" plan. To begin, open the Nano text editor and create a new file named hello-world.py by entering this at the command prompt:

sudo nano howdy-world.py

Enter this code into Nano, so press Ctrl-X and Y to exit and save the file:

            #!/usr/bin/python  print "Howdy, World!";          

All Python program files will need to be saved with a ".py" extension. Y'all tin write the plan in any text editor such as Notepad or Notepad++, just be sure 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 command prompt:

python hello-globe.py

Make a Python File Executable

Making a Python program executable allows you to run the program without inbound python earlier the file proper noun. Y'all can make a file executable by inbound this at the command prompt:

chmod +10 file-name.py

Now to run the program, all you lot need to enter is:

./file-name.py

Here are some additional resources that will help you make the nearly out of programming in Python:

  • Complete listing of Python syntax
  • The Python Parcel Index (PyPi)
  • Installing Python packages on the Raspberry Pi

Hopefully you found this post useful. If you have any questions, feel costless to leave a annotate below. If you know anyone else that would enjoy this article, please share it! Yous can also get updates when ever nosotros post a new commodity past subscribing!

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

Posted by: mortonbeile1955.blogspot.com

0 Response to "How To Download Python On Raspberry Pi"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel