0% found this document useful (0 votes)
10 views32 pages

Output

The document provides an overview of basic Python programming concepts, including the use of the print function, debugging, and writing simple programs. It includes instructions for using the IDLE IDE, modifying code, and understanding common syntax errors. Additionally, it encourages users to practice coding by predicting outputs and creating their own programs.

Uploaded by

vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Output

The document provides an overview of basic Python programming concepts, including the use of the print function, debugging, and writing simple programs. It includes instructions for using the IDLE IDE, modifying code, and understanding common syntax errors. Additionally, it encourages users to practice coding by predicting outputs and creating their own programs.

Uploaded by

vandana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

In pairs.

predict what the following code is doing

1 name=“Fred Flintstone”
2 print(“Hello”,name)
3 print()
4 print(“I hope you are well”)
5
print(“Goodbye”)
Output
Learning Objective

Predict the output of a Python program


Know how to use the print function to produce output
Know how to debug a program
Python programming

We will use Python 3.


Python is a high-level textual programming language.
If you have used Scratch you will already be familiar with some of the
concepts presented here such as selection (if else) and iteration
(loops) albeit in block form.
To write our programs we will use the IDLE interactive development
environment (IDE).
Demonstration (click on the image)
Interactive Development Environment

An IDE is an app that helps us to write, edit, debug and run computer
codes, and have the following among other things:
Editor – Allows you to write and edit programs
Error diagnostics – Tells you where there are syntax and run time
errors are in your program
Run time environment – Can track and execute instructions step-by-
step.
Translator – Converts the high-level language into machine code that
computers understand
IDE interactive shell
Open up IDLE. You should see the interactive shell similar to below.
Configuring IDLE: Add line numbers

It is really useful to configure IDLE to show the line numbers.


Open up the editor and go to Options | Show Line Numbers
Configuring IDLE: Colour coding
You can modify the color coding for
different programming constructs to
help with editing and debugging
code
Go to Options | Configure IDLE

Select Highlights
Writing your first program

1) In the Python shell go to File | New Window 2) In the new window write:
print (“Hello World”)
3) Go to File | Save and name the program firstProgram.py
4) To run the program Press the F5 key. Alternatively go to
Run Module| Python Shell. The output should appear on the
interactive shell.

Congratulations! You have now written your first program using Python
Output

Output is data that has been processed by a computer and then sent
to other devices.
In Python output is generated by using the print function.
 In this case information is sent from the computer to the monitor
The output is displayed in the interactive shell
Print function

print is a built-in function that can take up to 255 arguments.


An argument is a value that is provided as input to a function.
Each argument is separated by a comma.
Arguments are contained in brackets
The following statement has 3 arguments:
The arguments are in speechmarks because they are string data types

print(“Harry Potter”, “and”, “Ron Weasley”)


Anatomy of a function
Name of
function
(identifier) Argument 1 Argument 2 Argument 3
print(“Harry Potter”, “and”, “Ron Weasley”)

Commas

Brackets

identifier(argument1, argument2, argument3)


Sequencing

We can add as many print operations


to our program as we like. The
statements are run in sequence one-by-
one starting at the top of the program.
These are the simplest types of
programs where one instruction is run print (“As”)
at a time in order from top to bottom. print (“cool”)
print (“as”)
print (“a”)
print (“cucumber”)
Copy the code into the Python IDLE editor and run

1 name=“Fred Flintstone”
2 print(“Hello”,name)
3 print()
4 print(“I hope you are well”)
Does the code do as you expected?
5
print(“Goodbye”) You can download a copy of the
code from:

1-output.py
Debugging checklist
 Have you put in the closing brackets?
 Have you put in speech marks?
 Have you spelt everything correctly?
 Are your commas in the right place?
Commenting code
Comments are lines in you code that the program does not execute. Commenting code is very
helpful because it helps you and others to understand the program. In Python a commented
line begins with a hash #

# This is a comment.
# The program will ignore these lines.

At the top of every program that you write it is good practice to write your name, the date and a
short description of what the program does at the top of the file. For example: you might write.

# A program that outputs “Hello World”


# Author: WG
# Date: Friday 6th April 2021
Investigate the code
1 name=“Fred Flintstone”
2 print(“Hello”,name)
3 print()
4 print(“I hope you are well”)
5
print(“Goodbye”)

Add comments into the code when you answer each of the questions
1. What line is a variable assigned
2. What is output on line 2?
3. What is happening on line 3?
4. What happens when you remove the bracket )at the end of line 4
5. In what order are the statements run?
Investigate the code

# This is an assignment
# The variable is called name
name=“Fred Flintstone”
# prints out Hello Fred Flintstone
print(“Hello”,name)
# prints out a blank line
print()
print(“I hope you are well”)
print(“Goodbye”)
Modify the code
1 name=“Fred Flintstone”
2 print(“Hello”,name)
3 print()
4 print(“I hope you are well”)
5
print(“Goodbye”)

1. Line 1: Change “Fred Flintstone” to “Homer Simpson”


2. Swap lines 2 and 5. What happens?
3. Line 1 and Line 2: Change the name variable to character
4. Line 2: Change greeting from “Hello” to “Bonjour”
5. Line 4: Add in another blank line
6. Change “Goodbye” to “Adios”
Debugging

If you program does not work it is highly likely that you have made a syntax
error. Syntax errors are typographical mistakes that the programmer has made.
When the code gets translated for the computer to understand you will get an
error message. Sometimes the message is useful, sometimes it is not!

When you are writing programs especially as they get more complex it is very
difficult not to make syntax errors, so it is worth learning to understand the
error messages so you can fix them more easily. As you get more experienced
you will learn to interpret the error messages more easily.
Common syntax errors (1)

Forgotten bracket. You get the message “invalid syntax” in a popup box
along with the line after the line with the error being highlighted in red.
Common syntax errors (2)
Forgotten speech mark. You get the message “EOL while scanning
string literal” in a pop up box along with the line of the error being
highlighted in red.
Common syntax errors (3)
Misspell function name. We have
misspelled “print”. In the shell we
get a message telling us where and
what the problem is.
Find the 5 errors in the following code
print("Invictus")
print()
print("Out of the night that covers me,")
print("Black as the Pit from pole to pole")
print("I thank whatever gods may be")
print("For my unconquerable soul.") Download the broken code from:
print()
print("In the fell clutch of circumstance"
1-poem-invictus.py
print("I have not winced nor cried aloud.")
print "Under the bludgeonings of chance")
PRint("My head is bloody, but unbowed.")
print()
print("Beyond this place of wrath and tears)
print("Looms but the Horror of the shade,")
print("And yet the menace of the years")
print("Finds, and shall find, me unafraid.")
print()
print("It matters not how strait the gate,")
prnt("How charged with punishments the scroll.")
print("I am the master of my fate:")
print("I am the captain of my soul.")
print()
print("William Ernest Henley")
Find the 5 errors in the following code
print("Invictus")
print()
print("Out of the night that covers me,")
print("Black as the Pit from pole to pole")
print("I thank whatever gods may be")
print("For my unconquerable soul.") Download the broken code from:
print()
print("In the fell clutch of circumstance"
1-poem-invictus.py
print("I have not winced nor cried aloud.")
print "Under the bludgeonings of chance")
PRint("My head is bloody, but unbowed.")
print()
print("Beyond this place of wrath and tears)
print("Looms but the Horror of the shade,")
print("And yet the menace of the years")
print("Finds, and shall find, me unafraid.")
print()
print("It matters not how strait the gate,")
prnt("How charged with punishments the scroll.")
print("I am the master of my fate:")
print("I am the captain of my soul.")
print()
print("William Ernest Henley")
Make

Output the following sentence: “The cat sat on a mat” with one word
on each line using a sequence of print statements.

print(“The”)
print(“cat”)
print(“sat”)
print(“on”)
print(“a”)
print(“mat”)
Make: Write a program that outputs the following poem

Dreams
Make sure you include the blank
Hold fast to dreams
lines.
For if dreams die
Life is a broken-winged bird
That cannot fly. To help you, you can download the
Hold fast to dreams words from:
For when dreams go
Life is a barren field 1-poem-dreams.py
Frozen with snow

Langston Hughes
Make: Have fun creating ASCII Art
Write a program that outputs the following house shape

Extension: Create you own ASCII Art


Plenary
Correct the mistakes in the following Python code:
correct-the-mistakes.docx

You might also like