/
T he Python interpreter T he Python interpreter

T he Python interpreter - PowerPoint Presentation

alexa-scheidler
alexa-scheidler . @alexa-scheidler
Follow
381 views
Uploaded On 2017-05-08

T he Python interpreter - PPT Presentation

CSE 160 University of Washington Ruth Anderson 1 Two ways to run Python The Python interpreter You type one expression at a time The interpreter evaluates the expression and prints its value ID: 546196

interpreter python program print python interpreter print program expression shell command side function run code line ipython system statements

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "T he Python interpreter" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

The Python interpreter

CSE 160University of WashingtonRuth Anderson

1Slide2

Two ways to run Python

The Python interpreterYou type one expression at a timeThe interpreter evaluates the expression and prints its value

Running a Python

program

Python evaluates all the statements in the file, in orderPython does not print their values (but does execute print statements)Writing an expression outside a statement (assignment, print, etc.) is useless, unless it is a function call that has a side effect

2Slide3

The Python interpreter

The interpreter is a loop that does:Read an expression

Evaluate

the expression

Print the resultIf the result is None, the interpreter does not print itThis inconsistency can be confusing!

(Jargon: An interpreter is also called a “read-

eval

-print loop”, or a REPL)

3Slide4

How to launch the Python interpreter

Two ways to launch the interpreter:Run Canopy; in the window labeled “Python” you should see:“Welcome

to Canopy's interactive data-analysis environment

!”

This window is a Python interpreter called the ipython shell*Type python

or

ipython

at

the operating system command

lineType exit() to return to the operating system command lineThese are not the same:Operating system command line, or “shell” or “command prompt” (cmd.exe under Windows) or “terminal”Runs programs (Python, others), moves around the file systemDoes not understand Python code like 1+2 or x = 22Python interpreterExecutes Python statements and expressionsDoes not understand program names like python or cd

4

* The

ipython

shell is what is shown in Canopy. You can access either the

python

shell

or the

ipython

shell from the operating system command line. The

ipython

shell has

more features than the

python

shell, but

both are Python interpreters.Slide5

Running a Python program

Python evaluates each statement one-by-onePython does no extra output, beyond print

statements in the program

Two ways to run a program:

While editing a program within Canopy:press the green triangle/play buttonHit Control and the letter R at the same timeSelect the menu item “Run >> Run File”Type at operating system command line:

python myprogram.py

5Slide6

Python interpreter vs. Python program

Running a Python file as a program gives different results from pasting it line-by-line into the interpreterThe interpreter prints more output than the program would

In the Python interpreter,

evaluating a top-level expression prints its value

Evaluating a sub-expression generally does not print any outputThe interpreter does not print a value for an expression that evaluates to NoneThis is primarily code that is executed for side effect: assignments, print statements, calls to “non-fruitful” functions

In a Python program,

evaluating an expression generally does not print

any output

6Slide7

Side effects vs. results

Some Python code is executed because it has a useful value(72 – 32) * 5.0 / 9

math.sqrt

(3*3 + 4*4)

Some Python code is executed because it has a side effectprint “hello”

x =

22

A function (call) can be of either variety

Think Python

calls a function that returns a value a “fruitful function”

A function that only prints some text is non-fruitfulA function should either return a value, or have a side effectIt is bad style for a function to do both Printing a value is completely different from returning itWhen the code is executed for a side effect, its value is None7