Compuzzle.files.wordpress.com



Installing and Developing Programs in PythonPython is pre-installed on most Unix systems, including Linux and MAC OS X.The pre-installed version may not be the most recent.Python 3 is a non-backward compatible version.You should use version Python 3.5 for CS2021.Download latest version from PythonThere are Two Ways to Run Python1. Interactive Mode: Running the Python interpreterYou type one expression at a timeThe interpreter evaluates the expression and prints its value2. Scripting: Running a Python file as 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 effectThe interactive interpreter (or REPL) 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!REPL: The running interpreter is also called a “read-eval-print loop”How to launch the Python interpreterTwo ways to launch the interpreter:1. Run a program such as IDLE; interactive interpreter is called the “Python shell”2. Type and run python at the operating system command lineType exit() to return to the operating system command lineThe Operating system command line, or “shell” or “command prompt” (cmd.exe under Windows or “terminal app” in MacOSX) is different than python command prompt.Your OS command line: can run many programs (Python included), and used to manipulate and moves around the file system. But, does not understand Python code like 1+2 or x = 22Your Python interpreter:Executes Python statements and expressions; but, does not understand program names and OS commands like python or cdRunning a Python file as programPython evaluates each statement one-by-onePython does no extra output, beyond print statements in the programTwo ways to run Python file as program:While editing a program within IDLE, press F5 (menu item “Run >> Run Module”)Must save the program first, if it is modifiedType at operating system command line:? %python myprogram.pyPython interpreter vs. Python programRunning a Python file as a program gives different results from pasting it line-by-line into the interpreterThe interactive interpreter prints more output than the program would. In 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 None This 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 outputSide effects vs. resultsSome 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 varietyA function that returns a value is a “fruitful function”A function that only prints some text is “non-fruitful”A 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 it When the code is executed for side effect, its return value is NoneRunning Python Interactively in UNIX-based OSOn MacOSX or Linux…% python>>> 3+36Python prompts with ‘>>>’. To exit Python (not in IDLE):In Unix, type CONTROL-DIn Windows, type CONTROL-Z + <Enter>Evaluate exit() Running Programs on UNIX-based OSCall python program via the python interpreter% python fact.pyOr; Make a python file directly executable by Adding the appropriate path to your python interpreter as the first line of your file#!/usr/bin/pythonMaking the file executable% chmod a+x fact.pyInvoking file from Unix command line% fact.pyPython IDEsThere are many Integrated Development Environments and Package Management Systems for Python ProgrammingIDLEEmacsAnaconda / SpyderEnthought CanopyKomodoPyCharmEclipse + PyDevIDLE Development EnvironmentIDLE is the “official” IDE distributed with PythonWritten in Python with the Tkinter GUI package Multi-window text editor with syntax highlighting, auto-completion, smart indent and other featuresPython shell with syntax highlighting, line recall, …Integrated debugger?with stepping, persistent breakpoints,?and call stack visibilityDeveloping Python in EmacsEmacs python-mode.el has good support for editing Python, by default for .py filesFeatures: syntax completion, symbol help, eldoc, and inferior interpreter shell, etc.Tutorial on Configuring Emacs for python is a leading Python distribution for supporting SciPy for large-scale data processing, predictive analytics, and scientific computingDownload Anaconda: Quickstart: and Running Python LibrariesPower of Python is in its large standard library and 3rd Party Packages.?Python has several alternatives for installing 3rd party packages.?PyPI - the Python Package Index is a repository of software for the Python programming language. There are currently?over 65000?packages.?How to Get and Install Packages:To use a package from the PyPI index either use shell command$ pip install?package-name or explicitly download, unpack, and "python setup.py install" it.Python Package Terminology?-Programs are composed of modules-Modules contain statements.-Statements contain expressions.-Expressions create and process objects.”ModulesModules are the basic unit of code reusability in Python: a block of code imported by some other code. Three types of modules concern us here: pure Python modules, extension modules, and packages.pure Python modulea module written in Python and contained in a single?.py?file (and possibly associated?.pyc?and/or?.pyo?files). Sometimes referred to as a “pure module.”extension modulea module written in the low-level language (e.g. C) of the Python implementationpackagea module that contains other modules; typically contained in a directory in the filesystem and distinguished from other directories by the presence of a file?__init__.py.Using the Module System: import and from Clients of module (.py file) that use import get a module with attributes, while clients that use from get copies of the file’s names. Generally, you will prefer to use import.Python programs are composed of multiple module files linked together by import statements, and each module has attributes -- a collection of variables—call it a namespace.""" Dead Parrot Sketch - """a = " Dead " # Define three names/attributes b = " Parrot " # to export to other files c = " Sketch "print(a+b+c) % python threenames.py # run from the command line here Dead Parrot Sketch% python>>> import threenames # Grab the whole module: it runs here too Dead Parrot Sketch>>>>>> threenames.b, threenames.c # but now we access module attributes(' Parrot ', ' Sketch ')>>>>>> from threenames import a, b, c #Copy multiple names out of module>>> b, c(' Parrot ', ' Sketch ')>>> dir(threenames)['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b', 'c']”>>> threenames.__file__'threenames.py'>>> threenames.__name__'threenames'>>> threenames.__doc__' Dead Parrot Sketch 'What is a Docstring?A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download