<Programming_Tasks.
py>
Introduction to Python
Programming
Part 3: Using Comments
>>>
Robbot Resources
How to use this resource...
Skill Explanations: Pink Slides
• Within each unit, you will learn how to develop and apply a range of Python
programming skills. Skill explanations are in pink.
Programming Tasks: Rookie Pro Beast
• After reading the explanation of a skill, apply it within the given tasks. The
tasks are categorised into Rookie (Easy), Pro (Medium) or Beast (Hard).
Programming Challenges: Rookie Pro Beast
• Once you have learned how to apply your new skills, demonstrate your ability
by completing the given challenges. Don’t forget the bonus debugging task!
• Once complete, review your performance!
Programming skills you will learn
Rookie
• To understand why adding comments to your code is useful.
• To be able to add comments within your python code.
Pro
• To write comments which help to explain what programming
techniques you have used.
• To hide code lines with comments, e.g. test print( ) statements.
Beast
• To write clear and precise comments which means that
someone else can understand your programming techniques.
What are comments?
In Python, comments are used to explain and clarify the code,
making it more readable and maintainable.
Programmers add comments within their code for the following
reasons:
• It helps explain the programming techniques written and how
they work.
• If a programmer wants to reuse their code in the future, using
comments will help identify the required piece of code.
• They provide explanations for other people who may wish to use
the code or learn the same programming technique.
New Skill 1: Adding comments in Python
#
• To add a comment, simply type in a hashtag symbol # followed by the
required information.
• You write it like this:
#This is a comment. We use them to help other programmers using
our code to explain what we have wrote. For example...
#The code below outputs the phrase ‘Hello World’.
print(“Hello World”)
As a rule, always add comments above the code you are explaining.
Task 1: Rookie
x
<Partial_Solution.py>
1
a) Store the string phrase ‘Hello World’ within a 2
suitable variable. 3
4 #A variable was used, which
b) Output the variable.
c) Add comments to your code to explain what you
5 then is written in print
6
have done. 7
function, and the output
8 will be "Hello world"l
t
cha r 9 x = “Hello World”
w
Flo 10
print(x)
n
pla 11
12
13
14
15
16
17
18
19
20
21 Link your learning! Connect your prior
22 knowledge with the flowchart solution
23 provided!
<Task_1_Student_Evidence.py>
x
1 Add a print screen to show your written code here.
2
3
4
5
6
7
8
9
10
11
12
x
<Task_1_Output.py>
>>> Add a print screen to show the output of your code here.
New Skill 2: Comment out code
• Another useful technique you could use comment for is to stop a line of code from
running.
• For example, if you only want to use a print command to test if a variable is storing
the correct information, you can comment it out and use it later:
name = input( “Please enter your name: ”)
#print(name)
print(“Hello ” + name + “! It’s lovely to meet you!”)
• As you can see, this piece of code is only used to check that data is being stored
in the name variable. It can be uncommented when you need to go back and
test it.
Task 2: Pro
x
<Partial_Solution.py>
1
Comment out the code lines below 2
3 b = “Hello”
that output the variables b and c so 4
only variable x is outputted. 5
6
print(b)
7 c = “World”
8
9 print(c)
b = “Hello” 10
11
print(b) x = b + “ ” + c
12
13
c = “World” 14 print(x)
15
print(c) 16
17
x = b + “ ” + c 18
19
print(x) 20
21
22
23
<Task_2_Student_Evidence.py>
x
1 Add a print screen to show your written code here.
2
3
4
5
6
7
8
9
10
11
12
x
<Task_Output.py>
>>> Add a print screen to show the output of your code here.
Review of skills learned
Rookie
• To understand why adding comments to your code is useful.
• To be able to add comments within your python code.
Pro
• To write comments which help to explain what programming
techniques you have used.
• To hide code lines with comments, e.g. test print( ) statements.
Beast
• To write clear and precise comments which means that
someone else can understand your programming techniques.