Module 1 Chap 1 Sakshi Training
Module 1 Chap 1 Sakshi Training
Module 1 Chap 1 Sakshi Training
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 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.
an effect;
a result.
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;
finally, Python returns to your code (to the place just after
the invocation) and resumes its execution.
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;
Print()
\n
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("\")
Print()
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
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).
Print(“Monty Python”)
Print(“Monty Python”)
1.1.17 The print() function
end="\n"
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) →
Print(“Monty Python”)
1.1.19 The print() function
My-name-is-Monty-Python
The example doesn’t make much sense, but it visibly presents the
interactions between end and sep.
Can you predict the output?