Game Development Reference
In-Depth Information
line number 1.
Comments
1. # This program says hello and asks for my name.
This line is called a
comment
. Any text following a
#
sign (called the
pound sign
) is
a comment. Comments are not for the computer, but for you, the programmer. The
computer ignores them. They're used to remind you of what the program does or to tell
others who might look at your code what it is that your code is trying to do.
Programmers usually put a comment at the top of their code to give their program a title.
The IDLE program displays comments in red to help them stand out.
Functions
A
function
is kind of like a mini-program inside your program. It contains lines of code
that are executed from top to bottom. Python provides some built-in functions that we can
use. The great thing about functions is that we only need to know what the function does,
but not how it does it. (You need to know that the
print()
function displays text on the
screen, but you don't need to know how it does this.)
A
function call
is a piece of code that tells our program to run the code inside a
function. For example, your program can call the
print()
function whenever you want to
display a string on the screen. The
print()
function takes the string you type in between
the parentheses as input and displays the text on the screen. Because we want to display
Hello world!
on the screen, we type the
print
function name, followed by an
opening parenthesis, followed by the
'Hello world!'
string and a closing parenthesis.
The
print()
Function
2. print('Hello world!')
3. print('What is your name?')
This line is a call to the
print
function, usually written as
print()
(with the string to
be printed going inside the parentheses).
We add parentheses to the end of function names to make it clear that we're referring to a
function named
print()
, not a variable named
print
. The parentheses at the end of the
function let us know we are talking about a function, much like the quotes around the
number
'42'
tell us that we are talking about the string
'42'
and not the integer
42
.
Line 3 is another
print()
function call. This time, the program displays "What is your
name?"