Module 1 Chap 1 Sakshi Training

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Module 1

1.1.1 Your very first program

It’s time to start writing some real, working Python code. It’ll be
very simple for the time being.
As we’re going to show you some fundamental concepts and
terms, these snippets of code won’t be serious or complex.

Launch IDLE, create a new Python source file, fill it with this
code, name the file and save it.
Now run it. If everything goes okay, you’ll see the rhyme’s line in
the IDLE console window.
Alternatively, launch Edube Sandbox, enter the code in the editor
window, and run it.
If everything goes okay here, you'll see the line of text in the
Edube console window.

The code you have run should look familiar. You saw something
very similar when we led you through the setting up of the IDLE
environment.
Now we’ll spend some time showing you what you’re actually
seeing, and why it looks like this.

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

 the word print;


 an opening parenthesis;
 a quotation mark;
 a line taken from a classic nursery rhyme;
 another quotation mark;
 a closing parenthesis.
Each of the above plays a very important role in the code.

Eg - Print(“Pussy cat, pussy cat where have you been?”)

1.1.2 The print() function

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’s 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 or some values (e.g., the square root of a


value or the length of a given text); this is what makes
Python’s functions the relatives of mathematical concepts.

Moreover, many of Python’s functions can do the above two


things together.

Where do the functions come from?


 They may come from Python itself; the printfunction 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.

1.1.3 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


arguments.
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 numberincludes zero – some Python functions don’t
need any argument.
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 a convention.
The function we’re talking about here is print().
Does the print() function in this example have any arguments?
Of course it does, but what are they?

1.1.4 The print() function

The only argument delivered to the print()function in this


example is a string.
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.

1.1.5 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.
What happens when Python encounters an invocation like this
one?

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

1.1.6 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 formif needed (as you may suspect, strings don’t
require this action, as the string is already readable) and send
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 evaluate?


None. Its effect is enough. print() does not evaluate anything.

1.1.7 The print() function

You already know that this program contains one function


invocation. In turn, the function invocation is one of the possible
kinds of Python instruction. Ergo, this program consists of just
one instruction.
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. This is how it looks now →

Run it and note what you see in the console.

Eg - Print(“Pussy cat, pussy cat where have you been?”)

Print(“I’ve been to London to look at the queen.”)

1.1.8 The print() function

Your Python console should now look like this →

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)

eg - Pussy cat, pussy cat where have you been?

I’ve been to London to look at the queen.

1.1.9 The print() function

We’ve changed the example a bit – we’ve added


one empty print() function invocation. We call it emptybecause
we haven’t delivered any arguments to the function →

Follow the example and make the same amendment in your


editor window (don’t forget about the parentheses).
Run it.
What happens?

Eg - Print(“Pussy cat, pussy cat where have you been?”)

Print()

Print(“I’ve been to London to look at the queen.”)

1.1.10 The print() function

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


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.

1.1.11 The print() function

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

Run the code.

Eg - Print(“Pussy cat, pussy cat /nwhere have you been?”)

Print()

Print(“I’ve been to London /nto look at the queen.”)

1.1.12 The print() function

Your console should now look like this →

As you can see, two newlines appear in the rhyme, in the places
where the \n have been used.
You have now 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.
Pussy cat, pussy cat

where have you been?

I’ve been to London

to look at the queen.

1.1.13 The print() function

This is what we’re going to test now →

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.

Eg – Print(“Pussy cat,” ,”pussy cat”, “where have you been?”)

1.1.14 The print() function

Your console should now be showing the following text →


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

Two conclusions emerge from this example:

1. a print() function invoked with more than one


argument outputs them all on one line;

2. the print() function puts a space between the outputted


arguments on its own initiative.

Eg- Pussy cat, pussy cat where have you been?

1.1.15 The print() function

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 this output without running the
code.
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).

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.

Eg- print(“My name is”, “Python”)

Print(“Monty Python”)

1.1.16 The print() function

To the side, 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.

Eg- print(“My name is”, “Python”, end=” “)

Print(“Monty Python”)
1.1.17 The print() function

The output looks as follows now →

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 endkeyword
argument is implicitly used in the following way:

end="\n"

And now it’s time to try something more difficult

Eg - My name is Python Monty Python

1.1.18 The print() function

If you look carefully, you’ll see that we’ve used the endargument,
but the string assigned to is empty (it contains no characters at
all) →

What will happen now?


Run the program to find out.

Eg - print(“My name is”, end=” “)

Print(“Monty Python”)
1.1.19 The print() function

As the end argument has been set to nothing,


the print() function outputs nothing too, once its positional
arguments have been exhausted.
The output looks like this →

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.

Eg – My name is Monty Python

1.1.20 The print() function

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 example →

print(“My”, “name”, “is”, “Monty”,“Python”,sep=”-“)

1.1.21 The print() function


The sep argument delivers the following results →

The print() function 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.

My-name-is-Monty-Python

1.1.22 The print() function

Both keyword arguments may be mixed in one invocation, just


like here →

The example doesn’t make much sense, but it visibly presents the
interactions between end and sep.
Can you predict the output?

Print(“My”, “name”, “is”, sep=”-“,end=”*”)

Print(“Monty”, “Python”, sep=”-“,end=”*/n”)

1.1.23 The print() function

This is how it looks now →

Now that you understand the print() function, you’re ready to


consider how to store and process data in Python.
Without print(), you wouldn’t be able to see any results.
My_name_is*Monty*Python*

You might also like