An Introduction to Python Programming - Part 01 - Basic Outputs

in #coding7 years ago

Part 01 - Getting Outputs from your Program

logo.png

This is the second part of my Introductory Python Programming Tutorial. The first was Part 00 which can be found here and explains how to install Python.

All computer programs and systems boil down to a very simple model, basically there has to be some sort of input, whether that's from a user controlled device such as a keyboard or mouse, an automated device such as a sensor, or even an external device of some kind that sends a signal to our system.

This then is either processed itself or causes some sort of processing to take place. Finally, once a result is arrived at this then needs to be communicated back to the user or other computer systems by outputting some sort of value.

Basically, we have Input --> Process --> Output there may be other things which occur too, in terms of storing things, but every computer system and every computer program essentially boils down to a version of this model. So the first thing we are going to do is make Python tell us a story, in other words, we're going to get it to output some text and other things.

So we are going to use this to make the program output some text in the Python shell, initially, we are going to start with the simplest possible program which just outputs some text like we did in the previous section. Start up the Python shell like we did in Part 00 (select Idle, Python(Idle), Python(GUI) or something similar from your equivalent to the Start Menu). This will bring up the interactive Python Shell:

idle.png The Python Shell

Click on the File Menu in the Shell window and select the entry that reads either New File or New Window (it varies depending on your version of Python). This will open a new empty editor window, which is where we are going to write our code.

editor.png Blank Editor Window

Type the following Python code on the first line of the file:

print("Hello world!")

editor_with_code.png Editor window with line of code

If we run this it will just output the text which is between the two quote marks ". In technical terms, this is a string, which is a piece of text that we can output, manipulate or pass to some other part of the program so that it can do stuff to it. We can run the code either by clicking on the Run Menu and then choosing the bottom entry Run Module or by hitting the F5 key. Either way, you will be asked if you want to save your source code, you do so choose yes, give it a name such as hello (if you are on an older version of Python you may need to include the .py at the end as this didn't get added automatically in earlier versions). When you finish saving it, you should see the output appear in the Shell window, something like this:

shell_with_output.png Shell window with output

We can change the code so that it will output other text, by altering what lies between the quote marks. Do that now and rerun the code to make sure it works successfully (you will be asked if you want to save as you go, choose yes each time). Whenever you change your code, you must rerun it to see the changes take effect.

We can make the code output over multiple lines in a number of ways. Firstly we can add a print function for each line of text we want to output. The last verse of the poem "The Masque of Anarchy" by Shelley reads:

Rise, like lions after slumber
In unvanquishable number!
Shake your chains to earth like dew
Which in sleep had fallen on you:
Ye are many-they are few!

We can turn this into a program in a number of ways. Firstly as stated we can use multiple print functions:

print("Rise, like lions after slumber")
print("In unvanquishable number!")
print("Shake your chains to earth like dew")
print("Which in sleep had fallen on you:")
print("Ye are many-they are few!")

Each print automatically outputs a newline character after the text has been displayed, making each string display on a new line. This is behaviour we can modify if we so choose (more on this in a later section).

The second way in which we can display the poem on multiple lines is to put triple quotes around the whole verse, and use a single print function. This will output whatever is inside the string, using any newline characters that make up the string.

print("""Rise, like lions after slumber
In unvanquishable number!
Shake your chains to earth like dew
Which in sleep had fallen on you:
Ye are many-they are few!""")

Adding extra line breaks inside the string, is then reflected in the output from the print function. Try putting the cursor at the end of one of the lines of poetry and hitting enter, then rerun the code.

We can also get the same effect using a single print function, by including all of the lines of text in a single string and adding in the newline character ourselves (newline is a backslash followed by an n - \n).

print("Rise, like lions after slumber\nIn unvanquishable number!\nShake your chains to earth like dew\nWhich in sleep had fallen on you:\nYe are many-they are few!")

In the next part we'll look at making more complex use of the Python print function. Meanwhile have a play with print and see what you can do.


My name is Dave Ames. I've been a teacher for 25 years and for the last few of those I've been teaching both children and other adults, especially teachers to program in a variety of environments, but mostly Python.

I'm intending these to be an ongoing series of Introductory Python Programming Tutorials, that are small enough to be easily digestible.

Sort:  

Great, simple post about a very useful language. Better than I would have written. Can I resteem these posts as you do them? I ask because I am strongly in the homesteading movement, but believe that basic programming skills are essential. When I resteem them, theyll go out to a binch of homesteaders who may be inspired to add a little tech into their lives.

Of course, the more the merrier.

I've done various bits with Physical Computing, basically using Python on a Raspberry Pi to control sensors and other stuff like simple motors. I might add some tutorials on that sort of stuff as it's a simple step from doing something like that, to having a sensor monitor the height of water in a tank, and turn on a pump when the level drops below a certain height.

I'm not 100% sure what you mean by Homesteaders but it sounds like those sorts of skills might be useful to them (unless the name is in no way related to what you guys do).

The name is about all those folks who do self sufficiency kind of stuff. Im interested in Raspberry Pi and Arduino programming to add sendors and controls to my little urban farm so any tutorials you post willbe met with gratitude lol.

That's kind of what I was imagining it meant. I'll see what I can come up with in terms of Pi and Arduino stuff.

Thatll be great. We need lots of stuff on sensors so that people can relate computers to their needs at a practical level

The Raspberry Pi GPIO library for Python (the code that lets you talk to the pins) was written by a guy called Ben Croston to control his MicroBrewery all of which is run on Raspberry Pis.

It's called The Fuzzy Duck Brewery: http://www.fuzzyduckbrewery.co.uk

Congratulations @amos1969! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got a First Reply
Award for the number of upvotes
Award for the number of comments
Award for the number of upvotes received
Award for the number of comments received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

It Looks like you know what your talking about. :)

The @OriginalWorks bot has determined this post by @amos1969 to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

Your Post Has Been Featured on @Resteemable!
Feature any Steemit post using resteemit.com!
How It Works:
1. Take Any Steemit URL
2. Erase https://
3. Type re
Get Featured Instantly – Featured Posts are voted every 2.4hrs
Join the Curation Team Here

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62817.54
ETH 2573.14
USDT 1.00
SBD 2.74