Output
Output
1 name=“Fred Flintstone”
2 print(“Hello”,name)
3 print()
4 print(“I hope you are well”)
5
print(“Goodbye”)
Output
Learning Objective
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
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
Commas
Brackets
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.
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”)
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