Lessons on Python Modules and Packages

Size: px
Start display at page:

Download "Lessons on Python Modules and Packages"

Transcription

1 Lessons on Python Modules and Packages Walter Didimo [ 60 minutes ]

2 Programs so far So far we have written and tested simple programs in one of the two following ways: using the Python interactive mode (IDLE shell) putting the entire code in a single file and running it (either within the IDLE editor or from an OS shell)

3 Structuring programs When a program becomes more complex, you need to structure it in a better way: you may want to decompose the program into smaller parts, each written in a separate file you may want to reuse some of these parts to build other programs Every modern programming language makes it possible to structure a program into smaller parts, which can be exchanged among programmers and reused in several programs

4 Modules Python defines the concept of module: a module is a file that groups a certain set of definitions (variables, functions, classes, ); modules can be imported by any other module or in the interactive mode; by default, each module has its own namespace for the defined elements this mechanism allows different modules to coexist without causing collisions if they use an element (e.g., a variable) with the same name

5 A first example Let us start with a very simple example: create the following file, named util.py PI = minimum = min maximum = max util.py def avg(*args): sum = 0 for s in args: sum += float(s) avg = sum/len(args) return avg

6 A first example In the interactive mode you can write >>> import util.. and this statement imports all elements defined in the util.py module; after that, any of these elements can be recalled, using the module namespace (i.e., the string util.) as its prefix >>> util.minimum(10,util.pi,20) >>> util.avg(10,util.pi,20)

7 The import statement Other than in the interactive mode, the import statement can be used within any module to use another module As already observed, if A and B are two modules that define an element with the same name, say for example elem, both modules can be simultaneously imported by another module without problems A.elem will refer to the definition of elem given in A B.elem will refer to the definition of elem given in B

8 The Module Search Path When a module is imported, Python needs to know where this module can be searched: initially, it checks if it is a standard module, i.e., a module included in the standard library that comes with the Python installation if not, it searches in the list of directories represented by the variable sys.path (sys is a standard module)

9 The sys.path variable The sys.path variable is initialized with the following directories: the directory of the input program (or the current directory if a file is not specified) the list of directories in the OS environment variable PYTHONPATH (which can be set with the same syntax as the variable PATH) the installation-dependent default

10 The sys.path variable A program can modify the variable sys.path; for instance, the following code adds to the variable the path "C:\my_modules" >>> import sys >>> sys.path.append('c:\my_modules') Note that, sys is not automatically imported you need to import it if you want to use its definitions some other standard modules are automatically imported (e.g., string)

11 The from.. import statement The from.. import statement is a variant of the import statement: it can be used to import only some parts of a module it will not import the namespace of the module the imported definitions must be used directly with their names, without being preceded by the name of the module >>> from util import PI >>> print(pi) Curiosity: What happens if you import from two distinct modules an element with the same name?

12 The from.. import statement The from.. import statement can also be used with the * wildcard >>> from util import * It will import every element of the module, except those whose names start with one or more "_" elements whose names start with one or more "_" are considered private in Python and are not imported by default of course, you can import them explicitly

13 Inspecting a module: dir() The function dir() can be used to get the list of definitions in a module, lexicographically ordered >>> import util >>> dir(util) ['PI', ' builtins ', ' cached ', ' doc ', ' file ', ' loader ', ' name ', ' package ', ' spec ', 'avg', 'maximum', 'minimum']

14 The variable name The variable name for a module just stores the name of the module as a string >>> util. name 'util' If name is accessed without specifying a module, you get the name of the module that is currently running >>> name ' main '

15 The main module The first module that Python runs for a program is called the top-level program: if you run the interactive Python interpreter, it will be the top-level program If you use a command python x.py, module x will be the top-level program The top-level program is called main Thus, the value of the name variable within the top-level program is always ' main '

16 Executing modules as programs The name value can be checked within a module to run some code if and only if that module is the top-level program def compute(n): f = 1; for i in range(1,n+1): f *= i return f factorial.py if name == ' main ': import sys f = compute(int(sys.argv[1])) print(f)

17 Executing modules as programs def compute(n): f = 1; for i in range(1,n+1): f *= i return f factorial.py if name == ' main ': import sys f = compute(int(sys.argv[1])) print(f) python factorial 4 24 sys.argv[0] sys.argv[1]

18 Compiled modules For efficiency reasons, the first time a module is imported or called, Python generates a compiled version of that module (file) and caches it into a specific directory for future calls: the cache directory is named pycache Compiled codes allow for faster executions than interpreted codes, but compiling a source code may take some time: thus, caching compiling codes saves time

19 Compiled modules For a module named module, the corresponding compiled file is called: module.version.pyc.. where version is the Python version If the source code of the module changes, Python automatically recompiles it and updates the cache In an interpreter session, a module is imported only once; if the module changes you need to reload it, by writing the following import importlib; importlib.reload(module)

20 Packages Similarly to other programming languages (e.g. Java), modules can be grouped into packages From a logical point of view, a package should group a set of affine modules, i.e., modules used for some specific purpose (graphics, text processing, sounds,..) From a technical point of view, a package corresponds to a directory: it must contain all its modules and a special file called init.py

21 Packages The init.py file can be empty, or can contain some initialization code when the package is imported for the first time To import a module mod.py that is in a package pack, use one of the following syntaxes: import pack.mod from pack import mod A package can contain other sub-packages; if mod.py is inside subpack, and subpack is inside pack, you can write: import pack.subpack.mod

22 The * wildcard The * symbol can be used as a wildcard to import all modules of a package The following syntax can be used from pack import * In fact, this instruction will NOT import all modules of the package pack: it will execute init.py and will import those modules of pack whose names are inserted in the list variable all, defined in init_.py example: all =["mod1","mod2"]

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays.

Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays. NETB 329 Lecture 4 Data Structures in Python Dictionaries Data type built into Python. Dictionaries are sometimes found in other languages as associative memories or associative arrays. 1 of 70 Unlike

More information

Modules and scoping rules

Modules and scoping rules C H A P T E R 1 1 Modules and scoping rules 11.1 What is a module? 106 11.2 A first module 107 11.3 The import statement 109 11.4 The module search path 110 11.5 Private names in modules 112 11.6 Library

More information

CS Advanced Unix Tools & Scripting

CS Advanced Unix Tools & Scripting & Scripting Spring 2011 Hussam Abu-Libdeh slides by David Slater Hussam Abu-Libdeh slides by David Slater & Scripting Back to Python There are a variety of addons for python we can use. They are called

More information

7. MODULES. Rocky K. C. Chang October 17, (Based on Dierbach)

7. MODULES. Rocky K. C. Chang October 17, (Based on Dierbach) 7. MODULES Rocky K. C. Chang October 17, 2016 (Based on Dierbach) Objectives Explain the specification of modules. Become familiar with the use of docstrings in Python. Explain the use of modules and namespaces

More information

Table of Contents EVALUATION COPY

Table of Contents EVALUATION COPY Table of Contents Introduction... 1-2 A Brief History of Python... 1-3 Python Versions... 1-4 Installing Python... 1-5 Environment Variables... 1-6 Executing Python from the Command Line... 1-7 IDLE...

More information

Lecture 4. Introduction to Python! Lecture 4

Lecture 4. Introduction to Python! Lecture 4 Lecture 4 Introduction to Python Lecture 4 Summary Modules (general things) Using modules Importing modules Standard Library modules Modules search path Import modules phases Packages Organizing Python

More information

Lessons on Python Functions

Lessons on Python Functions Lessons on Python Functions Walter Didimo [ 90 minutes ] Functions When you write a program, you may need to recall a certain block of instructions several times throughout your code A function is a block

More information

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas If you have your own PC, download and install a syntax-highlighting text editor and Python

More information

Babes-Bolyai University

Babes-Bolyai University Babes-Bolyai University arthur@cs.ubbcluj.ro Overview 1 Modules programming - a software design technique that increases the extent to which software is composed of independent, interchangeable components

More information

Introduction to Python

Introduction to Python Introduction to Python Version 1.1.5 (12/29/2008) [CG] Page 1 of 243 Introduction...6 About Python...7 The Python Interpreter...9 Exercises...11 Python Compilation...12 Python Scripts in Linux/Unix & Windows...14

More information

Modules and Packages. CS 339R (Python) Chapter 8

Modules and Packages. CS 339R (Python) Chapter 8 Modules and Packages CS 339R (Python) Chapter 8 Spring 2011 Loading a Module The import statement: Reads the source file Creates a module object in the current scope Executes all top-level statements You

More information

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas

Introduction to Python. Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas If you have your own PC, download and install a syntax-highlighting text editor and Python

More information

Senthil Kumaran S

Senthil Kumaran S Senthil Kumaran S http://www.stylesen.org/ Agenda History Basics Control Flow Functions Modules History What is Python? Python is a general purpose, object-oriented, high level, interpreted language Created

More information

Fun with functions! Built-in and user-defined functions

Fun with functions! Built-in and user-defined functions Fun with functions! A function is a block of code that performs a single action that can be reused indefinitely. They provide a great way of breaking code into smaller reusable pieces. And they're fun!

More information

PYTHON TRAINING COURSE CONTENT

PYTHON TRAINING COURSE CONTENT SECTION 1: INTRODUCTION What s python? Why do people use python? Some quotable quotes A python history lesson Advocacy news What s python good for? What s python not good for? The compulsory features list

More information

LECTURE 2. Python Basics

LECTURE 2. Python Basics LECTURE 2 Python Basics MODULES ''' Module fib.py ''' from future import print_function def even_fib(n): total = 0 f1, f2 = 1, 2 while f1 < n: if f1 % 2 == 0: total = total + f1 f1, f2 = f2, f1 + f2 return

More information

Problem 1 (a): List Operations

Problem 1 (a): List Operations Problem 1 (a): List Operations Task 1: Create a list, L1 = [1, 2, 3,.. N] Suppose we want the list to have the elements 1, 2, 10 range(n) creates the list from 0 to N-1 But we want the list to start from

More information

CS Programming Languages: Python

CS Programming Languages: Python CS 3101-1 - Programming Languages: Python Lecture 5: Exceptions / Daniel Bauer (bauer@cs.columbia.edu) October 08 2014 Daniel Bauer CS3101-1 Python - 05 - Exceptions / 1/35 Contents Exceptions Daniel Bauer

More information

Computational Physics. User Defined Modules

Computational Physics. User Defined Modules Computational Physics User Defined Modules Jan 31, 2019 http://hadron.physics.fsu.edu/~eugenio/comphy/ eugenio@fsu.edu pydoc Documentation generator and online help system pydoc numpy.random.random Why

More information

CSCI-580 Advanced High Performance Computing

CSCI-580 Advanced High Performance Computing CSCI-580 Advanced High Performance Computing Performance Hacking: Matrix Multiplication Bo Wu Colorado School of Mines Most content of the slides is from: Saman Amarasinghe (MIT) Square-Matrix Multiplication!2

More information

Haskell Scripts. Yan Huang

Haskell Scripts. Yan Huang Haskell Scripts Yan Huang yh33@indiana.edu Last Quiz Objectives Writing Haskell programs in.hs files Note some differences between programs typed into GHCi and programs written in script files Operator

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

Writing Python Libraries. Import Statements and Packaging

Writing Python Libraries. Import Statements and Packaging Writing Python Libraries Import Statements and Packaging Basics A Python file is called either a script or a module, depending on how it s run: Script: Run file as a top-level script - python file.py -

More information

Core Python is small by design

Core Python is small by design Core Python is small by design One of the key features of Python is that the actual core language is fairly small. This is an intentional design feature to maintain simplicity. Much of the powerful functionality

More information

Object Model Comparisons

Object Model Comparisons Object Model Comparisons 1 Languages are designed, just like programs Someone decides what the language is for Someone decides what features it's going to have Can't really understand a language until

More information

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26,

Part IV. More on Python. Tobias Neckel: Scripting with Bash and Python Compact Max-Planck, February 16-26, Part IV More on Python Compact Course @ Max-Planck, February 16-26, 2015 36 More on Strings Special string methods (excerpt) s = " Frodo and Sam and Bilbo " s. islower () s. isupper () s. startswith ("

More information

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 CS 133 - Introduction to Computational and Data Science Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 Previous class We have learned the path and file system.

More information

What Version Number to Install

What Version Number to Install A Python Primer This chapter provides a quick overview of the Python language. The goal in this chapter is not to teach you the Python language excellent books have been written on that subject, such as

More information

Using Introspect ESP Python Modules in External Python Scripts

Using Introspect ESP Python Modules in External Python Scripts Using Introspect ESP Python Modules in External Python Scripts This document discusses the use of the Python modules that are supplied with Introspect IESP in external Python scripts that you write. The

More information

Introduction to Functional Programming

Introduction to Functional Programming A Level Computer Science Introduction to Functional Programming William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London Aims and Claims Flavour of Functional

More information

Python Interview Questions & Answers

Python Interview Questions & Answers Python Interview Questions & Answers Q 1: What is Python? Ans: Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high

More information

Programming IDL for Astronomy September 6, 2004

Programming IDL for Astronomy September 6, 2004 Programming IDL for Astronomy September 6, 2004 Marshall Perrin 1 1. Introduction This is not a programming course, but nonetheless it will involve a lot of programming. This is true of astronomy as a

More information

C and C++ I. Spring 2014 Carola Wenk

C and C++ I. Spring 2014 Carola Wenk C and C++ I Spring 2014 Carola Wenk Different Languages Python sum = 0 i = 1 while (i

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

CS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee

CS 323 Lecture 1. Design and Analysis of Algorithms. Hoeteck Wee { CS 323 Lecture 1 } Design and Analysis of Algorithms Hoeteck Wee hoeteck@cs.qc.cuny.edu http://cs323.qwriting.org/ Algorithmic ideas are pervasive APPLICATIONS. Economics, auctions and game theory Biology,

More information

NOTES ON RUNNING PYTHON CODE

NOTES ON RUNNING PYTHON CODE NOTES ON RUNNING PYTHON CODE ERIC MARTIN Part 1. Setting things up The School has python 3.2.3 installed. 1. Installing python if necessary On personal computers with no version of python 3 installed,

More information

Computer Programming-1 CSC 111. Chapter 1 : Introduction

Computer Programming-1 CSC 111. Chapter 1 : Introduction Computer Programming-1 CSC 111 Chapter 1 : Introduction Chapter Outline What a computer is What a computer program is The Programmer s Algorithm How a program that you write in Java is changed into a form

More information

Absent: Lecture 3 Page 1. def foo(a, b): a = 5 b[0] = 99

Absent: Lecture 3 Page 1. def foo(a, b): a = 5 b[0] = 99 1. A function is a procedural abstract (a named body of code to perform some action and return a resulting value). The syntax of a function definition is: def functionname([parameter [, parameter]*]):

More information

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors First Python Program Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2 nd edition, by

More information

CSC326 Python Imperative Core (Lec 2)

CSC326 Python Imperative Core (Lec 2) i CSC326 Python Imperative Core (Lec ii REVISION HISTORY NUMBER DATE DESCRIPTION NAME 1.0 2011-09 JZ iii Contents 1 Agenda 1 2 Invoking Python 1 3 Value, Type, Variable 1 4 Keywords 2 5 Statement 2 6 Expression

More information

AMCAT Automata Coding Sample Questions And Answers

AMCAT Automata Coding Sample Questions And Answers 1) Find the syntax error in the below code without modifying the logic. #include int main() float x = 1.1; switch (x) case 1: printf( Choice is 1 ); default: printf( Invalid choice ); return

More information

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java

Learning objectives. The Java Environment. Java timeline (cont d) Java timeline. Understand the basic features of Java Learning objectives The Java Environment Understand the basic features of Java What are portability and robustness? Understand the concepts of bytecode and interpreter What is the JVM? Learn few coding

More information

The Compilation Process

The Compilation Process Crash Course in C Lecture 2 Moving from Python to C: The compilation process Differences between Python and C Variable declaration and strong typing The memory model: data vs. address The Compilation Process

More information

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p. Lecture 10 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 10: Formatted Input and Output 27-Sep-2017 Location: Goldberg CS 127 Time: 14:35 15:25 Instructor:

More information

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python

Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer. October 6th, Universität des Saarlandes. Introduction to Python Outline Alastair Burt Andreas Eisele Christian Federmann Torsten Marek Ulrich Schäfer Universität des Saarlandes October 6th, 2009 Outline Outline Today s Topics: 1 More Examples 2 Cool Stuff 3 Text Processing

More information

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6 1. Python is a calculator. A variable is a container Introduction to Python and Programming BBM 101 - Introduction to Programming I Hacettepe University Fall 016 Fuat Akal, Aykut Erdem, Erkut Erdem 3.

More information

Semester 2, 2018: Lab 1

Semester 2, 2018: Lab 1 Semester 2, 2018: Lab 1 S2 2018 Lab 1 This lab has two parts. Part A is intended to help you familiarise yourself with the computing environment found on the CSIT lab computers which you will be using

More information

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name: CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics,

More information

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 19 20 20, 2013 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

Programming in Python Advanced

Programming in Python Advanced Programming in Python Advanced Duration: 3 days, 8 hours a day Pre-requisites: * Participants should be comfortable with the following technologies: Basic and working knowledge of the Pyton If new to the

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes GIS 4653/5653: Spatial Programming and GIS More Python: Statements, Types, Functions, Modules, Classes Statement Syntax The if-elif-else statement Indentation and and colons are important Parentheses and

More information

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s

PYTHON FOR KIDS A Pl ayfu l I ntrodu ctio n to Prog r am m i ng J a s o n R. B r i g g s PYTHON FO R K I D S A P l ay f u l I n t r o d u c t i o n to P r o g r a m m i n g Jason R. Briggs Index Symbols and Numbers + (addition operator), 17 \ (backslash) to separate lines of code, 235 in strings,

More information

Algorithms and Programming I. Lecture#12 Spring 2015

Algorithms and Programming I. Lecture#12 Spring 2015 Algorithms and Programming I Lecture#12 Spring 2015 Think Python How to Think Like a Computer Scientist By :Allen Downey Installing Python Follow the instructions on installing Python and IDLE on your

More information

Programming Concepts: IDEs, Debug. Paulo Penteado. (http://phdcomics.com/comics/archive.php?

Programming Concepts: IDEs, Debug. Paulo Penteado.  (http://phdcomics.com/comics/archive.php? Programming Concepts: IDEs, Debug Paulo Penteado http://www.ppenteado.net/pc/ (http://phdcomics.com/comics/archive.php?comicid=1690) IDEs Interactive Development Environments Exist for every language (even

More information

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow

Monty Python and the Holy Grail (1975) BBM 101. Introduction to Programming I. Lecture #03 Introduction to Python and Programming, Control Flow BBM 101 Monty Python and the Holy Grail (1975) Introduction to Programming I Lecture #03 Introduction to Python and Programming, Control Flow Aykut Erdem, Fuat Akal & Aydın Kaya // Fall 2018 Last time

More information

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CPS122 Lecture: From Python to Java last revised January 4, Objectives: Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Lecture 1. basic Python programs, defining functions

Lecture 1. basic Python programs, defining functions Lecture 1 basic Python programs, defining functions Lecture notes modified from CS Washington CS 142 Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

More information

PHY224 Practical Physics I. Lecture 2

PHY224 Practical Physics I. Lecture 2 PHY224 Practical Physics I Python Review Lecture 2 Sept. 15 16 16, 2014 Summary Functions and Modules Graphs (plotting with Pylab) Scipy packages References M H. Goldwasser, D. Letscher: Object oriented

More information

Programming to Python

Programming to Python Programming to Python Sept., 5 th Slides by M. Stepp, M. Goldstein, M. DiRamio, and S. Shah Compiling and interpreting Many languages require you to compile (translate) your program into a form that the

More information

file_object=open( file_name.txt, mode ) f=open( sample.txt, w ) How to create a file:

file_object=open( file_name.txt, mode ) f=open( sample.txt, w ) How to create a file: UNIT 5 FILES, MODULES AND PACKAGES Files: text files, reading and writing files, format operator, command line arguments, Errors and Exceptions: handling exceptions, Modules, Packages; Illustrative programs:

More information

Systems Design and Implementation I.4 Naming in a Multiserver OS

Systems Design and Implementation I.4 Naming in a Multiserver OS Systems Design and Implementation I.4 Naming in a Multiserver OS System, SS 2009 University of Karlsruhe 06.5.2009 Jan Stoess University of Karlsruhe The Issue 2 The Issue In system construction we combine

More information

CSE 12 Abstract Syntax Trees

CSE 12 Abstract Syntax Trees CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4 Python, Part 2 CS 8: Introduction to Computer Science Lecture #4 Ziad Matni Dept. of Computer Science, UCSB A Word About Registration for CS8 This class is currently FULL The waitlist is CLOSED 4/13/17

More information

Short, Unique and Mysterious

Short, Unique and Mysterious Short, Unique and Mysterious Q Why is the Programming Language named so? a Monty Python's Flying Circus "A t t h e t i m e w h e n h e b e g a n implementing Python, Guido van R o s s u m w a s a l s o

More information

Lecture 02, Fall 2018 Friday September 7

Lecture 02, Fall 2018 Friday September 7 Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python

More information

CPS122 Lecture: From Python to Java

CPS122 Lecture: From Python to Java Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

STSCI Python Introduction. Class URL

STSCI Python Introduction. Class URL STSCI Python Introduction Class 2 Jim Hare Class URL www.pst.stsci.edu/~hare Each Class Presentation Homework suggestions Example files to download Links to sites by each class and in general I will try

More information

15-122: Principles of Imperative Computation

15-122: Principles of Imperative Computation 15-122: Principles of Imperative Computation Lab 0 Navigating your account in Linux Tom Cortina, Rob Simmons Unlike typical graphical interfaces for operating systems, here you are entering commands directly

More information

1 Little Man Computer

1 Little Man Computer 1 Little Man Computer Session 5 Reference Notes CPU Architecture and Assembly 1.1 Versions Little Man Computer is a widely used simulator of a (very simple) computer. There are a number of implementations.

More information

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer i About the Tutorial Project is a comprehensive software suite for interactive computing, that includes various packages such as Notebook, QtConsole, nbviewer, Lab. This tutorial gives you an exhaustive

More information

Modules and Programs 1 / 14

Modules and Programs 1 / 14 Modules and Programs 1 / 14 Python Programs Python code organized in modules, packages, and scripts. We ve already used some modules, now we ll learn what they are, how they re orgainized in packages,

More information

Course Title: Python + Django for Web Application

Course Title: Python + Django for Web Application Course Title: Python + Django for Web Application Duration: 6 days Introduction This course offer Python + Django framework ( MTV ) training with hands on session using Eclipse+Pydev Environment. Python

More information

XREPL: extended REPL

XREPL: extended REPL XREPL: extended REPL Version 6.7 Eli Barzilay ăeli@barzilay.orgą October 26, 2016 (require xrepl) package: xrepl-lib Loading the xrepl library enables XREPL, which extends the racket REPL significantly,

More information

Control structure: Repetition - Part 1

Control structure: Repetition - Part 1 Control structure: Repetition - Part 1 01204111 Computers and Programming Chalermsak Chatdokmaiprai Department of Computer Engineering Kasetsart University Cliparts are taken from http://openclipart.org

More information

C and Programming Basics

C and Programming Basics Announcements Assignment 1 Will be posted on Wednesday, Jan. 9 Due Wednesday, Jan. 16 Piazza Please sign up if you haven t already https://piazza.com/sfu.ca/spring2019/cmpt125 Lecture notes Posted just

More information

Exercise 1. Try the following code segment:

Exercise 1. Try the following code segment: Exercise 1 Try the following code segment: >>> list = [3,4,2,1] >>> for number in list: print 'item %s in list %s' % (number, list) if number > 2: list.remove(number) Pay attention to the print line The

More information

By default, it is assumed that we open a file to read data. So the above is equivalent to the following:

By default, it is assumed that we open a file to read data. So the above is equivalent to the following: LING115 Lecture Note Session #5: Files, Functions and Modules 1. Introduction A corpus comes packaged as a set of files. Obviously, we must know how to read data from a file into our program. At the same

More information

Profiling General Purpose Systems for Parallelism

Profiling General Purpose Systems for Parallelism Profiling General Purpose Systems for Parallelism 06.11.009 Clemens Hammacher, Kevin Streit, Sebastian Hack, Andreas Zeller Saarland University Department of Computer Science Saarbrücken, Germany Motivation

More information

nostarch.com/pfk For bulk orders, please contact us at

nostarch.com/pfk For bulk orders, please contact us at nostarch.com/pfk For bulk orders, please contact us at sales@nostarch.com. Teacher: Date/Period: Subject: Python Programming Class: Topic: #1 - Getting Started Duration: Up to 50 min. Objectives: Install

More information

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class

9/11/08 (c) 2008 Matthew J. Rutherford Class (c) 2008 Matthew J. Rutherford Class 1 2 3 4 5 6 Walter Savitch Frank M. Carrano Introduction to Computers and Java Chapter 1 ISBN 0136130887 2007 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 7 Hardware and Software

More information

A Little Python Part 2

A Little Python Part 2 A Little Python Part 2 Introducing Programming with Python Data Structures, Program Control Outline Python and the System Data Structures Lists, Dictionaries Control Flow if, for, while Reminder - Learning

More information

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python

Outline. Simple types in Python Collections Processing collections Strings Tips. 1 On Python language. 2 How to use Python. 3 Syntax of Python Outline 1 On Python language 2 3 4 Marcin Młotkowski Object oriented programming 1 / 52 On Python language The beginnings of Pythons 90 CWI Amsterdam, Guido van Rossum Marcin Młotkowski Object oriented

More information

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2 CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with

More information

Lab 9: Creating a Reusable Class

Lab 9: Creating a Reusable Class Lab 9: Creating a Reusable Class Objective This will introduce the student to creating custom, reusable classes This will introduce the student to using the custom, reusable class This will reinforce programming

More information

COMP 110/401 WINDOWS COMMAND INTERPRETER. Instructor: Prasun Dewan (FB 150,

COMP 110/401 WINDOWS COMMAND INTERPRETER. Instructor: Prasun Dewan (FB 150, COMP 110/401 WINDOWS COMMAND INTERPRETER Instructor: Prasun Dewan (FB 150, dewan@unc.edu) WINDOWS COMMAND INTERPRETER 2 COMMAND INTERPRETER? Interprets Command Lines Provides alternative to (OS and Application)

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Chapter 2 Operating-System Structures

Chapter 2 Operating-System Structures This chapter will discuss the following concepts: 2.1 Operating System Services 2.2 User Operating System Interface 2.3 System Calls 2.4 System Programs 2.5 Operating System Design and Implementation 2.6

More information

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with Project 1 and Java Intro Sharma Chakravarthy Information Technology Laboratory (IT Lab) Computer Science and Engineering Department The University of Texas at Arlington, Arlington, TX 76019 Email: sharma@cse.uta.edu

More information

6 Git & Modularization

6 Git & Modularization 6 Git & Modularization Bálint Aradi Course: Scientific Programming / Wissenchaftliches Programmieren (Python) Prerequisites Additional programs needed: Spyder3, Pylint3 Git, Gitk KDiff3 (non-kde (qt-only)

More information

CherryPy on Apache2 with mod_python

CherryPy on Apache2 with mod_python Revision History CherryPy on Apache2 with mod_python Revision 1.5 November 9, 2009 Revised by: FB Ferry Boender 1. Introduction I ve recently written a web application using Python using the following

More information

Computer Science 217

Computer Science 217 Computer Science 17 Midterm Exam March 5, 014 Exam Number 1 First Name: Last Name: ID: Class Time (Circle One): 1:00pm :00pm Instructions: Neatly print your names and ID number in the spaces provided above.

More information

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS 1439-1440 1 Outline 1. Introduction 2. Why Python? 3. Compiler and Interpreter 4. The first program 5. Comments and Docstrings 6. Python Indentations

More information

Coding Getting Started with Python

Coding Getting Started with Python DEVNET-3602 Coding 1002 - Getting Started with Python Matthew DeNapoli, DevNet Developer Evangelist Cisco Spark How Questions? Use Cisco Spark to communicate with the speaker after the session 1. Find

More information

Introduction to computers and Python. Matthieu Choplin

Introduction to computers and Python. Matthieu Choplin Introduction to computers and Python Matthieu Choplin matthieu.choplin@city.ac.uk http://moodle.city.ac.uk/ 1 Objectives To get a brief overview of what Python is To understand computer basics and programs

More information

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name: CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics,

More information

4.2 Function definitions the basics

4.2 Function definitions the basics 4.2. FUNCTION DEFINITIONS THE BASICS 89 4.2 Function definitions the basics There are three questions you must answer before you can write a function definition: What will the function do? What inputs

More information

Change Log. L-Py. July 24th 2009: version (rev 6689): Introduce Lsystem::Debugger. Introduce first ui of a Lsystem Debugger.

Change Log. L-Py. July 24th 2009: version (rev 6689): Introduce Lsystem::Debugger. Introduce first ui of a Lsystem Debugger. L-Py Change Log July 24th 2009: version 1.4.0 (rev 6689): Introduce Lsystem::Debugger Introduce first ui of a Lsystem Debugger. fix bug with animation when resuming (avoid reloading text) July 17th 2009:

More information