Python Note 1
Python Note 1
28/12/2023
A function (in this context) is a separate part of the computer code able to:
cause some effect (e.g., send text to the terminal, create a file, draw
an image, play a sound, etc.); this is something completely unheard of
in the world of mathematics;
evaluate a value (e.g., the square root of a value or the length of a
given text) and return it as the function's result; this is what makes
Python functions the relatives of mathematical concepts.
an effect;
a result.
The function name (print in this case) along with the parentheses and
argument(s), forms the function invocation.
First, Python checks if the name specified is legal (it browses its
internal data in order to find an existing function of the name; if this
search fails, Python aborts the code);
second, Python checks if the function's requirements for the number of
arguments allows you to invoke the function in this way (e.g., if a
specific function demands exactly two arguments, any invocation
delivering only one argument will be considered erroneous, and will
abort the code's execution);
third, Python leaves your code for a moment and jumps into the
function you want to invoke; of course, it takes your argument(s) too
and passes it/them to the function;
fourth, the function executes its code, causes the desired effect (if
any), evaluates the desired result(s) (if any) and finishes its task;
finally, Python returns to your code (to the place just after the
invocation) and resumes its execution.
You have already seen a computer program that contains one function
invocation. A function invocation is one of many possible kinds of
Python instructions.
Python makes one exception to this rule - it allows one instruction to spread
across more than one line (which may be helpful when your code contains
complex constructions).
the program invokes the print() function twice, and you can see
two separate lines in the console - this means that print() begins its
output from a new line each time it starts its execution; you can
change this behavior, but you can also use it to your advantage;
each print() invocation contains a different string, as its argument
and the console content reflects it - this means that the instructions
in the code are executed in the same order in which they have
been placed in the source file; no next instruction is executed until the
previous one is completed (there are some exceptions to this rule, but
you can ignore them for now)
We've changed the example a bit - we've added one empty print() function
invocation. We call it empty because we haven't delivered any arguments to
the function. As you can see, the empty print() invocation is not as empty
as you may have expected - it does output an empty line, or (this
interpretation is also correct) its output is just a newline.
newline characters
The backslash (\) has a very special meaning when used inside strings - this
is called the escape character. The word escape should be understood
specifically - it means that the series of characters in the string escapes for
the moment (a very short moment) to introduce a special inclusion. In other
words, the backslash doesn't mean anything in itself, but is only a kind of
announcement, that the next character after the backslash has a different
meaning too.
The letter n placed after the backslash comes from the word newline.
Both the backslash and the n form a special symbol named a newline
character, which urges the console to start a new output line.
1. If you want to put just one backslash inside a string, don't forget its
escaping nature - you have to double it, e.g., such an invocation will cause an
error:
2. Not all escape pairs (the backslash coupled with another character) mean
something
The print() function - using multiple
arguments
Look at the editor window. This is what we're going to test now:
The spaces, removed from the strings, have appeared again. Can you explain
why?
The way in which we are passing the arguments into the print() function is
the most common in Python, and is called the positional way (this name
comes from the fact that the meaning of the argument is dictated by its
position, e.g., the second argument will be outputted after the first, not the
other way round).
The print() function - the keyword
arguments
Python offers another mechanism for the passing of arguments, which can be
helpful when you want to convince the print() function to change its
behavior a bit.
The mechanism is called keyword arguments. The name stems from the
fact that the meaning of these arguments is taken not from its location
(position) but from the special word (keyword) used to identify them.
The print() function has two keyword arguments that you can use for your
purposes. The first of them is named end.
My-name-is-Monty-Python.
Key takeaways
1. The print() function is a built-in function. It prints/outputs a specified
message to the screen/console window.
4. Python strings are delimited with quotes, e.g., "I am a string" (double
quotes), or 'I am a string, too' (single quotes).
8. Keyword arguments are the ones whose meaning is not dictated by their
location, but by a special word (keyword) used to identify them.
9. The end and sep parameters can be used for formatting the output of
the print() function. The sep parameter specifies the separator between the
outputted arguments, e.g., print("H", "E", "L", "L", "O", sep="-") ,
whereas the end parameter specifies what to print at the end of the print
statement.