2! Multimedia Programming with! Python and SDL



Similar documents
Programming in Python. Basic information. Teaching. Administration Organisation Contents of the Course. Jarkko Toivonen. Overview of Python

Getting Started with the Internet Communications Engine

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Crash Dive into Python

Announcements FORTRAN ALGOL COBOL. Simula & Smalltalk. Programming Languages

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Moving from CS 61A Scheme to CS 61B Java

3 Multimedia Programming with C++ and Multimedia Frameworks

Evolution of the Major Programming Languages

CSCI 3136 Principles of Programming Languages

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

dictionary find definition word definition book index find relevant pages term list of page numbers

What is a programming language?

C# and Other Languages

Introduction to Object-Oriented Programming

Programming Languages

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Crash Dive into Python

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

cs2010: algorithms and data structures

Welcome to Introduction to programming in Python

Introduction to Python

1/20/2016 INTRODUCTION

3 Multimedia Programming with C++ and Multimedia Frameworks

Lecture 9. Semantic Analysis Scoping and Symbol Table

Fundamentals of Java Programming

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Outline. multiple choice quiz bottom-up design. the modules main program: quiz.py namespaces in Python

Java CPD (I) Frans Coenen Department of Computer Science

Image credits:

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Programming Languages CIS 443

Computing Concepts with Java Essentials

Programming Language Pragmatics

Programming Languages

The C Programming Language course syllabus associate level

6.1. Example: A Tip Calculator 6-1

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

CRASH COURSE PYTHON. Het begint met een idee

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

ML for the Working Programmer

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Invitation to Ezhil : A Tamil Programming Language for Early Computer-Science Education 07/10/13

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

McGraw-Hill The McGraw-Hill Companies, Inc.,

Python for Scientific Computing.

Pseudo code Tutorial and Exercises Teacher s Version

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

CS 106 Introduction to Computer Science I

02 B The Java Virtual Machine

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Concepts and terminology in the Simula Programming Language

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Introduction to Java

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Python CS1 as Preparation for C++ CS2

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Glossary of Object Oriented Terms

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Semester Review. CSC 301, Fall 2015

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

Ruby in the context of scientific computing

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Lecture Data Types and Types of a Language

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

The Smalltalk Programming Language. Beatrice Åkerblom

Computer Science. 232 Computer Science. Degrees and Certificates Awarded. A.S. Degree Requirements. Program Student Outcomes. Department Offices

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

MICHIGAN TEST FOR TEACHER CERTIFICATION (MTTC) TEST OBJECTIVES FIELD 050: COMPUTER SCIENCE

C++ Programming Language

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Computer Science. Computer Science 207. Degrees and Certificates Awarded. A.S. Computer Science Degree Requirements. Program Student Outcomes

Name Spaces. Introduction into Python Python 5: Classes, Exceptions, Generators and more. Classes: Example. Classes: Briefest Introduction

AP Computer Science Java Subset

Introduction to Programming

Computer Science. Computer Science 213. Faculty and Offices. Degrees and Certificates Awarded. AS Computer Science Degree Requirements

Implementing Rexx Handlers in NetRexx/Java/Rexx

MEAP Edition Manning Early Access Program Nim in Action Version 1

High-Level Language. Building a Modern Computer From First Principles.

How to start with 3DHOP

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Transcription:

2 Multimedia Programming with Python and SDL 2.1 Introduction to Python 2.2 SDL/Pygame: Multimedia/Game Frameworks for Python Literature: G. van Rossum and F. L. Drake, Jr., An Introduction to Python - The Python Tutorial (version 2.5), Network Theory 2006 http://www.network-theory.co.uk/docs/pytut/ LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 1

Guido van Rossum, 1991, CWI Amsterdam Now open source, current main versions: 2.7.6 (used here) and 3.4.0 Targeted at programming novices Characteristics: Interpreted scripting language Compiled to intermediate byte code (similar to Java) Multi-paradigm language: imperative/structured, object-oriented, functional, aspect-oriented Dynamic typing Automatic garbage collection Do you really understand all these terms? QUIZ: How is the foot related to Python? Images: Wikipedia LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 2

Java to Python: Imperative Example (Java) public class Main { public static int sequentialsearch(int q, int[] a) { for(int i = 0; i < a.length; i++) { if(a[i]==q) { return i; } } return -1; } public static void main(string[] args) { int[] a = {11, 22, 33, 44, 55, 66}; System.out.println("Array a: "+a); System.out.println("Search for 55: "+sequentialsearch(55,a)); System.out.println("Search for 23: "+sequentialsearch(23,a)); } } LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 3

Java to Python: Imperative Example def sequentialsearch (q, a): for i in range(0,len(a)): if a[i]==q: return i return -1 a = [11, 22, 33, 44, 55, 66] print "Array a: ", a print "Search for 55: ",sequentialsearch(55,a) print "Search for 23: ",sequentialsearch(23,a) QUIZ: What are the differences to Java? LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 4

First Observations on Python Very compact code Data types are not specified Powerful but simple built-in list datatype Indentation (white space) is important for program semantics Block levels given by indentation What is done in Java with {} brackets, is done here with indentation Example: A different (wrong) algorithm: def sequentialsearch (q, a): for i in range(0,len(a)): if a[i]==q: return i return -1 LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 5

Scripting Language Traditionally: A scripting language is a programming language that is used to control some application software Command languages for operating systems Task automatization in user interfaces Scripts for Web browsers, word processors, spreadsheet software, Historically, considered slow in execution and limited in program size Modern general-purpose scripting languages Have inherited many features from traditional scripting languages Are considered as full application programming languages: Examples: Rexx, Perl, Python, Ruby LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 6

Compilation, Interpretation and Others Program Compiler Machine code Compilation (Example: C) Input Machine code Output Input Interpreter Output Interpretation (Example: Python) Variations: Program Compilation to intermediate code (Java) Just-in-time compilation Internal compilation to intermediate code in interpreter (Python/ CPython) LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 7

Interactive Interpreter Interpreted languages can easily be executed line-by-line Interactive execution is helpful for understanding See BASIC, Logo etc. LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 8

Static and Dynamic Typing Type checking: Simple, automatically executable form of proof for program correctness (in certain limited respects) Avoids operations to be applied to unsuitable arguments Static typing: Type information is checked before execution of program (at compile time) Program code has to specify (explicitly or implicitly) types for all variables Examples: Java, Pascal, C, Standard ML Dynamic typing: Type information is checked during execution of program (at run time) Type information for variables only exists after value assignment Examples: Smalltalk, Python, JavaScript In practice, static and dynamic tying are sometimes mixed: See the dynamic type check for downcast operations in Java LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 9

Strong and Weak Typing Surprisingly ill-defined terms Do not take this classification too serious Strong typing: Basic idea: Strong typing provides no (or only very limited) possibility to evade the restrictions of the type system Examples of strongly typed languages: Java, Pascal, Standard ML, Python Weak typing: Implicit type conversions Type conversions with undefined result Examples of weakly typed languages: Visual Basic, C, JavaScript LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 10

Duck When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck. James Whitcomb Riley The type of an object is determined only by the fact whether it has the features required from it. Appropriate for object-oriented programming languages with dynamic types - like Python. LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 11

String Operations in Python Operations valid for all sequence types: Indexing: str[5] (str is the string object) Negative indexing: str[-5] (counting from the end) Slicing: str[2:5], str[:5], str[2:6:2], str[::-1] Omitted index is begin or end, third value is step size (covers reversion) Operations: len(str), min(str), max(str), x in str Numerous methods specific for strings like: capitalize() count(substr) find(substr) isalpha() partition(sep) replace split(sep) upper() title() LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 12

Lists in Python List: Sequential collection of objects (of arbitrary, also varying type) Can be easily used as stack or queue data structures Flexible creation of lists e.g. by list comprehension: l = [3*x for x in range(1,4)] Lists are mutable (can be even changed through slices) List methods: append count extend index insert pop remove reverse sort LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 13

Sets in Python Set: Unordered collection without duplicates Constructor set builds a set from a list Basic mathematical operations for sets: Union ( ) Intersection (&) Difference (-) Symmetric difference (^) Example: set('multimedia') & set('programming') LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 14

Java to Python: Imperative Example def sequentialsearch (q, a): return q in a a = [11, 22, 33, 44, 55, 66] print a print "Array a: ", a print "Search for 55: ",sequentialsearch(55,a) print "Search for 23: ",sequentialsearch(23,a) LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 15

Tuples and Dictionaries in Python Tuple: immutable collection of objects (of arbitrary type) N = ('max','muster') N = 'max','muster' Strange: One-element tuple written as 'max', Easy unpacking of tuples: vorname, nachname = ('max','muster') Dictionary: Mutable collection of object maps (of arbitrary type) age = {'anna':23, 'max':22} Key entries can only be of immutable type (strings, numbers, tuples) Key entries must be hashable Main purpose: indexed access age['anna'] Constructor accepts lists or generator expressions: dict((x, x*x) for x in range(0,5)) LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 16

Java to Python: Object-Oriented Example public class Counter { private int k = 0; public void count () { k++; } public void reset () { k = 0; } public int getvalue () { return k; } } LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 17

Java to Python: Object-Oriented Example (Python) class Counter: def init (self): self.k = 0 def count(self): self.k += 1 def reset(self): self.k = 0 def getvalue(self): return self.k Initialization (constructor) Instance variable k Self parameter is implicit in method calls but explicitly mentioned in declaration LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 18

Constructing Objects, Invoking Methods Example: c = Counter() print c.getvalue() c.count() c.count() c.count() print c.getvalue() LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 19

Inheritance in Python class LimitCounter(Counter): def init (self, limit): self.k = 0 self.limit = limit def count(self): if self.k = self.limit: self.k += 1 In contrast to Java, Python allows multiple inheritance LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 20

Python Modules Module: A file containing Python definitions and statements File name is module name with suffix.py Module name is available as global variable name Statements in a module are executed when the module is imported (initialization) Importing a module m: import m Accessing a definition f() in m: m.f() from m import * Accessing a definition f() in m: f() LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 21

Why Python in This Lecture? Python is not a specific multimedia language We will use a simple Python-binding for a multimedia/gaming framework Generally, knowing Python is a good thing to get programming tasks done easily. LMU München, Sommer 2014 Prof. Hußmann: Multimedia-Programmierung Kapitel 2 Teil a, Folie 22