Python Tutorial_ Interactive Mode
Python Tutorial_ Interactive Mode
Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact
Aiming in the same In the following example we use the interpreter as a simple calculator by typing an arithmetic expression:
Data Protection
direction is a famous Declaration
>>> 4.567 * 8.323 * 17
quote by J.P. Morgan 646.18939699999999
"The first step >>> Data Protection
towards getting
Declaration
somewhere is to Python follows the usual order of operations in expressions. The standard order of operations is expressed in the following enumeration:
decide that you are
not going to stay 1. exponents and roots
where you are." 2. multiplication and division
3. addition and subtraction
This means that we don't need parenthesis in the expression "3 + (2 * 4):
Breaking the
Shell >>> 3 + 2 * 4
11
"Your pain is the >>>
breaking of the shell
that encloses your The most recent output value is automatically stored by the interpreter in a special variable with the name "_". So we can print the output from the recent example again by typing an underscore after the prompt:
understanding."
>>> _
(Kahlil Gibran, 11
Lebanese poet and >>>
artist)
This website is The underscore can be used in other expressions like any other variable:
created by:
>>> _ * 3
33
>>>
The underscore variable is only available in the Python shell. It's NOT available in Python scripts or programs.
Using Variables
It's simple to use variables in the Python shell. If you are an absolute beginner and if you don't know anything about variable, please confer our chapter about variables and data types.
Values can be saved in variables. Variable names don't require any special tagging, like they do in Perl, where you have to use dollar signs, percentage signs and at signs to tag variables:
Multiline Statements
We haven't introduced multine statements so far. So beginners can skip the rest of this chapter and can continue with the following chapters.
We will show, how the interactive prompt deals with multiline statements like for loops.
After having input "for character in l:" the interpretor expects the input of the next line to be indented. In other words: The interpretor expects an indented block, which is the body of the for loop. This indented
block will be iterated. The interpretor shows this "expectation" by showing three dots "..." instead of the standard Python interactive prompt ">>>". Another special feature of the interactive shell: When we have
finished with the indented lines, i.e. the block, we have to enter an empty line to indicate that the block is finished.
Attention: The additional empty line is only necessary in the interactive shell! In a Python program, it is enough to return to the indentation level of the "for" line, the one with the colon ":" at the end.
Strings
Strings are created by putting a sequence of characters in quotes. Strings can be surrounded by single quotes, double quotes or triple quotes, which are made up of three single or three double quotes. Strings are
immutable. This means that once defined, they cannot be changed. We will cover this topic in detail in another chapter.
String in triple quotes can span several lines without using the escape character:
>>>
>>> ".-." * 4
'.-..-..-..-.'
>>>
© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein