Python Useful Functions and Methods

Slides:



Advertisements
Similar presentations
Python Regrets OSCON, July 25, 2002
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Dictionaries: Keeping track of pairs
Sequences A sequence is a list of elements Lists and tuples
Introduction to Python (for C++ programmers). Background Information History – created in December 1989 by Guido van Rossum Interpreted Dynamically-typed.
October 4, 2005ICP: Chapter 4: For Loops, Strings, and Tuples 1 Introduction to Computer Programming Chapter 4: For Loops, Strings, and Tuples Michael.
Lists in Python.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Built-in Data Structures in Python An Introduction.
Iterators, Linked Lists, MapReduce, Dictionaries, and List Comprehensions... OH MY! Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Python Lists and Sequences Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark List properties What are lists? A list is a mutable.
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Quiz 4 Topics Aid sheet is supplied with quiz. Functions, loops, conditionals, lists – STILL. New topics: –Default and Keyword Arguments. –Sets. –Strings.
Fundamentals of Programming I Higher-Order Functions
Lesson 06: Functions Class Participation: Class Chat:
Higher Order Functions
Advanced Python Idioms
Sharing, mutability, and immutability
Lecture 3 Python Basics Part 2.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Python Comprehension and Generators
Intro To Pete Alonzi University of Virginia Library
Department of Computer Science,
C++ Standard Library.
Python Useful Functions and Methods
Core libraries Scripts, by default only import sys (various system services/functions) and builtins (built-in functions, exceptions and special objects.
Lecture 10 Data Collections
Java Algorithms.
LING 388: Computers and Language
CISC101 Reminders Quiz 2 this week.
More Loop Examples Functions and Parameters
CHAPTER THREE Sequences.
CISC101 Reminders Slides have changed from those posted last night…
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Test Automation For Web-Based Applications
CSC1018F: Intermediate Python
Ruth Anderson UW CSE 160 Winter 2017
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Topics Sequences Introduction to Lists List Slicing
Python Advanced Data Structures
Python Primer 1: Types and Operators
Computer Science 111 Fundamentals of Programming I
Lambda Functions, MapReduce and List Comprehensions
Advanced Python Idioms
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Python Comprehension and Generators
Python Sets and Dictionaries
Advanced Python Idioms
Ruth Anderson CSE 160 University of Washington
Topics Sequences Introduction to Lists List Slicing
Ruth Anderson UW CSE 160 Spring 2018
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
More Basics of Python Common types of data we will work with
Python Simple file reading
Python Useful Functions and Methods
Sample lecture slides.
Introduction to PYTHON
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Python Useful Functions and Methods Peter Wad Sackett

Nothing more to learn ? There are quite a few built-in functions not mentioned yet, see https://docs.python.org/3/library/functions.html So far has been mentioned 14 out of 68: dict(), float(), format(), input(), int(), len(), list(), open(), print(), range(), reversed(), set(), sorted(), str() and tuple() Many of the unmentioned functions works with python’s object model or the inner workings of python. Useful for advanced python code. Classes for making your own python objects, with methods working on them.

Nice to know built-in functions The following are nice to know for a beginner: abs(number) returns absolute value of a number dir(object) returns info on an object enumerate(iterable) returns an enumerated list of tuples help(string) returns help in the subject given id(object) returns the id of an object, guaranteed to be unique iter(iterable) returns an iterable – dicts as key/value tuples max(iterable) returns the maximum value min(iterable) returns the minimum value round(number) rounds a number sum(iterable) returns the sum of the iterable zip(iterables) returns a list of tubles of the input in ”pairs”

Python concepts on iteration This overview of various iteration concepts will be explored on the next slides. Figure by Vincent Driessen

Concepts A container is ”holding data” – typically lists, sets, dict or strings. The key factor is the container can be queried for membership of an element – does it contain the element. An iterable is often a container but can also be an open file or other object that can return an iterator. An iterator is stateful helper object. It keeps track of the iteration over the iterable. It can be created by using the iter() function call on an iterable. # The container, but also the iterable numbers = [1, 2, 3] # Creating two iterators iter1 = iter(numbers) iter2 = iter(numbers) # Using the next function call next(iter1) # gives 1 next(iter1) # gives 2 next(iter2) # gives 1 next(iter1) # gives 3 next(iter1) # raises the exception StopIteration

Lambda, map and filter – equal to comprehension The lambda is a small anonymous function which can be put any place, where a function is required. Here shown with method sort: names = [’Aiz’, ’Cain’, ’Gemini’, ’Zenia’] # The names shold be sorted according to the last letter names.sort(key=lambda x: x[-1]) # Result: [’Zenia’, ’Gemini’, ’Cain’, ’Aiz’] The generel syntax of the lambda is: lambda argument_list: expression lambda x,y: x*y-4 The map() function returns an iterator. Lambda is often used here. reverse_names = list(map(lambda x: x[::-1], names)) The filter() function is choosing which elements to keep. a_names = list(filter(lambda x: ’a’ in x, names))

Built-in data types A useful sequence method: sequencetype.count(item) returns the number of occurrences New data types: bytes, bytearray and frozenset Bytes is similar to strings; immutable and supports all string methods. Efficient, supports only the ascii charset, seems like a list of numbers. name = b’Peter’ print(name[0]) #result: 80 Bytearrays are like mutable strings and supports all string methods. They are more efficient when your strings change. name = bytearray(’Peter’) Frozensets are immutable sets and supports all set methods, that does not change the frozenset, obviously. Can be used as keys in dicts and sets. Frozensets are to sets, what tuples are to lists. fruits = frozenset(’apple’, ’banana’, ’cherry’, ’date’)

Standard libraries Python has many standard libraries, far too many to memorize. https://docs.python.org/3/library/ Each library contains several functions, all relevant and appropriate to the core functionality of the library. You won’t find string functions in the math library. Learn to find what you need from the list. In the course the following standard libraries have been used: string – string operations re – regular expressions math – mathmatical functions os – operating system interfaces sys – system specific functions Some new ones which are surprisingly often useful: random – pseudo random numbers copy – shallow and deep copy operations