• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Training
  • Shop
  • Our Latest eBook Offer
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
  • Pluralsight Courses
You are here: Home / Linux / Certification / LPI Linux Essentials / An Introduction to Python on the Raspberry Pi

An Introduction to Python on the Raspberry Pi

May 17, 2014 by The Urban Penguin

LinuxEssentialsBorderSMALLFor this edition of LPI Linux Essentials for the Raspberry Pi we take the time to introduce ourselves to Python programming. When writing code in Python the idea is to stick to the 20 Zen rules of Python, the 20th is never spoken. To display this rules from a Python prompt you can type:

import this

We can start a Python prompt from the command line of the Raspberry Pi by entering the command:

python3

Note: Running just python we start python version 2.7 not version 3.

The interactive Python shell is known as the REPL:

  • Read
  • Evaluate
  • Print
  • Loop

We can run simple arithmetic operations:

8 * 3

And so on. For more complex mats we can import a Python module:

import math
math.sqrt(144)

The result of the evaluation will be printed, I think it is 12.

Python uses significant white space to help define code blocks rather than braces that are often used in other languages. The lack of braces should aid readability by forcing the need to indent code. The standard is to use 4 spaces for an indent level. We see this in the video with a simple for loop:

for name in 'bill', 'jo', 'jack':
    print("=====")
    print(name)
print("We are out of the loop")

The end of the for statement and the start of the code block is identified by the : . Each line of code that makes up part on the loop is then indented 4 spaces. The final line is not indented and so is not part of the loop.

If we wanted to print the name in upper case (upper) or proper case (capitalize) we can use methods from the string. The variable name once created is immutable, ot cannot be changed. When we convert it to upper case we create anew string, this means that we can use the original string if we need;

for name in 'bill', 'jo', 'jack':
    print("=====")
    print(name.upper())
    print(name.capitalize())
    print(name)
print("We are out of the loop")

To leave the REPL interactive shell on the Pi use the keys Control + D

 

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: LPI Linux Essentials, Raspberry Pi

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Live Training

Powered by Eventbrite

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • Scheduled Linux Essentials Classes
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Courses

© 2019 The Urban Penguin · All Rights Reserved

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok