Thonny Programming

broken image


Named after Monty Python (and not the snake), the Python Programming Language has become a popular choice among software developers since its initial launch in 1991. Python was intended for developing applications for desktops, laptops and servers, that have a powerful processor and run an OS.

Python

Thonny is a Python Editor ith a beginner-friendly environment with features suitable for writing codes and running programs! If you're running on Raspbian, simply head to the Pi icon - Programming - Thonny Python IDE as follows: Thonny. Once Thonny is launched, click File Open, and then navigate to your Python program. This single stepping through the program is a very powerful way of understanding how a programming language works. For example, when stepping through an expression, Thonny shows you all of the intermediate operations and displays the current value of the expression and variables. Now you can see there is a bug in your program. The answer should be 4! Next, you'll use Thonny's debugger to find the bug. Now that we have a bug in our program, this is a great chance to use Thonny's debugging features: Click the bug icon at the top of the window. This enters debugger mode. Thonny comes with Python 3.7 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners.

Python is a wonderful and powerful programming language that's easy to use (easy to read and write) and, with Raspberry Pi, lets you connect your project to the real world.

Python syntax is very clean, with an emphasis on readability, and uses standard English keywords.

Thonny

The easiest introduction to Python is through Thonny, a Python3 development environment. Open Thonny from the Desktop or applications menu:

Thonny gives you a REPL (Read-Evaluate-Print-Loop), which is a prompt you can enter Python commands into. Because it's a REPL, you even get the output of commands printed to the screen without using print. In the Thonny application, this is called the Shell window.

All the best Sunflower Line Drawing 40+ collected on this page. Feel free to explore, study and enjoy paintings with PaintingValley.com. Sunflower one line drawing. Find sunflower line drawing stock images in HD and millions of other royalty-free stock photos, illustrations and vectors in the Shutterstock collection. Thousands of new, high.

You can use variables if you need to but you can even use it like a calculator. For example:

Thonny also has syntax highlighting built in and some support for autocompletion. You can look back on the history of the commands you've entered in the REPL with Alt + P (previous) and Alt + N (next).

Basic Python usage

Hello world in Python:

Simple as that!

Indentation

Some languages use curly braces { and } to wrap around lines of code which belong together, and leave it to the writer to indent these lines to appear visually nested. However, Python does not use curly braces but instead requires indentation for nesting. For example a for loop in Python:

The indentation is necessary here. A second line indented would be a part of the loop, and a second line not indented would be outside of the loop. For example:

would print:

whereas the following:

would print:

Variables

To save a value to a variable, assign it like so:

Note that data types were not specified with these variables, as types are inferred, and can be changed later.

This time I used comments beside the increment command.

Comments

Comments are ignored in the program but there for you to leave notes, and are denoted by the hash # symbol. Multi-line comments use triple quotes like so:

Thonny

Lists

Python also has lists (called arrays in some languages) which are collections of data of any type:

Lists are denoted by the use of square brackets [] and each item is separated by a comma.

Iteration

Thonny

Some data types are iterable, which means you can loop over the values they contain. For example a list:

This takes each item in the list numbers and prints out the item:

Note I used the word number to denote each item. This is merely the word I chose for this - it's recommended you choose descriptive words for variables - using plurals for lists, and singular for each item makes sense. It makes it easier to understand when reading.

Other data types are iterable, for example the string:

This loops over each character and prints them out:

Range

Lirac wine map. The integer data type is not iterable and trying to iterate over it will produce an error. For example:

will produce:

However you can make an iterable object using the range function:

range(5) contains the numbers 0, 1, 2, 3 and 4 (five numbers in total). To get the numbers 1 to 5 (inclusive) use range(1, 6).

Length

You can use functions like len to find the length of a string or a list:

If statements

You can use if statements for control flow:

Python files in Thonny

To create a Python file in Thonny, click File > New and you'll be given a window. This is an empty file, not a Python prompt. You write a Python file in this window, save it, then run it and you'll see the output in the other window.

For example, in the new window, type:

Then save this file (File > Save or Ctrl + S) and run (Run > Run Module or hit F5) and you'll see the output in your original Python window.

Executing Python files from the command line

You can write a Python file in a standard editor, and run it as a Python script from the command line. Just navigate to the directory the file is saved in (use cd and ls for guidance) and run with python3, e.g. python3 hello.py.

Other ways of using Python

Command Line

The standard built-in Python shell is accessed by typing python3 in the terminal.

Python Programming Examples

This shell is a prompt ready for Python commands to be entered. You can use this in the same way as Thonny, but it does not have syntax highlighting or autocompletion. You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys. Use Ctrl + D to exit.

IPython

IPython is an interactive Python shell with syntax highlighting, autocompletion, pretty printing, built-in documentation, and more. IPython is not installed by default. Install with:

Then run with ipython from the command line. It works like the standard python3, but has more features. Try typing len? and hitting Enter. You're shown information including the docstring for the len function:

Try the following dictionary comprehension:

This will pretty print the following:

In the standard Python shell, this would have printed on one line:

You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys like in python. The history also persists to the next session, so you can exit ipython and return (or switch between v2/3) and the history remains. Use Ctrl + D to exit.

Installing Python libraries

apt

Python Programming Interview Questions

Some Python packages can be found in the Raspberry Pi OS archives, and can be installed using apt, for example:

This is a preferable method of installing, as it means that the modules you install can be kept up to date easily with the usual sudo apt update and sudo apt full-upgrade commands.

pip

Not all Python packages are available in the Raspberry Pi OS archives, and those that are can sometimes be out of date. If you can't find a suitable version in the Raspberry Pi OS archives, you can install packages from the Python Package Index (known as PyPI).

To do so, install pip:

Python Programming For Beginners

Python

Thonny is a Python Editor ith a beginner-friendly environment with features suitable for writing codes and running programs! If you're running on Raspbian, simply head to the Pi icon - Programming - Thonny Python IDE as follows: Thonny. Once Thonny is launched, click File Open, and then navigate to your Python program. This single stepping through the program is a very powerful way of understanding how a programming language works. For example, when stepping through an expression, Thonny shows you all of the intermediate operations and displays the current value of the expression and variables. Now you can see there is a bug in your program. The answer should be 4! Next, you'll use Thonny's debugger to find the bug. Now that we have a bug in our program, this is a great chance to use Thonny's debugging features: Click the bug icon at the top of the window. This enters debugger mode. Thonny comes with Python 3.7 built in, so just one simple installer is needed and you're ready to learn programming. (You can also use a separate Python installation, if necessary.) The initial user interface is stripped of all features that may distract beginners.

Python is a wonderful and powerful programming language that's easy to use (easy to read and write) and, with Raspberry Pi, lets you connect your project to the real world.

Python syntax is very clean, with an emphasis on readability, and uses standard English keywords.

Thonny

The easiest introduction to Python is through Thonny, a Python3 development environment. Open Thonny from the Desktop or applications menu:

Thonny gives you a REPL (Read-Evaluate-Print-Loop), which is a prompt you can enter Python commands into. Because it's a REPL, you even get the output of commands printed to the screen without using print. In the Thonny application, this is called the Shell window.

All the best Sunflower Line Drawing 40+ collected on this page. Feel free to explore, study and enjoy paintings with PaintingValley.com. Sunflower one line drawing. Find sunflower line drawing stock images in HD and millions of other royalty-free stock photos, illustrations and vectors in the Shutterstock collection. Thousands of new, high.

You can use variables if you need to but you can even use it like a calculator. For example:

Thonny also has syntax highlighting built in and some support for autocompletion. You can look back on the history of the commands you've entered in the REPL with Alt + P (previous) and Alt + N (next).

Basic Python usage

Hello world in Python:

Simple as that!

Indentation

Some languages use curly braces { and } to wrap around lines of code which belong together, and leave it to the writer to indent these lines to appear visually nested. However, Python does not use curly braces but instead requires indentation for nesting. For example a for loop in Python:

The indentation is necessary here. A second line indented would be a part of the loop, and a second line not indented would be outside of the loop. For example:

would print:

whereas the following:

would print:

Variables

To save a value to a variable, assign it like so:

Note that data types were not specified with these variables, as types are inferred, and can be changed later.

This time I used comments beside the increment command.

Comments

Comments are ignored in the program but there for you to leave notes, and are denoted by the hash # symbol. Multi-line comments use triple quotes like so:

Lists

Python also has lists (called arrays in some languages) which are collections of data of any type:

Lists are denoted by the use of square brackets [] and each item is separated by a comma.

Iteration

Some data types are iterable, which means you can loop over the values they contain. For example a list:

This takes each item in the list numbers and prints out the item:

Note I used the word number to denote each item. This is merely the word I chose for this - it's recommended you choose descriptive words for variables - using plurals for lists, and singular for each item makes sense. It makes it easier to understand when reading.

Other data types are iterable, for example the string:

This loops over each character and prints them out:

Range

Lirac wine map. The integer data type is not iterable and trying to iterate over it will produce an error. For example:

will produce:

However you can make an iterable object using the range function:

range(5) contains the numbers 0, 1, 2, 3 and 4 (five numbers in total). To get the numbers 1 to 5 (inclusive) use range(1, 6).

Length

You can use functions like len to find the length of a string or a list:

If statements

You can use if statements for control flow:

Python files in Thonny

To create a Python file in Thonny, click File > New and you'll be given a window. This is an empty file, not a Python prompt. You write a Python file in this window, save it, then run it and you'll see the output in the other window.

For example, in the new window, type:

Then save this file (File > Save or Ctrl + S) and run (Run > Run Module or hit F5) and you'll see the output in your original Python window.

Executing Python files from the command line

You can write a Python file in a standard editor, and run it as a Python script from the command line. Just navigate to the directory the file is saved in (use cd and ls for guidance) and run with python3, e.g. python3 hello.py.

Other ways of using Python

Command Line

The standard built-in Python shell is accessed by typing python3 in the terminal.

Python Programming Examples

This shell is a prompt ready for Python commands to be entered. You can use this in the same way as Thonny, but it does not have syntax highlighting or autocompletion. You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys. Use Ctrl + D to exit.

IPython

IPython is an interactive Python shell with syntax highlighting, autocompletion, pretty printing, built-in documentation, and more. IPython is not installed by default. Install with:

Then run with ipython from the command line. It works like the standard python3, but has more features. Try typing len? and hitting Enter. You're shown information including the docstring for the len function:

Try the following dictionary comprehension:

This will pretty print the following:

In the standard Python shell, this would have printed on one line:

You can look back on the history of the commands you've entered in the REPL by using the Up/Down keys like in python. The history also persists to the next session, so you can exit ipython and return (or switch between v2/3) and the history remains. Use Ctrl + D to exit.

Installing Python libraries

apt

Python Programming Interview Questions

Some Python packages can be found in the Raspberry Pi OS archives, and can be installed using apt, for example:

This is a preferable method of installing, as it means that the modules you install can be kept up to date easily with the usual sudo apt update and sudo apt full-upgrade commands.

pip

Not all Python packages are available in the Raspberry Pi OS archives, and those that are can sometimes be out of date. If you can't find a suitable version in the Raspberry Pi OS archives, you can install packages from the Python Package Index (known as PyPI).

To do so, install pip:

Python Programming For Beginners

Then install Python packages (e.g. simplejson) with pip3:

Read more on installing software in Python here.

piwheels

The official Python Package Index (PyPI) hosts files uploaded by package maintainers. Some packages require compilation (compiling C/C++ or similar code) in order to install them, which can be a time-consuming task, particlarly on the single-core Raspberry Pi 1 or Pi Zero.

piwheels is a service providing pre-compiled packages (called Python wheels) ready for use on the Raspberry Pi. Raspberry Pi OS is pre-configured to use piwheels for pip. Read more about the piwheels project at www.piwheels.org.

More





broken image