Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Note 1

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Note 1

Personal Python Learning Note

Uploaded by

brian07151992
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Note 1

28/12/2023

The print() function

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.

Where do the functions come from?


 They may come from Python itself; the print function is one of this
kind; such a function is an added value received together with Python
and its environment (it is built-in); you don't have to do anything
special (e.g., ask anyone for anything) if you want to make use of it;
 they may come from one or more of Python's add-ons
named modules; some of the modules come with Python, others may
require separate installation - whatever the case, they all need to be
explicitly connected with your code (we'll show you how to do that
soon);
 you can write them yourself, placing as many functions as you want
and need inside your program to make it simpler, clearer and more
elegant.

As we said before, a function may have:

 an effect;
 a result.

There's also a third, very important, function component - the argument(s).

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.

The print() function - instructions

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's syntax is quite specific in this area. Unlike most programming


languages, Python requires that there cannot be more than one
instruction in a line.

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).

This is a good opportunity to make some observations:

 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.

The print() function - the escape and

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:

print("The itsy bitsy spider" , "climbed up" , "the waterspout.")

There is one print() function invocation, but it contains three arguments.


All of them are strings.

The arguments are separated by commas. We've surrounded them with


spaces to make them more visible, but it's not really necessary, and we won't
be doing it anymore.

The spaces, removed from the strings, have appeared again. Can you explain
why?

Two conclusions emerge from this example:

 a print() function invoked with more than one argument outputs


them all on one line;
 the print() function puts a space between the outputted
arguments on its own initiative.

The print() function - the positional way of

passing the arguments

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.

In order to use it, it is necessary to know some rules:

 a keyword argument consists of three elements: a keyword identifying


the argument ( end here); an equal sign ( = ); and a value assigned to
that argument;
 any keyword arguments have to be put after the last positional
argument (this is very important)
 We've said previously that the print() function separates its
outputted arguments with spaces. This behavior can be changed, too.
 The keyword argument that can do this is
named sep (like separator).
 Look at the code in the editor, and run it.
 The sep argument delivers the following results:

My-name-is-Monty-Python.

 The print() function now uses a dash, instead of a space, to separate


the outputted arguments.
 Note: the sep argument's value may be an empty string, too. Try it for
yourself.

Key takeaways
1. The print() function is a built-in function. It prints/outputs a specified
message to the screen/console window.

2. Built-in functions, contrary to user-defined functions, are always available


and don't have to be imported. Python 3.8 comes with 69 built-in functions.
You can find their full list provided in alphabetical order in the Python
Standard Library.

3. To call a function (this process is known as function


invocation or function call), you need to use the function name followed by
parentheses. You can pass arguments into a function by placing them inside
the parentheses. You must separate arguments with a comma,
e.g., print("Hello,", "world!") . An "empty" print() function outputs an
empty line to the screen.

4. Python strings are delimited with quotes, e.g., "I am a string" (double
quotes), or 'I am a string, too' (single quotes).

5. Computer programs are collections of instructions. An instruction is a


command to perform a specific task when executed, e.g., to print a certain
message to the screen.

6. In Python strings the backslash ( \ ) is a special character which announces


that the next character has a different meaning, e.g., \n (the newline
character) starts a new output line.

7. Positional arguments are the ones whose meaning is dictated by their


position, e.g., the second argument is outputted after the first, the third is
outputted after the second, etc.

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.

You might also like