0% found this document useful (0 votes)
63 views

What Makes A Language?: Certified Entry-Level Python Programmer (PCEP-30-02)

The document discusses the key elements that make up a programming language including alphabet, lexis, syntax, and semantics. It then covers the differences between compiled and interpreted languages, noting that Python is an interpreted language. The text provides details on what compilers and interpreters do and the advantages and disadvantages of each approach.

Uploaded by

Sneha v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

What Makes A Language?: Certified Entry-Level Python Programmer (PCEP-30-02)

The document discusses the key elements that make up a programming language including alphabet, lexis, syntax, and semantics. It then covers the differences between compiled and interpreted languages, noting that Python is an interpreted language. The text provides details on what compilers and interpreters do and the advantages and disadvantages of each approach.

Uploaded by

Sneha v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Certified Entry-Level Python Programmer (PCEP-30-02)

MODULE 1

What makes a language?


We can say that each language (machine or natural, it doesn't matter) consists of the following elements:

 an alphabet: a set of symbols used to build words of a certain language (e.g., the Latin alphabet
for English, the Cyrillic alphabet for Russian, Kanji for Japanese, and so on)
 a lexis: (aka a dictionary) a set of words the language offers its users (e.g., the word "computer"
comes from the English language dictionary, while "cmoptrue" doesn't; the word "chat" is present
both in English and French dictionaries, but their meanings are different)
 a syntax: a set of rules (formal or informal, written or felt intuitively) used to determine if a
certain string of words forms a valid sentence (e.g., "I am a python" is a syntactically correct
phrase, while "I a python am" isn't)
 semantics: a set of rules determining if a certain phrase makes sense (e.g., "I ate a doughnut"
makes sense, but "A doughnut ate me" doesn't)

The IL is, in fact, the alphabet of a machine language. This is the simplest and most primary set of
symbols we can use to give commands to a computer. It's the computer's mother tongue.

Unfortunately, this mother tongue is a far cry from a human mother tongue. We both (computers and
humans) need something else, a common language for computers and humans, or a bridge between the two
different worlds.

We need a language in which humans can write their programs and a language that computers may use to
execute the programs, one that is far more complex than machine language and yet far simpler than natural
language.

Such languages are often called high-level programming languages. They are at least somewhat similar to
natural ones in that they use symbols, words and conventions readable to humans. These languages enable
humans to express commands to computers that are much more complex than those offered by ILs.

A program written in a high-level programming language is called a source code (in contrast to the
machine code executed by computers). Similarly, the file containing the source code is called the source
file.

Compilation vs. interpretation


Computer programming is the act of composing the selected programming language's elements
in the order that will cause the desired effect. The effect could be different in every specific case
– it's up to the programmer's imagination, knowledge and experience.

Of course, such a composition has to be correct in many senses:

 alphabetically – a program needs to be written in a recognizable script, such as Roman,


Cyrillic, etc.
 lexically – each programming language has its dictionary and you need to master it;
thankfully, it's much simpler and smaller than the dictionary of any natural language;
 syntactically – each language has its rules and they must be obeyed;
 semantically – the program has to make sense.

Unfortunately, a programmer can also make mistakes with each of the above four senses. Each
of them can cause the program to become completely useless.

Let's assume that you've successfully written a program. How do we persuade the computer to
execute it? You have to render your program into machine language. Luckily, the translation can
be done by a computer itself, making the whole process fast and efficient.

There are two different ways of transforming a program from a high-level programming
language into machine language:

COMPILATION - the source program is translated once (however, this act must be repeated
each time you modify the source code) by getting a file (e.g., an .exe file if the code is intended to
be run under MS Windows) containing the machine code; now you can distribute the file
worldwide; the program that performs this translation is called a compiler or translator;

INTERPRETATION - you (or any user of the code) can translate the source program each time it
has to be run; the program performing this kind of transformation is called an interpreter, as it
interprets the code every time it is intended to be executed; it also means that you cannot just
distribute the source code as-is, because the end-user also needs the interpreter to execute it.

What does the interpreter actually do?


Let's assume once more that you have written a program. Now, it exists as a computer file: a computer
program is actually a piece of text, so the source code is usually placed in text files.

Note: it has to be pure text, without any decorations like different fonts, colors, embedded images or other
media. Now you have to invoke the interpreter and let it read your source file.

The interpreter reads the source code in a way that is common in Western culture: from top to bottom and
from left to right. There are some exceptions - they'll be covered later in the course.

First of all, the interpreter checks if all subsequent lines are correct (using the four aspects covered earlier).

If the interpreter finds an error, it finishes its work immediately. The only result in this case is an error
message.

The interpreter will inform you where the error is located and what caused it. However, these messages
may be misleading, as the interpreter isn't able to follow your exact intentions, and may detect errors at
some distance from their real causes.

For example, if you try to use an entity of an unknown name, it will cause an error, but the error will be
discovered in the place where it tries to use the entity, not where the new entity's name was introduced.

In other words, the actual reason is usually located a little earlier in the code, for example, in the place
where you had to inform the interpreter that you were going to use the entity of the name.
If the line looks good, the interpreter tries to execute it (note: each line is usually executed separately, so
the trio "read-check-execute" can be repeated many times - more times than the actual number of lines in
the source file, as some parts of the code may be executed more than once).

It is also possible that a significant part of the code may be executed successfully before the interpreter
finds an error. This is normal behavior in this execution model.

You may ask now: which is better? The "compiling" model or the "interpreting" model? There is no
obvious answer. If there had been, one of these models would have ceased to exist a long time ago. Both of
them have their advantages and their disadvantages.

Compilation vs. interpretation - advantages and


disadvantages

COMPILATION INTERPRETATION
 the execution of the
translated code is usually  you can run the code as soon as you
faster; complete it - there are no additional
 only the user has to have phases of translation;
the compiler - the end-user  the code is stored using
may use the code without it; programming language, not machine
ADVANTAGES
 the translated code is stored language - this means that it can be
using machine language - as run on computers using different
it is very hard to understand machine languages; you don't
it, your own inventions and compile your code separately for
programming tricks are each different architecture.
likely to remain your secret.
 the compilation itself may  don't expect interpretation to ramp
DISADVANTAGES
be a very time-consuming up your code to high speed - your
COMPILATION INTERPRETATION
process - you may not be code will share the computer's power
able to run your code with the interpreter, so it can't be
immediately after making really fast;
an amendment;  both you and the end user have to
 you have to have as many have the interpreter to run your code.
compilers as hardware
platforms you want your
code to be run on.

What does this all mean for you?

 Python is an interpreted language. This means that it inherits all the described advantages and
disadvantages. Of course, it adds some of its unique features to both sets.
 If you want to program in Python, you'll need the Python interpreter. You won't be able to run
your code without it. Fortunately, Python is free. This is one of its most important advantages.

Due to historical reasons, languages designed to be utilized in the interpretation manner are often
called scripting languages, while the source programs encoded using them are called scripts.

What is Python?
Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic
semantics, used for general-purpose programming.

And while you may know the python as a large snake, the name of the Python programming language
comes from an old BBC television comedy sketch series called Monty Python's Flying Circus.

At the height of its success, the Monty Python team were performing their sketches to live audiences
across the world, including at the Hollywood Bowl.

Since Monty Python is considered one of the two fundamental nutrients to a programmer (the other being
pizza), Python's creator named the language in honor of the TV show.

Who created Python?


One of the amazing features of Python is the fact that it is actually one person's work. Usually, new
programming languages are developed and published by large companies employing lots of professionals,
and due to copyright rules, it is very hard to name any of the people involved in the project. Python is an
exception.

There are not many languages whose authors are known by name. Python was created by Guido van
Rossum, born in 1956 in Haarlem, the Netherlands. Of course, Guido van Rossum did not develop and
evolve all the Python components himself.
The speed with which Python has spread around the world is a result of the continuous work of thousands
(very often anonymous) programmers, testers, users (many of them aren't IT specialists) and enthusiasts,
but it must be said that the very first idea (the seed from which Python sprouted) came to one head -
Guido's.

A hobby programming project


The circumstances in which Python was created are a bit puzzling. According to Guido van Rossum:

In December 1989, I was looking for a "hobby" programming project that would keep me
occupied during the week around Christmas. My office (...) would be closed, but I had a home
computer, and not much else on my hands. I decided to write an interpreter for the new scripting
language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C
hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and
a big fan of Monty Python's Flying Circus).Guido van Rossum

Python goals
In 1999, Guido van Rossum defined his goals for Python:
 an easy and intuitive language just as powerful as those of the major competitors;
 open source, so anyone can contribute to its development;
 code that is as understandable as plain English;
 suitable for everyday tasks, allowing for short development times.

About 20 years later, it is clear that all these intentions have been fulfilled. Some sources say that Python is
the most popular programming language in the world, while others claim it's the second or the third.

Either way, it still occupies a high rank in the top ten of the PYPL PopularitY of Programming
Language and the TIOBE Programming Community Index.

Python isn't a young language anymore. It is mature and trustworthy. It's not a one-hit wonder. It's a
bright star in the programming firmament, and time spent learning Python is a very good investment.

What makes Python special?


How does it happen that programmers, young and old, experienced and novice, want to use it? How did it
happen that large companies adopted Python and implemented their flagship products using it?

There are many reasons – we've listed some of them already, but let's enumerate them again in a more
practical manner:

 it's easy to learn – the time needed to learn Python is shorter than for many other languages; this
means that it's possible to start the actual programming faster;
 it's easy to teach – the teaching workload is smaller than that needed by other languages; this
means that the teacher can put more emphasis on general (language-independent) programming
techniques, not wasting energy on exotic tricks, strange exceptions and incomprehensible rules;
 it's easy to use for writing new software – it's often possible to write code faster when using
Python;
 it's easy to understand - it's also often easier to understand someone else's code faster if it is
written in Python;
 it's easy to obtain, install and deploy – Python is free, open and multiplatform; not all languages
can boast that.

Of course, Python has its drawbacks, too:

 it's not a speed demon – Python does not deliver exceptional performance;
 in some cases it may be resistant to some simpler testing techniques – this may mean that
debugging Python code can be more difficult than with other languages; fortunately, making
mistakes is also harder in Python.
It should also be stated that Python is not the only solution of its kind available on the IT market.

It has lots of followers, but there are many who prefer other languages and don't even consider Python for
their projects.

Python rivals?
Python has two direct competitors, with comparable properties and predispositions. These are:

 Perl – a scripting language originally authored by Larry Wall;


 Ruby – a scripting language originally authored by Yukihiro Matsumoto.

The former is more traditional and more conservative than Python, and resembles some of the old
languages derived from the classic C programming language.
In contrast, the latter is more innovative and more full of fresh ideas than Python. Python itself lies
somewhere between these two creations.

The Internet is full of forums with infinite discussions on the superiority of one of these three over the
others, should you wish to learn more about each of them.

Where can we see Python in action?


We see it every day and almost everywhere. It's used extensively to implement complex Internet
services like search engines, cloud storage and tools, social media and so on. Whenever you use any of
these services, you are actually very close to Python, although you wouldn't know it.

Many developing tools are implemented in Python. More and more everyday-use applications are being
written in Python. Lots of scientists have abandoned expensive proprietary tools and switched to Python.
Lots of IT project testers have started using Python to carry out repeatable test procedures. The list is long.

Why not Python?


Despite Python's growing popularity, there are still some niches where Python is absent, or is rarely seen:

 low-level programming (sometimes called "close to metal" programming): if you want to


implement an extremely effective driver or graphical engine, you wouldn't use Python;
 applications for mobile devices: although this territory is still waiting to be conquered by Python,
it will most likely happen someday.
There is more than one Python
There are two main kinds of Python, called Python 2 and Python 3.

Python 2 is an older version of the original Python. Its development has since been intentionally stalled,
although that doesn't mean that there are no updates to it. On the contrary, the updates are issued on a
regular basis, but they are not intended to modify the language in any significant way. They rather fix any
freshly discovered bugs and security holes. Python 2's development path has reached a dead end already,
but Python 2 itself is still very much alive.

Python 3 is the newer (or to be more precise, the current) version of the language. It's going through
its own evolutionary path, creating its own standards and habits.

These two versions of Python aren't compatible with each other. Python 2 scripts won't run in a Python 3
environment and vice versa, so if you want the old Python 2 code to be run by a Python 3 interpreter, the
only possible solution is to rewrite it, not from scratch, of course, as large parts of the code may remain
untouched, but you do have to revise all the code to find all possible incompatibilities. Unfortunately, this
process cannot be fully automatized.

It's too hard, too time-consuming, too expensive, and too risky to migrate an old Python 2 application to a
new platform, and it's even possible that rewriting the code will introduce new bugs into it. It's easier, and
more sensible, to leave these systems alone and to improve the existing interpreter, instead of trying to
work inside the already functioning source code.

Python 3 isn't just a better version of Python 2


– it is a completely different language, although it's very similar to its predecessor. When you
look at them from a distance, they appear to be the same, but when you look closely, though,
you notice a lot of differences.

If you're modifying an old existing Python solution, then it's highly likely that it was coded in
Python 2. This is the reason why Python 2 is still in use. There are too many existing Python 2
applications to discard it altogether.

NOTE
If you're going to start a new Python project, you should use Python 3, and this is the version of Python
that will be used during this course.

It is important to remember that there may be smaller or bigger differences between subsequent Python 3
releases (e.g., Python 3.6 introduced ordered dictionary keys by default under the CPython
implementation) – the good news, though, is that all the newer versions of Python 3 are backward
compatible with the previous versions of Python 3. Whenever meaningful and important, we will always
try to highlight those differences in the course.

All the code samples you will find during the course have been tested against Python 3.4, Python 3.6,
Python 3.7, and Python 3.8.

Python aka CPython


In addition to Python 2 and Python 3, there is more than one version of each.

First of all, there are the


Pythons which are maintained by the people gathered around the PSF (Python Software
Foundation), a community that aims to develop, improve, expand, and popularize Python and its
environment. The PSF's president is Guido von Rossum himself, and for this reason, these
Pythons are called canonical. They are also considered to be reference Pythons, as any other
implementation of the language should follow all standards established by the PSF.

Guido van Rossum used the "C" programming language to implement the very first version of his language
and this decision is still in force. All Pythons coming from the PSF are written in the "C" language. There
are many reasons for this approach. One of them (probably the most important) is that thanks to it, Python
may be easily ported and migrated to all platforms with the ability to compile and run "C" language
programs (virtually all platforms have this feature, which opens up many expansion opportunities for
Python).

This is why the PSF implementation is often referred to as CPython. This is the most influential Python
among all the Pythons in the world.

Cython
Another Python family member is Cython.
Cython is one of a possible
number of solutions to the most painful of Python's traits – the lack of efficiency. Large and
complex mathematical calculations may be easily coded in Python (much easier than in "C" or
any other traditional language), but the resulting code execution may be extremely time-
consuming.

How are these two contradictions reconciled? One solution is to write your mathematical ideas using
Python, and when you're absolutely sure that your code is correct and produces valid results, you can
translate it into "C". Certainly, "C" will run much faster than pure Python.

This is what Cython is intended to do – to automatically translate the Python code (clean and clear, but not
too swift) into "C" code (complicated and talkative, but agile).

Jython
Another version of Python is called Jython.

"J" is for "Java". Imagine a Python written in Java instead of C. This is useful, for example, if you develop
large and complex systems written entirely in Java and want to add some Python flexibility to them. The
traditional CPython may be difficult to integrate into such an environment, as C and Java live in
completely different worlds and don't share many common ideas.

Jython can communicate with existing Java infrastructure more effectively. This is why some projects find
it useful and necessary.

Note: the current Jython implementation follows Python 2 standards. There is no Jython conforming to
Python 3, so far.
PyPy and RPython
Take a look at the logo below. It's a rebus. Can you solve it?

It's a logo of
the PyPy - a Python within a Python. In other words, it represents a Python environment written
in Python-like language named RPython (Restricted Python). It is actually a subset of Python.

The source code of PyPy is not run in the interpretation manner, but is instead translated into the
C programming language and then executed separately.

This is useful because if you want to test any new feature that may be (but doesn't have to be) introduced
into mainstream Python implementation, it's easier to check it with PyPy than with CPython. This is why
PyPy is rather a tool for people developing Python than for the rest of the users.

This doesn't make PyPy any less important or less serious than CPython, of course.

In addition, PyPy is compatible with the Python 3 language.

There are many more different Pythons in the world. You'll find them if you look, but this course will
focus on CPython.
Starting your work with Python
Now that you have Python 3 installed, it's time to check if it works and make the very first use of
it.

This will be a very simple procedure, but it should be enough to convince you that the Python
environment is complete and functional.

There are many ways of utilizing Python, especially if you're going to be a Python developer.

To start your work, you need the following tools:

 an editor which will support you in writing the code (it should have some special
features, not available in simple tools); this dedicated editor will give you more than the
standard OS equipment;
 a console in which you can launch your newly written code and stop it forcibly when it
gets out of control;
 a tool named a debugger, able to launch your code step-by-step, which will allow you to
inspect it at each moment of execution.

Besides its many useful components, the Python 3 standard installation contains a very simple
but extremely useful application named IDLE.

IDLE is an acronym: Integrated Development and Learning Environment.

How to spoil and fix your code


You may have noticed that the error message generated for the previous error is quite different from the
first one.
This is because the nature of the error is different and the error is discovered at a different stage of
interpretation.

The editor window will not provide any useful information regarding the error, but the console windows
might.

The message (in red) shows (in the subsequent lines):

 the traceback (which is the path that the code traverses through different parts of the program -
you can ignore it for now, as it is empty in such a simple code);
 the location of the error (the name of the file containing the error, line number and module
name); note: the number may be misleading, as Python usually shows the place where it first
notices the effects of the error, not necessarily the error itself;
 the content of the erroneous line; note: IDLE’s editor window doesn’t show line numbers, but it
displays the current cursor location at the bottom-right corner; use it to locate the erroneous line in
a long source code;
 the name of the error and a short explanation.

Experiment with creating new files and running your code. Try to output a different message to the screen,
e.g., roar! , meow , or even maybe an oink! . Try to spoil and fix your code - see what happens.
MODULE 2

print("Hello, World!")

As you can see, the first program consists of the following parts:

 the word print ;


 an opening parenthesis;
 a quotation mark;
 a line of text: Hello, World! ;
 another quotation mark;
 a closing parenthesis.

Each of the above plays a very important role in the code.

he print() function
Look at the line of code below:

print("Hello, World!")

The word print that you can see here is a function name. That doesn't mean that wherever the word
appears it is always a function name. The meaning of the word comes from the context in which the word
has been used.

You've probably encountered the term function many times before, during math classes. You can probably
also list several names of mathematical functions, like sine or log.

Python functions, however, are more flexible, and can contain more content than their mathematical
siblings.

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.
Moreover, many of Python functions can do the above two things together.

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.

The name of the function should be significant (the name of the print function is self-evident).

Of course, if you're going to make use of any already existing function, you have no influence on its name,
but when you start writing your own functions, you should consider carefully your choice of names.

The print() function


As we said before, a function may have:

 an effect;
 a result.

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

Mathematical functions usually take one argument, e.g., sin(x) takes an x, which is the measure of an
angle.

Python functions, on the other hand, are more versatile. Depending on the individual needs, they may
accept any number of arguments - as many as necessary to perform their tasks. Note: any number includes
zero - some Python functions don't need any argument.

print("Hello, World!")

In spite of the number of needed/provided arguments, Python functions strongly demand the presence of a
pair of parentheses - opening and closing ones, respectively.

If you want to deliver one or more arguments to a function, you place them inside the parentheses. If
you're going to use a function which doesn't take any argument, you still have to have the parentheses.
Note: to distinguish ordinary words from function names, place a pair of empty parentheses after their
names, even if the corresponding function wants one or more arguments. This is a standard convention.

The function we're talking about here is print() .

Does the print() function in our example have any arguments?

Of course it does, but what are they?

The print() function


The only argument delivered to the print() function in this example is a string:

print("Hello, World!")

As you can see, the string is delimited with quotes - in fact, the quotes make the string - they cut out a
part of the code and assign a different meaning to it.

You can imagine that the quotes say something like: the text between us is not code. It isn't intended to be
executed, and you should take it as is.

Almost anything you put inside the quotes will be taken literally, not as code, but as data. Try to play with
this particular string - modify it, enter some new content, delete some of the existing content.

There's more than one way to specify a string inside Python's code, but for now, though, this one is
enough.

So far, you have learned about two important parts of the code: the function and the string. We've talked
about them in terms of syntax, but now it's time to discuss them in terms of semantics.
The print() function
The function name (print in this case) along with the parentheses and argument(s), forms the function
invocation.

We'll discuss this in more depth soon, but we should just shed a little light on it right now.

print("Hello, World!")

What happens when Python encounters an invocation like this one below?

function_name(argument)

Let's see:

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

print("Hello, Python!")
print(Sneha)

error:
Hello, Python!
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(Sneha)
NameError: name 'Sneha' is not defined

The print() function


Three important questions have to be answered as soon as possible:

1. What is the effect the print() function causes?

The effect is very useful and very spectacular. The function:

 takes its arguments (it may accept more than one argument and may also accept less than one
argument)
 converts them into human-readable form if needed (as you may suspect, strings don't require this
action, as the string is already readable)
 and sends the resulting data to the output device (usually the console); in other words, anything
you put into the print() function will appear on your screen.

No wonder then, that from now on, you'll utilize print() very intensively to see the results of your
operations and evaluations.

2. What arguments does print() expect?

Any. We'll show you soon that print() is able to operate with virtually all types of data offered by
Python. Strings, numbers, characters, logical values, objects - any of these may be successfully passed
to print() .

3. What value does the print() function return?

None. Its effect is enough.

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


print("Down came the rain and washed the spider out.")

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.

Of course, any complex program usually contains many more instructions than one. The
question is: how do you couple more than one instruction into the Python code?

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.

A line can be empty (i.e., it may contain no instruction at all) but it must not contain two, three or
more instructions. This is strictly prohibited.
Note: 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).

Let's expand the code a bit, you can see it in the editor. Run it and note what you see in the
console.

Your Python console should now look like this:

The itsy bitsy spider climbed up the waterspout.


Down came the rain and washed the spider out.

output

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)

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


print()
print("Down came the rain and washed the spider out.")

The print() function - instructions


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.

You can see it in the editor window. Run the code.

What happens?

If everything goes right, you should see something like this:

The itsy bitsy spider climbed up the waterspout.

Down came the rain and washed the spider out.

output

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.
This is not the only way to produce a newline in the output console. We're now going to show
you another way.

print("The itsy bitsy spider\nclimbed up the waterspout.")


print()
print("Down came the rain\nand washed the spider out.")

The print() function - the escape and newline characters


We've modified the code again. Look at it carefully.

There are two very subtle changes - we've inserted a strange pair of characters inside the rhyme.
They look like this: \n .

Interestingly, while you can see two characters, Python sees one.

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.

Run the code. Your console should now look like this:

The itsy bitsy spider


climbed up the waterspout.

Down came the rain


and washed the spider out.

output

As you can see, two newlines appear in the nursery rhyme, in the places where the \n have
been used.
The print() function - the escape and newline characters
This convention has two important consequences:

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:

print("\")

while this one won't:

print("\\")

2. Not all escape pairs (the backslash coupled with another character) mean something.

Experiment with your code in the editor, run it, and see what happens.

The print() function - using multiple arguments


So far we have tested the print() function behavior with no arguments, and with one
argument. It's also worth trying to feed the print() function with more than one argument.

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.

In this case, the commas separating the arguments play a completely different role than the
comma inside the string. The former is a part of Python's syntax, the latter is intended to be
shown in the console.

If you look at the code again, you'll see that there are no spaces inside the strings.
Run the code and see what happens.

The console should now be showing the following text:

The itsy bitsy spider climbed up the waterspout.

output

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.

print("My name is", "Python.")


print("Monty Python.")
My name is Python.
Monty Python.

The print() function - the positional way of passing the


arguments
Now that you know a bit about print() function customs, we're going to show you how to
change them.

You should be able to predict the output without running the code in the editor.

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

print("My name is", "Python.", end=" ")


print("Monty Python.")

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.

We aren't going to explain it in depth right now. We plan to do this when we talk about functions.
For now, we simply want to show you how it works. Feel free to use it in your own programs.
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 the editor window you can see a very simple example of using a keyword argument.

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)

In our example, we have made use of the end keyword argument, and set it to a string
containing one space.

Run the code to see how it works.

The console should now be showing the following text:

My name is Python. Monty Python.

output

As you can see, the end keyword argument determines the characters the print() function sends
to the output once it reaches the end of its positional arguments.

The default behavior reflects the situation where the end keyword argument is implicitly used in
the following way: end="\n" .

print("My name is ", end="")


print("Monty Python.")

The print() function - the keyword arguments


And now it's time to try something more difficult.

If you look carefully, you'll see that we've used the end argument, but the string assigned to it is
empty (it contains no characters at all).

What will happen now? Run the program in the editor to find out.
As the end argument has been set to nothing, the print() function outputs nothing too, once
its positional arguments have been exhausted.

The console should now be showing the following text:

My name is Monty Python.

output

Note: no newlines have been sent to the output.

The string assigned to the end keyword argument can be of any length. Experiment with it if you
want.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy