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

Python Tutorial_ Interactive Mode

The document is a tutorial on Python programming, covering various topics such as the interactive shell, data types, and functions. It provides insights into using the Python interpreter, executing commands, and handling strings and variables. The tutorial aims to facilitate self-learning and offers additional training options for those seeking in-person instruction.

Uploaded by

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

Python Tutorial_ Interactive Mode

The document is a tutorial on Python programming, covering various topics such as the interactive shell, data types, and functions. It provides insights into using the Python interpreter, executing commands, and handling strings and variables. The tutorial aims to facilitate self-learning and offers additional training options for those seeking in-person instruction.

Uploaded by

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

Python Course

Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact

Previous Chapter: The Origins of Python


Next Chapter: Executing a Script

The Interpreter, an Interactive Shell

The Terms 'Interactive' and 'Shell'


Follow Bernd Klein,
the author of this
The
website, at Google+:
term
Bernd Klein on
Python 3
Google
Tutorial
Bernd Klein on
The Origins of Facebook
Python
Starting with
Python: The Search this website:
Interactive Shell
Executing a Go
Script
Indentation This topic in German
Data Types and / Deutsche
Variables Übersetzung: Der
Interpreter
Operators
Sequential Data
Types: Lists and
Python 3
Strings
This is a tutorial in
List
Python3, but this
Manipulations
chapter of our course
Shallow and is available in a
Deep Copy version for Python
Dictionaries 2.x as well:
Sets and Frozen Interactive Mode in
Sets Python 2.x
An Extensive
Example Using Training Classes
Sets
input via the This website aims at
keyboard providing you with
educational material
Conditional
suitable for self-
Statements
learning.
Loops, while Nevertheless, it is
Loop faster and more
For Loops efficient to attend a
Difference "real" Python course
between in a classroom, with
interators und an experienced
trainer. So why not
Iterables
attend one of the live
Output with Print
Python courses
Formatted output
with string
modulo and the
format method
Functions
Recursion and
Recursive
Functions
Parameter
Passing in
Functions
Namespaces
in Strasbourg, Paris,
Global and Local
London, Berlin,
Variables Munich, Hamburg,
Decorators "interactive" traces back to the Latin expression "inter agere". The verb "agere" means amongst other things "to do something" and "to act", while "inter" denotes the spatial and temporal position to things and Frankfurt, or Lake
Memoization with events, i.e. "Between" or "among" objects, persons, and events. So "inter agere" means "to act between" or "to act among" these. Constance by Bernd
Decorators Klein, the author of
Read and Write With this in mind, we can say that the interactive shell is between the user and the operating system (e.g. Linux, Unix, Windows or others). Instead of an operating system an interpreter can be used for a this tutorial?
Files programming language like Python as well. The Python interpreter can be used from an interactive shell.
Modular
The interactive shell is also interactive in the way that it stands between the commands or actions and their execution. This means the Shell waits for commands from the user, which it executes and returns the In-house
Programming
result of the execution. After this the shell waits for the next input. Training
and Modules
Packages in
Courses
A shell in biology is a calcium carbonate "wall" which protects snails or mussels from its environment or its enemies. Similarly, a shell in operating systems lies between the kernel of the operating system and the
Python user. It's a "protection" in both direction. The user doesn't have to use the complicated basic functions of the OS but is capable of using simple and easier to understand shell commands. The kernel is protected
If you like it, we will
Regular from unintended incorrect usages of system function.
come to your
Expressions
company or institute
Regular Python offers a comfortable command line interface with the Python shell, which is also known as the "Python interactive shell".
and provide a special
Expressions, It looks like the term "interactive Shell" is a tautology, because "Shell" is interactive on its own, at least the kind of shells we have described in the previous paragraphs.
training for your
Advanced employees, as we've
Lambda done it many times in
Operator, Filter, Using the Python interactive Shell Amsterdam (The
Reduce and Map Netherlands), Berlin
With the Python interactive interpreter it is easy to check Python commands. The Python interpreter can be invoked by typing the command "python" without any parameter followed by the "return" key at the shell (Germany), Bern
List
prompt: (Switzerland), Basel
Comprehension
(Switzerland), Zurich
Iterators and python (Switzerland),
Generators Frankfurt (Germany),
Exception Python comes back with the following information: Locarno
Handling (Switzerland), Den
Tests, DocTests, $ python Haag (The Hague),
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) Hamburg, Munich
UnitTests [GCC 5.3.1 20160413] on linux2
Object Oriented (Germany),
Type "help", "copyright", "credits" or "license" for more information.
>>> Bucharest (Romania),
Programming
Toronto (Canada),
Class and
A closer look at the output above reveals that we have the wrong Python version. We wanted to use Python 3.x, but what we got is the installed standard of the operating system, i.e. version 2.7.11+. Edmonton (Canada),
Instance and many other
Attributes cities. We do training
The easiest way to check if a Python 3.x version is installed: Open a Terminal. Type in python but no return. Instead type the "Tab" key. You will see possible extensions and other installed versions, if there are
Properties vs. some: courses in England,
getters and Switzerland,
setters bernd@venus:~$ $ python Liechtenstein,
python python2.7 python3.5 python3m Austria, Germany,
Inheritance
python2 python3 python3.5m France, Belgium, the
Multiple bernd@venus:~$ python
Netherlands,
Inheritance
Luxembourg,
Magic Methods If no other Python version shows up, python3.x has to be installed. Afterwards, we can start the newly installed version by typing python3:
Romania, UK, Italy,
and Operator Spain and other
$ python3
Overloading Python 3.5.1+ (default, Mar 30 2016, 22:46:26) locations in Europe
OOP, Inheritance [GCC 5.3.1 20160330] on linux and in Canada.
Example Type "help", "copyright", "credits" or "license" for more information.
>>> This way you will get
Slots
Classes and a perfect training up
Once the Python interpreter is started, you can issue any command at the command prompt ">>>". to your needs and it
Class Creation
Let's see, what happens, if we type in the word "hello": will be extremely cost
Road to
efficient as well.
Metaclasses >>> hello
Metaclasses Traceback (most recent call last): Contact us so we can
Metaclass Use File "<stdin>", line 1, in <module>
find the ideal course
NameError: name 'hello' is not defined
Case: Count >>> to meet your needs.
Function Calls
Abstract Classes Of course, "hello" is not a proper Python command, so the interactive shell returns ("raises") an error.
Skilled Python
The first real command we will use is the print command. We will create the mandatory "Hello World" statement: Programmers
First Steps
>>> print("Hello World")
You are looking for
Hello World
"Take the first step, >>> experienced Python
and your mind will developers or
mobilize all its forces It couldn't have been easier, could it? Oh yes, it can be written in a even simpler way. In the interactive Python interpreter - but not in a program - the print is not necessary. We can just type in a string or a number programmers? We
to your aid. But the and it will be "echoed" can help you, please
first essential is that contact us.
you begin. Once the >>> "Hello World"
'Hello World' Quote of the
battle is startled, all
>>> 3
that is within and 3 Day:
without you will come >>>
to your assistance." "If programmers
(Robert Collier, the deserve to be
author of self help rewarded for creating
books) innovative programs,
How to Quit the Python Shell
by the same token
He also wrote: "The they deserve to be
So, we have just started, and we already talk about quitting the shell. We do this, because we know, how annoying it can be, if you don't know how to properly quit a program.
first essential, of punished if they
course, is to know restrict the use of
It's easy to end the interactive session: You can either use exit() or Ctrl-D (i.e. EOF) to exit. The brackets behind the exit function are crucial. (Warning: exit without brackets works in Python2.x but doesn't work
what you want." these programs. "
anymore in Python3.x)
We hope you know (Richard Stallmann)
what you want, and
that it means to
learn, to understand
and to apply Python.
The Shell as a Simple Calculator

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:

>>> maximal = 124


Python Training >>> width = 94
Courses in Toronto, >>> print(maximal - width)
Canada 30
On site trainings in >>>
Europe, Canada and
the US.

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.

>>> l = ["A",42,78,"Just a String"]


>>> for character in l:
... print(character)
...
A
42
78
Just a String
>>>

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.

>>> "Hello" + " " + "World"


'Hello World'

String in triple quotes can span several lines without using the escape character:

>>> city = """


... Toronto is the largest city in Canada
... and the provincial capital of Ontario.
... It is located in Southern Ontario on the
... northwestern shore of Lake Ontario.
... """
>>> print(city)

Toronto is the largest city in Canada


and the provincial capital of Ontario.
It is located in Southern Ontario on the
northwestern shore of Lake Ontario.

>>>

There is a multiplication on strings defined, which is essentially a multiple concatenation:

>>> ".-." * 4
'.-..-..-..-.'
>>>

Previous Chapter: The Origins of Python


Next Chapter: Executing a Script

© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein

You might also like