Python Useful Functions and Methods

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Dictionaries: Keeping track of pairs
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Sequences A sequence is a list of elements Lists and tuples
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
UW CSE 190p Section 8/2, Summer 2012 Dun-Yu Hsiao.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Python’s Standard Library Part I Joe Houpert CS265.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
Python I/O Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Classic file reading 1 infile = open(’filename.txt’, ’r’) for line.
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.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Python Advanced Data Structures Peter Wad Sackett.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Lesson 06: Functions Class Participation: Class Chat:
CMPT 120 Topic: Python’s building blocks -> More Statements
Advanced Python Idioms
Sharing, mutability, and immutability
Lecture 3 Python Basics Part 2.
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Python Comprehension and Generators
CS 100: Roadmap to Computing
Python Data Structures & Algorithms in Python
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
LING 388: Computers and Language
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
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…
LING/C SC/PSYC 438/538 Lecture 6 Sandiway Fong.
Python Useful Functions and Methods
CISC101 Reminders Quiz 2 this week.
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Test Automation For Web-Based Applications
Variables and Expressions
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Python Advanced Data Structures
Sharing, mutability, and immutability
Lesson 10: Dictionaries Class Chat: Attendance: Participation
Reference semantics, variables and names
CS1110 Today: collections.
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
CS 100: Roadmap to Computing
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Winter 2019 CISC101 5/30/2019 CISC101 Reminders
Introduction to Dictionaries
More 2D Array and Loop Examples Functions and Parameters
More Basics of Python Common types of data we will work with
Python Simple file reading
Python Useful Functions and Methods
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sample lecture slides.
Introduction to PYTHON
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() & tuple() Many of the unmentioned functions works with python’s object model or the inner workings of python. Useful for advanced python code.

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 map(function, iterable) the function is applied on the iterable 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”

Built-in data types A useful sequence method: sequencetype.count(item) returns the number of occurences New data types: bytearray and frozenset 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 appropiate 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