Chapter 2. Starting with Python Strings, Files, and Menus

In this chapter, we will cover the following topics:

  • Working with text and strings
  • Using files and handling errors
  • Creating a boot-up menu
  • Creating a self-defining menu

Introduction

In this chapter, we discuss how to use Python to perform some basic encryption by scrambling letters. This will introduce some basic string manipulation, user input, progressing on to creating reusable modules, and graphical user interfaces.

To follow, we will create some useful Python scripts that can be added to run as the Raspberry Pi boots or an easy-to-run command that will provide quick shortcuts to common or frequently-used commands. Taking this further, we will make use of threading to run multiple tasks and introduce classes to define multiple objects.

As it is customary to start any programming exercise with a Hello World example, we will kick off with that now.

Create the hellopi.py file using nano, as follows:

nano -c hellopi.py

Within our hellopi.py file, add the following code:

#!/usr/bin/python3
#hellopi.py
print ("Hello Raspberry Pi")

When done, save and exit (Ctrl + X, Y, and Enter). To run the file, use the following command:

python3 hellopi.py

Congratulations, you have created your first program!

Your result should be similar to the following screenshot:

Introduction

The Hello Raspberry Pi output

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.175.180