T he Python interpreter PowerPoint Presentation, PPT - DocSlides

T he Python interpreter PowerPoint Presentation, PPT - DocSlides

2017-05-08 34K 34 0 0

Description

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

Direct Link: Link:https://www.docslides.com/alexa-scheidler/t-he-python-interpreter Embed code:

Download this presentation

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

Presentations text content in T he Python interpreter

Slide1

The Python interpreter

CSE 160University of WashingtonRuth Anderson

1

Slide2

Two ways to run Python

The Python interpreterYou type one expression at a timeThe interpreter evaluates the expression and prints its valueRunning a Python programPython 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

2

Slide3

The Python interpreter

The interpreter is a loop that does:Read an expressionEvaluate the expressionPrint 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)

3

Slide4

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

5

Slide6

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 wouldIn the Python interpreter, evaluating a top-level expression prints its valueEvaluating 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” functionsIn a Python program, evaluating an expression generally does not print any output

6

Slide7

Side effects vs. results

Some Python code is executed because it has a useful value(72 – 32) * 5.0 / 9math.sqrt(3*3 + 4*4)Some Python code is executed because it has a side effectprint “hello”x = 22A function (call) can be of either varietyThink 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 None

7

Slide8


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.