CS 234 Python Review Part 2

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

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

Fundamentals of Programming (Python) Object-Oriented Programming. Ali Taheri Sharif University of Technology Spring 2018

Iterators & Generators

Dictionaries. Upsorn Praphamontripong. CS 1111 Introduction to Programming Spring 2018

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

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

Collections. Lists, Tuples, Sets, Dictionaries

CME 193: Introduction to Scientific Python Lecture 4: File I/O and Classes

Python for Non-programmers

Python Tutorial. Day 1

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

Basic Python Revision Notes With help from Nitish Mittal

Python debugging and beautification

Try and Error. Python debugging and beautification

Babu Madhav Institute of Information Technology, UTU 2015

Types, lists & functions

The Practice of Computing Using PYTHON

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

Notebook. March 30, 2019

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

[301] JSON. Tyler Caraza-Harter

Python memento TI-Smart Grids

Object Oriented Programming

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Beyond Blocks: Python Session #1

MEMOIZATION, RECURSIVE DATA, AND SETS

Introduction to Python. Fang (Cherry) Liu Ph.D. Scien5fic Compu5ng Consultant PACE GATECH

Data Structures. Lists, Tuples, Sets, Dictionaries

Review Sheet for Midterm #1 COMPSCI 119 Professor William T. Verts

Data Handing in Python

APT Session 2: Python

Object Oriented Programming

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

ENGR 102 Engineering Lab I - Computation

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

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

Data Types and Conversion

Computational Concepts Toolbox. Object Oriented Programming. Today: class. Review: Objects

MEIN 50010: Python Data Structures

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

Introduction to Python, Cplex and Gurobi

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

Python. Karin Lagesen.

COMPSCI 105 S Principles of Computer Science. Classes 3

PYTHON DATA SCIENCE TOOLBOX II. List comprehensions

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

Object Oriented Programming

Python. Objects. Geog 271 Geographic Data Analysis Fall 2010

Introduction to Python for Plone developers

Python Review IPRE

Exceptions & a Taste of Declarative Programming in SQL

List of squares. Program to generate a list containing squares of n integers starting from 0. list. Example. n = 12

(IUCAA, Pune) kaustubh[at]iucaa[dot]ernet[dot]in.

Python - Variable Types. John R. Woodward

Software Development Python (Part B)

Module 09: Additional Options for Organizing Data

Introduction to Python

Python Intro GIS Week 1. Jake K. Carr

Chapter 8 : Computer Science. Datastructures: Class XII ( As per CBSE Board) lists, stacks, queues. Visit : python.mykvs.in for regular updates

Programming to Python

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

CMSC 201 Fall 2015 Lab 12 Tuples and Dictionaries

Key Differences Between Python and Java

University of Texas at Arlington, TX, USA

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

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

Chapter 10: Strings and Hashtables

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

CME 193: Introduction to Scientific Python Lecture 6: Classes and iterators

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

Python Review IPRE

Python Programming Language

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

CSC102 INTRO TO PROGRAMMING WITH PYTHON REVIEW MICHAEL GROSSBERG

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

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

ENGR/CS 101 CS Session Lecture 12

CME 193: Introduction to Scientific Python Lecture 4: Strings and File I/O

Java+- Language Reference Manual

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

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

Topic 7: Lists, Dictionaries and Strings

61A Lecture 21. Friday, March 13

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

COMP519 Web Programming Lecture 21: Python (Part 5) Handouts

CSC326 Python Sequences i. CSC326 Python Sequences

Overview.

Dictionaries. Looking up English words in the dictionary. Python sequences and collections. Properties of sequences and collections

Dictionaries. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, KV 5 Jaipur II Shift

Structure and Interpretation of Computer Programs

LECTURE 3 Python Basics Part 2

Variable and Data Type 2

DSC 201: Data Analysis & Visualization

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

Here n is a variable name. The value of that variable is 176.

PREPARING FOR THE FINAL EXAM

Transcription:

CS 234 Python Review Part 2

Recap import function: define, return boolean, conditional, branching loop: for, range, while file: open, close, readlines string: split

Classes Define blueprint for a custom data type. Have all of the data types wrapped under one name. Important to have (self) as an argument to all methods in Python classes. self is the instance itself. Access class methods and attributes by the dot (. ) operator.

Classes: example Class CS234Students Name Program Year Midterm Marks Final Marks Assignment Marks

Classes: example

Classes: methods XXX: public method XXX: protected method XXX: private method

Classes: methods class Test1: def public method(self): print( This is a public method ); def protected method(self): print( This is a protected method ); def private method(self): print( This is a private method ); def call private(self): self. private method(); class Test2(Test1): def call protected(self): self. protected method();

Classes: magic methods init : called whenever a new class object is created. It is optional to have it in the program. (You are supposed to include this in your assignment.)... contains (xx): membership test, called by xx in object. len : returns length, called by len(object).

Classes: some useful magic methods Definition Usage equality object. eq (self,other) object==other addition object. add (self,other) object+other subtraction object. sub (self,other) object-other multiplication object. mul (self,other) object*other division object. truediv (self,other) object/other absolute value object. abs (self) abs(object) the official string object. repr (self) repr(object) the informal string object. str (self) str(object)

Recursion

Recursion: formally The solution to a problem depends on solutions to smaller instances of the same problem. Achieved by a function calling itself. It is important to have a base case, so that the recursion terminates at some point.

Recursion: example Fibonacci sequence: 1, 1, 2, 3, 5, 8,... def fibonacci(n): if n==0 or n==1: return 1; else: return fibonacci(n-1)+fibonacci(n-2); for i in range(0,10): print(fibonacci(i));

Lists List of comma-separated values (items) between square brackets. Items in the list can be of different types. Access and update items by indices in square brackets. list=[1,2,3,4, cat, dog,5,6,7]; print( list[0] :,list[0]); print( list[1] :,list[1]); list[3]= rat ; print( list[0:1] :,list[0:1]); print( list[2:5] :,list[2:5]);

Lists: operations append: adds a single element to the end of the list. extend: adds the elements in another list to the end of the list. do not return the new list, just modifies the original. list1=[1,2,3,4]; list1.append([5,6]); print( list1:,list1); list2=[1,2,3,4]; list2.extend([5,6]); print( list2:,list2);

Lists: + a=[1,2]; b=[3,4]; print(a+b);

Tuples Tuples are like lists except that once created, they cannot be changed. Tuples use round brackets. It s possible to add 2 tuples: a=(1,2, rat ); b=(5, cat, dog ); print(a+b);

Dictionaries A dictionary in Python is a group of key-value pairs. Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. Very useful when we want a mapping between 2 entities in O(1) time.

Dictionaries # create a dictionary dict={5: cat,8: dog } print(dict); # add a key-value pair dict[3]= mouse ; print(dict); # remove a key-value pair del dict[3]; print(dict); # access a value print(dict[5]); # modify a value dict[8]= hot dog ; print(dict);

That s All