CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

Similar documents
61A Lecture 2. Friday, August 28, 2015

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

CMSC 150 LECTURE 7 RECURSION

Python for C programmers

61A Lecture 3. Friday, September 5

61A Lecture 4. Monday, September 9

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Scope. Chapter Ten Modern Programming Languages 1

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

What is an algorithm?

Recursive Definitions

Reading 8 : Recursion

CSE : Python Programming

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Recursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!

APCS-AB: Java. Recursion in Java December 12, week14 1

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

To figure this out we need a more precise understanding of how ML works

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Python Boot Camp. Day 3

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

Chapter 5 Names, Binding, Type Checking and Scopes

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Algorithm. Building blocks of algorithm

Accelerating Information Technology Innovation

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSC148 winter stools, names, tracing week 5

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Lecture 24 Tao Wang 1

(Refer Slide Time: 00:51)

Recursion. Chapter 5

CMPT 120 Basics of Python. Summer 2012 Instructor: Hassan Khosravi

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

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

CSC-140 Assignment 4

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Decision-Making and Repetition

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Chapter 15: Recursion

The Stack, Free Store, and Global Namespace

Recursion continued. Programming using server Covers material done in Recitation. Part 2 Friday 8am to 4pm in CS110 lab

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

1.7 Recursion. Department of CSE

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

MITOCW watch?v=0jljzrnhwoi

Python Programming. Introduction Part II

Getting to places from my house...

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

UNIT 5A Recursion: Basics. Recursion

CmpSci 187: Programming with Data Structures Spring 2015

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

CS 2505 Computer Organization I

Programming Languages

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CSE : Python Programming

Functions and Recursion

Next: What s in a name? Programming Languages. Data model in functional PL. What s in a name? CSE 130 : Fall Lecture 13: What s in a Name?

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

More Complicated Recursion CMPSC 122

Solving problems by recursion

Lecture P6: Recursion

Recursion. Recursion in C

PREPARING FOR PRELIM 1

Writing Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

EC 413 Computer Organization

CSC Software I: Utilities and Internals. Programming in Python

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

JAVA: A Primer. By: Amrita Rajagopal

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS 206 Introduction to Computer Science II

CS1 Lecture 22 Mar. 6, 2019

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Prolog Cut: Summary. CSC324 Logic & Relational Programming Illustrated in Prolog. Prolog Cut: Summary. Non-deterministic Programming

Lecture 16: Static Semantics Overview 1

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

The SPL Programming Language Reference Manual

CSE200 Lecture 6: RECURSION

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

Chapter 10 Implementing Subprograms

Subset sum problem and dynamic programming

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Structure and Interpretation of Computer Programs Spring 2014 Final (with corrections)

CSC 8301 Design and Analysis of Algorithms: Recursive Analysis

2: Complexity and Recursion

Lecture #3: Recursion

Pace University. Fundamental Concepts of CS121 1

Transcription:

CSC 148 Lecture 3 Dynamic Typing, Scoping, and Namespaces Recursion

Announcements Python Ramp Up Session Monday June 1st, 1 5pm. BA3195 This will be a more detailed introduction to the Python language than we have time to do in class. Attendance is optional, and recommended if you are struggling with the Python language. Assignment 2 will be posted Friday afternoon.

Names vs. Values A name (e.g. variable) is just a way of identifying some object (value) that you want to keep track of For example, I may assign the value Indiana Jones or Star Trek to the variable named movie. movie is the name of a variable Indiana Jones and Star Trek are the string objects that can be assigned to the variable.

Defining Variables in Python Variables are never explicitly defined like in other languages A variable is created when you first assign a value to it variable x does not yet exist print x print hello world x = 5 print x x = 7 variable x defined for the first time

Dynamic Typing Python is a dynamically typed language type checking is done at runtime You don't have to declare the type of a variable statically when you write your code. This is different from languages like Java, C, C++.

Python Variables and Dynamic Typing Variables have no type information associated with them But... if this is true, how does the following TypeError happen? >>> x= 5 >>> y = "hello" >>> x + y Traceback (most recent call last): File "<string>", line 1, in <string> TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python Variables and Dynamic Typing Type information is associated with the object stored in the variable. variable (name) object (value) x references 5 integer y references hello string

Python Variables and Dynamic Typing Whenever a variable appears in your code, it is replaced by the object (value) that it is referencing. >>> x= 5 >>> y = "hello" >>> x + y When your program executes, Python sees this as 5 + hello

What's in a name? A variable is essentially a name (label, identifier). You assign values to a variable (name) by using assignment statements This is technically known as name binding. You are binding an object value to a name Variables are not the only entities in Python that have names. There's also: classes, methods, modules, functions

Functions The def statement is an executable statement. When it's executed, the name of the function is bound to the function definition (i.e., the function object) def func(): # do stuff here

Modules and Classes import statements bind a module object to a module name class statements bind a class object to a class name Terminology alert: do not to confuse class object with an object that is an instance of some class.

Hang on a second... If a class definition (or function definition, import statement, etc) is an executable statement that binds a name to an object, doesn't this mean that I can put it anywhere in my code? Yes. Well... anywhere within reason. Your program still has to be valid python. But this does mean you can put it in places you might have not expected. (... especially if you're used to a language like C or Java)

Hang on a second... And can I treat class names (function names, module names) just like variable names? Yes. These names (if bound) have objects that they reference. You can access these objects just like you can an object assigned to a variable name.

Example Lets see an example in Wing!

Where else can names be bound? Function parameters in a function header for loop headers except clause headers

Namespaces How are names and their bindings kept track of? Namespace: a mapping from names to objects think of it as a dictionary there can be different namespaces: e.g. the variable x can be bound to different objects in different namespaces.

Scope How does Python distinguish between different namespaces? Scope: Technically, a region of the program that has a distinct namespace. Sometimes we'll talk about the scope of a name : A region of the program in which a particular (name, object) binding lives. Every namespace belongs to a scope, and every scope has a namespace. Namespace and Scope are sometimes used interchangeably.

Scopes in Python The scopes in python are as follows: the local scope (e.g. in a function call) enclosing scope (e.g., in an enclosing function) global scope (module scope) built in scope

The Global Scope and Modules The namespace of the enclosing module resides in the global scope. Terminology alert: Global doesn't mean that it's global to everything. Global means global to a module. The namespace of other modules can be accessed by using the import statement. Any name in the other module's namespace is an attribute of the module object.

Name binding and Namespaces When a name is bound, what namespace will the binding be stored in? Generally, it is stored in the namespace associated with the scope where the binding takes place e.g. in an assignment statement in a function call, the binding is associated with the namespace for the local scope of the function if the assignment statement is at the module level, the binding is associated with namespace for the global module scope

Name Resolution Name resolution: figuring out which namespace to use to look up a reference to a name LEGB rule: When a name is referenced, python looks it up in the following order: the Local (function) scope the Enclosing function scope the Global (module) scope the Built in scope

Examples Lets see some examples in Wing!

Recursion The problem is too big/too complicated! I don't know how to solve it! If I could solve a slightly smaller problem, I would be able to use that solution to come up with the solution to the original problem! But I don't know how to solve that slightly smaller problem! If I could solve a problem that's even a bit smaller than that one...

Recursion Recursion: a method for solving problems that involves breaking a problem down into smaller and smaller subproblems until you get a small enough problem that can be solved trivially using the solution to the smaller problem to solve the larger problem

Recursion The Base Case The small enough problem that can be solved trivially is known as the base case. Recursion ends when the base case has been reached.

Thinking Recursively The idea behind recursion is very easy to state Thinking recursively takes some practice Lets spend some time working on a few examples

Determining the sum of a list of Wing! integers

Factorial Function The expression n! is read as n factorial. It is recursively defined for non negative n as follows: n! = n * (n 1)! for n >= 1 0! = 1 (base case) Lets come up with both iterative and recursive solutions in Wing!

A Well known Sequence 0,1,1,2,3,5,8,13,21,34,... What's the pattern? How do we define it recursively?

Fibonacci Sequence F(n) = 0 if n = 0 F(n) = 1 if n = 1 F(n) = F(n 1) + F(n 2) if n > 1 Implementing a recursive function naively can sometimes be inefficient! Lets compare iterative and (naïve) recursive implementations in Wing Iterative version much faster than recursive. Why?

Towers of Hanoi Three pegs A number of different sized discs, all stacked from largest disc (at bottom) to smallest (at top) on peg 1. Move the discs from peg 1 to peg 3, using peg 2, without ever putting a larger disc on top of a smaller disc

Towers of Hanoi How do we modify this function we wrote in Wing (same as the function in your text) so that it returns the total number of moves required?