Python: Short Overview and Recap

Similar documents
Computerlinguistische Anwendungen Python/Git

Introduction to Python! Lecture 2

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Control of Flow. There are several Python expressions that control the flow of a program. All of them make use of Boolean conditional tests.

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Physics 514 Basic Python Intro

A Little Python Part 2

Babu Madhav Institute of Information Technology, UTU 2015

Lecture 7: Python s Built-in. in Types and Basic Statements

Data Structures. Dictionaries - stores a series of unsorted key/value pairs that are indexed using the keys and return the value.

Practical Workbook Computer Programming

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program:

CSC148 Fall 2017 Ramp Up Session Reference

MEIN 50010: Python Data Structures

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

CIS192 Python Programming

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Script language: Python Data structures

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Introduction to Bioinformatics

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Python Mini Lessons last update: May 29, 2018

LECTURE 3 Python Basics Part 2

Senthil Kumaran S

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

STA141C: Big Data & High Performance Statistical Computing

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Lists, Tuples and Dictionaries. HORT Lecture 10 Instructor: Kranthi Varala

Worksheet 6: Basic Methods Methods The Format Method Formatting Floats Formatting Different Types Formatting Keywords

Variable and Data Type I

[Software Development] Python (Part A) Davide Balzarotti. Eurecom Sophia Antipolis, France

Programming to Python

Sequences and iteration in Python

Python Programming: Lecture 2 Data Types

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Python a modern scripting PL. Python

Python for Finance. Control Flow, data structures and first application (part 2) Andras Niedermayer

CS Summer 2013

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

CPSC 217 Midterm (Python 3 version)

Python Tutorial. Day 1

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Python Tutorial. CSE 3461: Computer Networking

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

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

ENGR 101 Engineering Design Workshop

6. Data Types and Dynamic Typing (Cont.)

Introduction to Python (All the Basic Stuff)

Today: Revisit some objects. Programming Languages. Key data structure: Dictionaries. Using Dictionaries. CSE 130 : Winter 2009

CPSC 217 Midterm (Python 3 version)

Variable and Data Type I

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

Topic 7: Lists, Dictionaries and Strings

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Play with Python: An intro to Data Science

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Programming in Python

Intro to Strings. CSE 231 Rich Enbody. String: a sequence of characters. Indicated with quotes: or " " 9/11/13

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited

Sequence Types FEB

Lists How lists are like strings

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

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

CS Advanced Unix Tools & Scripting

Recap from last Python lecture

Introduction to Programming in Python (1)

Download Python from Any version will do for this class

Introduction to Python

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017

And Parallelism. Parallelism in Prolog. OR Parallelism

MITOCW watch?v=rvrkt-jxvko

18.1. CS 102 Unit 18. Python. Mark Redekopp

CS S-02 Python 1. Most python references use examples involving spam, parrots (deceased), silly walks, and the like

PYTHON CONTENT NOTE: Almost every task is explained with an example

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

Python. Karin Lagesen.

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

61A LECTURE 15 MEMOIZATION, RECURSIVE DATA, SETS

Lecture #12: Mutable Data. Last modified: Sun Feb 19 17:15: CS61A: Lecture #12 1

Python source materials

Final Exam(sample), Fall, 2014

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

CS 116 Fall 2016 Review Notes Created By: Rina Dong

EE 355 Unit 17. Python. Mark Redekopp

Introduction to Python

CSC326 Python Sequences i. CSC326 Python Sequences

Introduction to Python

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

Introduction to Python

ENGR 102 Engineering Lab I - Computation

Python. Department of Computer Science And Engineering. European University Cyprus

Exercise: The basics - variables and types

(CC)A-NC 2.5 by Randall Munroe Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Transcription:

Python: Short Overview and Recap Benjamin Roth CIS LMU Benjamin Roth (CIS LMU) Python: Short Overview and Recap 1 / 39

Data Types Object type Example creation Numbers (int, float) 123, 3.14 Strings this class is cool Lists [1, 2, [1, 2]] Dictionaries { 1 : abc, 2 : def } Tuples (1, Test, 2) Files open( file.txt ), open( file.bin, wb ) Sets set( a, b, c ) Others boolean, None Program unit types functions, modules, classes Benjamin Roth (CIS LMU) Python: Short Overview and Recap 2 / 39

Variables store data, e.g., numbers content can be changed (is variable) have a data type assignment: var_name = value, e.g., num = 17 Benjamin Roth (CIS LMU) Python: Short Overview and Recap 3 / 39

Dynamic Typing dynamic typing model types are determined automatically at runtime type of a variable can change check type with type(var) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 4 / 39

Number data types integers, floating-point numbers, complex numbers, decimals, rationals Numbers support the basic mathematical operations, e.g.: + addition *, /, // multiplication, division >>> 1/4 >>> 1//4 ** exponentiation <, >, <=, >= comparison!=, == (in)equality Benjamin Roth (CIS LMU) Python: Short Overview and Recap 5 / 39

String data types Immutable sequence of single characters s1="first line\nsecond line" s2=r"first line\nstill first line" s3="""first line second line""" s4='using different quotes' How to create the following two-line string? what s up, "dude"? -Bob Benjamin Roth (CIS LMU) Python: Short Overview and Recap 6 / 39

String operations I s1 = 'the' Operation Description Output len(s1) length of the string 3 s1[0] indexing, 0-based 't' s1[-1] backwards indexing 'e' s1[0:3] slicing, extracts a substring 'the' s1[:2] slicing, extracts a substring 'th' s1 + ' sun' concatenation 'the sun' s1 * 3 repetition 'thethethe'!=, == (in)equality True, False Benjamin Roth (CIS LMU) Python: Short Overview and Recap 7 / 39

String operations II s1 = 'these' Operation Description Output '-'.join(s1) concatenate (delimiter: - ) 't-h-e-s-e' s1.find('se') finds start of substring 3 s1.replace('ese', 'at') replace substrings 'that' s1.split('s') splits at string ['the','e'] s1.upper() upper case 'THESE' s1.lower() lower case 'these' Benjamin Roth (CIS LMU) Python: Short Overview and Recap 8 / 39

Lists collection of arbitrarily typed objects mutable positionally ordered no fixed size initialization: l = [123, 'spam', 1.23] empty list: l = [] Benjamin Roth (CIS LMU) Python: Short Overview and Recap 9 / 39

List operations I l = [123, 'spam', 1.23] Operation Description Output len(l) length of the list 3 l[1] indexing, 0-based 'spam' l[0:2] slicing, extracts a sublist [123, 'spam'] l + [4, 5, 6] concatenation [123, 'spam', 1.23, 4, 5, 6] l * 2 repetition [123, 'spam', 1.23, 123, 'spam', 1.23] Benjamin Roth (CIS LMU) Python: Short Overview and Recap 10 / 39

List operations II l = [123, 'spam', 1.23] Operation Description Output l.append('ni') append to the end [123, 'spam', 1.23, 'NI'] l.pop(2) remove item [123, 'spam'] l.insert(0, 'aa') insert item at index ['aa', 123, 'spam', 1.23] l.remove(123) remove given item ['spam', 1.23] l.reverse() reverse list (in place) [1.23, 'spam', 123] l.sort() sort list (in place) [1.23, 123, 'spam'] Benjamin Roth (CIS LMU) Python: Short Overview and Recap 11 / 39

Nested lists Let us consider the 3x3 matrix of numbers M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. M is a list of 3 objects, which are in turn lists as well and can be referred to as rows. M[1] returns the second row in the main list: [4, 5, 6] M[1][2] returns the third object situated in the in the second row of the main list: 6 Benjamin Roth (CIS LMU) Python: Short Overview and Recap 12 / 39

Dictionaries Dictionaries are mappings, not sequences They represent a collection of key:value pairs Example: d = {'food':'spam', 'quantity':4, 'color':'pink'} Efficient access ( constant time): what is the value associated with a key? They are mutable like lists: Key-value pairs can be added, changed, and removed Keys need to be immutable why? Benjamin Roth (CIS LMU) Python: Short Overview and Recap 13 / 39

Dictionary operations I >>> d = {'food':'spam', 'quantity':4, 'color':'pink'} >>> d['food'] #Fetch value of key 'food' 'Spam' >>> d['quantity'] += 1 #Add 1 to the value of 'quantity' >>> d d = {'food':'spam', 'quantity':5, 'color':'pink'} Benjamin Roth (CIS LMU) Python: Short Overview and Recap 14 / 39

Dictionary operations II >>> d = {} >>> d['name'] = 'Bob' >>> #Create keys by assignment >>> d['job'] = 'researcher' >>> d['age'] = 40 >>> d d = {'name':'bob', 'job':'researcher', 'age':40} >>> print(d['name']) Bob Benjamin Roth (CIS LMU) Python: Short Overview and Recap 15 / 39

Dictionary operations III >>> #Alternative construction techniques: >>> d = dict(name='bob', age=40) >>> d = dict([('name', 'Bob'), ('age', 40)]) >>> d = dict(zip(['name', 'age'], ['Bob', 40])) >>> d {'age': 40, 'name': 'Bob'} >>> #Check membership of a key >>> 'age' in d True >>> d.keys() #Get keys ['age', 'name'] >>> d.values() #Get values [40, 'Bob'] >>> d.items() #Get all keys and values [('age', 40), ('name', 'Bob')] >>> len(d) #Number of entries 2 Benjamin Roth (CIS LMU) Python: Short Overview and Recap 16 / 39

Dictionary operations IV >>> d = {'name': 'Bob'} >>> d2 = {'age': 40, 'job': 'researcher'} >>> d.update(d2) >>> d {'job': 'researcher', 'age': 40, 'name': 'Bob'} >>> d.get('job') 'researcher' >>> d.pop('age') 40 >>> d {'job': 'researcher', 'name': 'Bob'} Benjamin Roth (CIS LMU) Python: Short Overview and Recap 17 / 39

Tuples Sequences like lists but immutable like strings Used to represent fixed collections of items >>> t = (1, 2, 3, 4) #A 4-item tuple >>> len(t) #Length 4 >>> t + (5, 6) #Concatenation (1, 2, 3, 4, 5, 6) >>> t[0] #Indexing, slicing and more 1 >>> len(t)??? Benjamin Roth (CIS LMU) Python: Short Overview and Recap 18 / 39

Sets Mutable Unordered collections of unique and immutable objects Efficient check ( constant time), whether object is contained in set. >>> set([1, 2, 3, 4, 3]) {1, 2, 3, 4} >>> set('spaam') {'a', 'p', 's', 'm'} >>> {1, 2, 3, 4} {1, 2, 3, 4} >>> S = {'s', 'p', 'a', 'm'} >>> S.add('element') >>> S {'a', 'p', 's', 'm', 'element'} Benjamin Roth (CIS LMU) Python: Short Overview and Recap 19 / 39

Sets >>> s1 = set(['s', 'p', 'a', 'm', 'element']) >>> 'element' in s1 True >>> 'spam' in s1 False >>> s2 = set('ham') >>> s1.intersection(s2) {'m', 'a'} >>> s1.union(s2) {'s', 'm', 'h', 'element', 'p', 'a'} intersection and union return a new set, the original sets stay unchanged Benjamin Roth (CIS LMU) Python: Short Overview and Recap 20 / 39

Immutable vs. Mutable Immutable: numbers strings tuples Mutable: lists dictionaries sets newly coded objects Benjamin Roth (CIS LMU) Python: Short Overview and Recap 21 / 39

Control flow: if-statements >>> x = 'killer rabbit'... if x == 'roger':... print('shave and a haircut')... elif x == 'bugs':... print('whats up?')... else:... print('run away!') run away! Note! The elif statement is the equivalent of else if in Java or elsif in Perl. Benjamin Roth (CIS LMU) Python: Short Overview and Recap 22 / 39

Control flow: While loops >>> while True:... print('type Ctrl-C to stop me!')... >>> x == 'spam'... while x: #while x is not empty... print(x)... x = x[1:]... spam pam am m x[len(x):len(x)] returns the empty string. Benjamin Roth (CIS LMU) Python: Short Overview and Recap 23 / 39

Control flow: For loops The for loop is a generic iterator in Python: it can step through the items in any ordered sequence or other iterable objects (strings, lists, tuples, and other built-in iterables, as well as new user-defined iterables). l = [1, 2, 3, 4] for i in l: print(i) for i in range(0, 5): print(i) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 24 / 39

Files: Read file line by line file_name = '/path/to/file.txt' with open(file_name, mode='r') as f: for line in f.readlines(): # Lines still contain line-break. # Print without newline: print(line, end='') How to remove trailing new line? Benjamin Roth (CIS LMU) Python: Short Overview and Recap 25 / 39

Files: Write file line by line file_name = '/path/to/file.txt' lines = ['line1', 'second line', 'another line', 'last one'] with open(file_name, mode='w') as f: for line in lines: f.write(line + '\n') Benjamin Roth (CIS LMU) Python: Short Overview and Recap 26 / 39

Functions A function is a device that groups a set of statements so they can be run more than once in a program Why use functions? Maximizing code reuse and minimizing redundancy Procedural decomposition Benjamin Roth (CIS LMU) Python: Short Overview and Recap 27 / 39

Defining functions def name(arg1, arg2,..., argn): statements def name(arg1, arg2,..., argn):... return value >>> def times(x, y):... return x*y... >>> times(2, 5) 10 Benjamin Roth (CIS LMU) Python: Short Overview and Recap 28 / 39

Function objects def func(): #Create function object... func() # Call object. func.attr = value # Attach attributes. Benjamin Roth (CIS LMU) Python: Short Overview and Recap 29 / 39

Module Packaging of program code and data for reuse Provides self contained namespaces that avoid variable name clashes across programs The names that live in a module are called its attributes one Python file one module Some modules provide access to functionality written in external languages such C++ or Java. (wrappers) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 30 / 39

Module import Lets a client (importer) fetch a module as a whole from Allows clients to fetch particular names from a module Benjamin Roth (CIS LMU) Python: Short Overview and Recap 31 / 39

Module imports Benjamin Roth (CIS LMU) Python: Short Overview and Recap 32 / 39

Regular expressions A regular expression is an algebraic formula (a pattern) that represents a set of strings. Can be used to search for strings that match the pattern. Regex Strings a a ab ab a* ɛ, a, aa, aaa,... a*b* ɛ, a, b, aa, ab, bb,..., aaaaaab,... Benjamin Roth (CIS LMU) Python: Short Overview and Recap 33 / 39

Regular Expressions What can you match with the following regular expressions? 1 '^[Tt]the\b.*' 2 '[:;]-?[\ oppd\)\(]' 3 '<.*?>' 4 '\d +\-year\-old' Documentation: https://docs.python.org/3/library/re.html Test your regex online: https://pythex.org/ Benjamin Roth (CIS LMU) Python: Short Overview and Recap 34 / 39

Regular Expressions To use Regular Expressions in Python, import the module re Then, there are two basic ways that you can use to match patterns: re.match(): Finds match of pattern at the beginning of a string re.search(): Finds match of pattern anywhere in a string re.match() Both return a match object, that stores more information about the match, and None when there is no match. Benjamin Roth (CIS LMU) Python: Short Overview and Recap 35 / 39

Regular Expressions import re wordlist = ['farmhouse', 'greenhouse', 'guesthouse'] for w in wordlist: if re.match('(g.*?)(?=house)', w): print(w) match = re.search(pattern, string) if match: match_str = match.group(0) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 36 / 39

Compiling regular expressions If the same regular expression is used repeatedly (in a loop), it is more efficient to compile it outside of the loop. import re wordlist = ['farmhouse', 'greenhouse', 'guesthouse'] regex = re.compile('(g.*?)(?=house)') for w in wordlist: if regex.match(w): print(w) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 37 / 39

Python classes class Classifier: def init (self, lambda1, lambda2): self.l1 = lambda1 self.l2 = lambda2 def train(self, data):... def test(self, data):... if name = ' main ': data = 'This is training data' testdata = 'This is test data' lambda1 = 0.002 lambda2 = 0.0005 model = Classifier(lambda1, lambda2) model.train(data) model.test(testdata) Benjamin Roth (CIS LMU) Python: Short Overview and Recap 38 / 39

Summary Data types: numbers, strings, tuples, lists, dictionaries Mutable / Immutable If-statement, while-loop, for-loop Reading / writing from files Functions Importing modules Regular expressions Any questions? Benjamin Roth (CIS LMU) Python: Short Overview and Recap 39 / 39