>>> print( X ) [0,1,2,3] def mystery(l):??

Size: px
Start display at page:

Download ">>> print( X ) [0,1,2,3] def mystery(l):??"

Transcription

1 REVIEW EXERCISES Problem 4 What is the output of the following script? Disclaimer. I cannot guarantee that the exercises of the final exam are going to look like this. Also, for the most part, the exercises are organized to fit tight in each page. They are not ordered by difficulty nor by topic. Problem 1 What is the output of the following instructions? print (7 > 10)... print (4 < 16)... print (4 == 4)... print (4 <= 4)... print (4!= 4)... x = 17.0 / 2 % 2 * 3**3 print (x)... Problem 2 What is the output of the following script? num1 = 5 if num1 >= 91: num2 = 3 if num1 < 6: num2 = 4 num2 = 2 x = num2 * num1 + 1 print (x,x%7) list1 = [1,2,3] list1 = list1 * 2 print (list1) list3 = [2,4,6,8,10,12,14,16,18,20] print (list3[0:1]) print (list3[5:7]) Problem 5 Replace the unknowns in the following script (each unknown consists, at most, of a single line): def mystery(l): if len(l)==0: return -1?? return??? >>> X=list(range(5)) >>> print( mystery(x) ) 0 >>> print( X ) [0,1,2,3]? Problem 3 Fill in the blanks of the following function. You may need only one line here. def swapper(l): This function mutates a list by swapping its first and last elements. Parameters: L: a list, of len(l)>=2. Returns: swapper does not return anything.... = =... 1 Problem 6 Replace the unknowns in the following script (each unknown consists, at most, of a single line): def mystery(l):?? >>> X=list(range(5,0,-1)) >>> print( mystery(x) ) None >>> print( X ) [1, 2, 3, 4, 5] >>> X=[(1,0), (0,2), (-10,10), (10,10)] >>> mystery( X ) >>> print( X ) [(-10,10), (0,2), (1,2), (10,10)]

2 REVIEW EXERCISES 2 Problem 7 Replace the unknowns in the following script (each unknown consists, at most, of a single line): class Empty: pass def putsome(x): def printhello():????? >>> A = Empty() >>> putsome(a) >>> A.f() Hello!? Problem 8 s1 = spamandeggs x = s1.find( and ) print (x)... print (s1[0:1],s1[5:7]) s = one\ttwo\tthree print (s)... print (len(s))... Problem 9 Fill in the blanks so function foof does as expected. def foof(l): Removes the elements of a list if they are strictly smaller than the previous one. Input: L, a list; foof may mutate L (remove elems). Returns: Nothing....=[L[i] for i in range(...) if...] >>> L = [1,3,2,4,3,5] >>> foof(l) >>> L [1, 3, 4, 5] >>> L = [1,-1,1,-1,1] >>> foof(l) [1, 1, 1, 1] >>> L = [ a, c, b, d ] >>> foof(l) [ a, b, d ] Fill in the unknown (??) Printed!: class empty: pass x = empty() y = empty()?? if y is x.z: print("printed!") Problem 10 so that the following script prints... Problem 11 Replace the unknowns in the following script (each unknown consists, at most, of a single line): class Kount: def init (self): self.c = 1 def tellme(self): c = self.c self.c += 1 return c class Negnt(??): def tellme(self): c = self.c??? return c >>> A = Kount() >>> B = Negnt() >>> print( A.tellMe(), B.tellMe() ) 1 1 >>> print( A.tellMe(), B.tellMe() ) 2-1 >>> print( A.tellMe(), B.tellMe() ) 3 1 >>> print( A.tellMe(), B.tellMe() ) 4-1? Problem 12 list2 = ["B","C","A"] list2.extend(["x","y"]) list2.reverse() list2.append("s") list2.sort() list2.reverse() print (list2)

3 REVIEW EXERCISES 3 Problem 13 def f(s): """Parameter s is a string.""" d = {} for c in s: if c in d.keys(): d[c] += 1 d[c] = 1 x = None for k in d.keys(): if x == None: x = d[k] y = k elif d[k] > x: x = d[k] y = k return y For the function defined above, answer the following questions. (1) What is the value of f( )?... (2) What is the value of f( )?... (3) What does f do? Specify its documentation below: Problem 15 Consider the program of creating the type (class) dictionary from scratch. Complete in the following code so the class below becomes functional. class D: # one very simple name! def init (self): self.mem = [] # This will store the (key,value)s def additem( self, key, value ): This function will add pair (key,value) to the internal list MEM. Each key, however, has only one value associated to it. if self.hasitem(key): # To ensure key is unique self.delitem(key) # add the pair self.mem.append( (...,...) ) def hasitem( self, key ): This function checks whether there is a value stored associated to key. return key in [...] def getitem( self, key ): Returns the value associated to key. Does not check if key exists in MEM. return [...][0] def delitem( self, key ): Deletes the pair (key,value) from MEM. Does not check if key exists in MEM. # There are three lines below, but one line is # actually enough! del self.mem[i] Problem 14 Consider the following function definition: def f(l): result = [] for e in L: if type(e)!= list: result.append(e) return f(e) return result Answer the following questions: (1) What does f([ a, b, c ]) do? (2) What does f( hello word ) do? (3) What does f(f( hello word )) do? (4) What does f([f]) return? def length( self ): Returns the number of entries in MEM.... An example implementation of the module above produced the following real output using the console (I stored D in D.py): >>> from D import D >>> A=D() >>> A.addItem(1,2) >>> A.addItem(0,5) >>> A.addItem(3,2) >>> A <D.D instance at 0x7f12bf38e5a8> >>> A.MEM [(1, 2), (0, 5), (3, 2)] >>> A.addItem(3,3) >>> A.MEM [(1, 2), (0, 5), (3, 3)] >>> A.delItem(0) >>> A.MEM [(1, 2), (3, 3)]

4 REVIEW EXERCISES 4 Problem 16 Problem 18 Fill in the blanks of the following script to make it print the results desired. def xpand(word): S = [...] print("-".join(s)) def phoo(x): if type(x)==type(""): print(... ) elif type(x)==type([]): xpand(x) print(...) return... >>> phoo( BINGO ) B-I-N-G-O None >>> phoo(10) 20 >>> phoo(0) 0 >>> phoo(20) 40 >>> phoo([1,2,3,"4"]) Implement the following function according to its definition. def average_word_length (text): text is a str consisting only of words and spaces (no punctuation). text is guaranteed to have at least one word. There can be one or more than one whitespace character between pairs of words. Return the average length of all words in text, as a float. # This can be done in a couple of lines Problem 19 Write a good docstring for the following function. Problem 17 Fill in the blanks of the following function. def distionaree(d): This function creates a tuple with the elements of the dictionary such that keys are immediately followed by their values in the tuple, i.e., if D[x]=y, then (...,x,y,...) in the returned tuple by distionaree(d). No additional order is enforced on the returned tuple. Examples: distionary({1:0,2:2, a :3}) -> (1,0,2,2, a,3) distionary({10:5, ow :()}) - > ( ow,(),10,5) Parameters: D: a dictionary. Returns: a tuple. accum = () for key in??:??? return accum? def enigma(s, c, n): count = 0 result = "" for char in s: if char == c and count < n: result += X count += 1 result += char return result

5 REVIEW EXERCISES 5 Problem 20 L = [[ on, off ], [ go, slow, stop ], [ up ]] print( L[1][2][3] )... print( L[0][0][0] )... L[2:] = L L[2:] = L print( L ) Problem 21 Implement the following function according to the functionality specified in its docstring. def longest_sequence(r): This function counts the number of lines in the longest consecutive sequence of blank lines in open reader r (a file handle, i.e., r was defined using r=open(...) or something alike), or zero if there are no blank lines at all. Parameters: r, a file handle. Returns: An integer specifying the longest consec. sequence of blank lines read from r. Problem 22 D = {} D[ apple ] = 4 D[ apple ] = 8 print( D )... D = {2 : [ d ], 0 : [ c ]} D[0].append( a ) print( D[0] )... Problem 23 Implement the following function according to the functionality specified in its docstring. def swapcases(s): This function swaps the case of each symbol in string s; lowercase symbols become uppercase and uppercase symbols become lowercase. The function returns nothing; rather, it mutates string s. Problem 24 D = {4 : 3, 3 : 2, 2 : 1} for x in D.values(): print(d[x]) D1 = { a : 1, b : 2, c : 3} D2 = { b : 2, c : 3, a : 1} print D1 == D Problem 25 Write a good docstring for the following function. def enigmatic(a, b): i = 0 while b!= "" and i < len(a): if a[i] in b: b = b.replace(a[i], "") i += 1 return b == ""

6 REVIEW EXERCISES 6 Problem 26 def mystery(s): result = for ch in s: if ch in AEIOUaeiou : result += x elif ch.isalpha(): result += _ result += ch return result if name == main : print( mystery( Why ) ) print( mystery( am ) ) print( mystery( I ) ) print( mystery( so tired? ) ) Problem 27 Write a proper documentation for the following function. def enigma(d1, d2): ans = {} for key, value in d1.items(): if key in d2: ans[key] = max(value, d2[key]) ans[key] = value for key, value in d2.items(): if key in d1: ans[key] = max(value, d1[key]) ans[key] = value return ans Problem 28 def bar1(y): x = len(y) - 1 while(x >= 0): if(x % 2 == 0): y[x] = x = x - 1 return # Main x = 5 names =[ Foo, Boo, Goo, Hoo, Voo, Doo ] result = bar1(names) print(x) print(names) print(result) Problem 29 Consider the following function. def whatdoido(x): Parameter x can be a tuple, a list or a string. This function returns True or False after testing some property on x. for i in range( 1, len(x) ): if x[i]==x[i-1]: return True return False Rewrite the function without using loops (for, while). Use recursion instead. def whatdoido(x): Parameter x can be a tuple, a list or a string. This function returns True or False after testing some property on x. if len(x)<=1:

7 REVIEW EXERCISES 7 Problem 30 Problem 31 Consider the linked list problem. We will implement a list-like class but using a recursive structure in it. Note: this class definition makes intensive use of recursion. class LL: def init (self): self.next=none self.value=none def append(self,value): This method inserts "appends" value into the linked list. if self.value == None: self.value =... elif self.next == None: self.next = LL() self.next.insert( value ) self.next.insert( value ) def has(self,value): Returns true if "value" is stored in the linked list, and returns false otherwise. if self.value == value:... if self.next!= None: return self.next... # If the above were unsuccessful... def get(self,i): Returns the value stored in the i-th element of the linked list. If i is negative or is after the last index of the linked list, we return None. if i < 0: if i == 0: if i > 0 and self.next == None: # Getting here means that: # i > 0 and self.next!= None... def set(self,i,value): Replaces the i-th element of the linked list with value. If i is negative or after the last index of the linked list, we do nothing. if i == 0: self.value... if i > 0 and self.next!= None:... def len(self): Returns the length of the linked list. if self.value == None: if self.next == None:... Consider the following function. def whatdoido(x): Parameter x can be a tuple, a list or a string. This function returns a list the reverse of x. For example, whatdoido([1, 2, 3]) is [3, 2, 1], and whatdoido( hello ) is [ o, l, l, e, h ]. R = [] for i in range(len(x)): R = [ x[i] ] + R return R Rewrite the function without using loops (for, while). Use recursion instead. def whatdoido(x): Parameter x can be a tuple, a list or a string. This function returns a list the reverse of x. For example, whatdoido([1, 2, 3]) is [3, 2, 1], and whatdoido( hello ) is [ o, l, l, e, h ]. if len(x)<=0: Problem 32 Create a function that takes a list L and creates a new function f, such that f(0) = L[0], f(1) = L[1], f(-1) = None, f(len(l))=none, etc. Part of the code is given; you just need to complete it. def list2func( L ): if L == []: # if the list is empty, we return the trivial # function, which always returns None def emptycase(n): return... return emptycase # now, we are not in the trivial case next_f = list2func( L[...] ) def nonemptycase(n): if n == 0: # return the current value return L[...] if n < 0: # this is "f(-1)" return... if n > 0: return next_f( n-1 ) return nonemptycase # we give the ball to the next guy >>> f = list2func( [4,1,2,3] ) >>> f(-1), f(0), f(1), f(2), f(3), f(4) (None, 4, 1, 2, 3, None)

8 REVIEW EXERCISES 8 Problem 33 Problem 35 def foo(*x,**y): print(x) print(y) foo( hello world ) foo( hello, world,0) foo() foo(150,x=150) foo({}) Problem 34 Consider the following function. def whatdoido(f,iters = 1000): This function computes the "fixed" point of function "f". A fixed point is a number such that f(z)=z. To avoid issues with arithmetic precision, we require that z-f(z) <threshold to accept z as an answer. Our starting point is z=0. Also, since the search could go on forever, parameter "iters" tells when to stop the search, if we have not found a fixed point. threshold = 10.0**-3 z = 0 while abs( z - f(z) ) < threshold and iters > 0: z = f(z) iters-= 1 return z Rewrite the function without using loops (for, while). Use recursion instead. def whatdoido(...): This function computes the "fixed" point of function "f". A fixed point is a number such that f(z)=z. To avoid issues with arithmetic precision, we require that z-f(z) <threshold to accept z as an answer. Our starting point is z=0. Also, since the search could go on forever, parameter "iters" tells when to stop the search, if we have not found a fixed point File wordos.txt contains a bunch of words. Write a script that counts how many words and how many different words are there in wordos.txt, and then prints those numbers. The file may be multiline, but there is no punctiation. (Hint: make good use of str.split.) f = open(...) for word in ListOfWords: print( Number of words:,...) print( Number of different words:,...) Problem 36 Let us define an -list using recursion. An -list is a normal Python list whose elements are either: (1) the empty list [ ], or (2) another -list. For example, the following is an -list: [ [ [ [ ] ] ], [ ], [ [ ], [ ] ] ] For this problem, answer the following questions: (1) Write a function that checks whether a list is an -list. The function must return True or False. (2) Write a function that returns the height (number of levels) of a given -list. For example, [ ] has height 1, [ [ ] ] has height 2, [ [ ], [ ] ] also has height 2, [ [ [ ] ] ] has height 3, etc. (3) Write a function that counts the number of -lists contained in a -list, including the list itself. For example, [ ] has one -list, [ [ ], [ ] ] has three -lists, etc.

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2010 EXAMINATIONS CSC 108 H1S Instructors: Horton Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number: Family Name(s):

More information

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

Python review. 1 Python basics. References. CS 234 Naomi Nishimura Python review CS 234 Naomi Nishimura The sections below indicate Python material, the degree to which it will be used in the course, and various resources you can use to review the material. You are not

More information

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Student Number: Comments are not required except where indicated, although they may help us mark your answers. CSC 108H5 F 2018 Midterm Test Duration 90 minutes Aids allowed: none Student Number: utorid: Last Name: First Name: Do not turn this page until you have received the signal to start. (Please fill out the

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science DECEMBER 2013 EXAMINATIONS CSC 108 H1F Instructors: Craig and Gries Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number:

More information

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

Question 1. Part (a) Part (b) December 2013 Final Examination Marking Scheme CSC 108 H1F. [13 marks] [4 marks] Consider this program: Question 1. Part (a) [4 marks] Consider this program: [13 marks] def square(x): (number) -> number Write what this program prints, one line per box. There are more boxes than you need; leave unused ones

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2012 EXAMINATIONS CSC 108 H1S Instructors: Campbell Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number: Family

More information

Babu Madhav Institute of Information Technology, UTU 2015

Babu Madhav Institute of Information Technology, UTU 2015 Five years Integrated M.Sc.(IT)(Semester 5) Question Bank 060010502:Programming in Python Unit-1:Introduction To Python Q-1 Answer the following Questions in short. 1. Which operator is used for slicing?

More information

Question 1. December 2009 Final Examination Marking Scheme CSC 108 H1F

Question 1. December 2009 Final Examination Marking Scheme CSC 108 H1F Question 1. [10 marks] Below are five segments of code. Each one runs without error. To the right of each segment, show the output it generates. L = [1, 2, 3, 4] for item in L: item = item * 5 print L

More information

Programming to Python

Programming to Python Programming to Python Sept., 5 th Slides by M. Stepp, M. Goldstein, M. DiRamio, and S. Shah Compiling and interpreting Many languages require you to compile (translate) your program into a form that the

More information

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully.

Do not turn this page until you have received the signal to start. In the meantime, please read the instructions below carefully. UNIVERSITY OF TORONTO MISSISSAUGA DECEMBER 2014 FINAL EXAMINATION CSC108H5F Instructor: Zingaro, Petersen, Tong Duration: 3 hours Examination Aids: None Student Number: Family Name(s): Given Name(s): The

More information

CPSC 301: Computing in the Life Sciences Winter , Term 2. Practice Questions

CPSC 301: Computing in the Life Sciences Winter , Term 2. Practice Questions CPSC 301: Computing in the Life Sciences Winter 2015 2016, Term 2 Practice Questions Note: These questions are for you to practice for the final exam. The format and coverage of the final questions may

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science SUMMER 2012 EXAMINATIONS CSC 108 H1Y Instructors: Janicki Duration NA PLEASE HAND IN Examination Aids: None Student Number: Family Name(s):

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/58 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

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

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/60 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science DECEMBER 2009 EXAMINATIONS CSC 108 H1F Instructors: Gries, Horton, Zingaro Duration 3 hours PLEASE HAND IN Examination Aids: None Student

More information

This quiz is open book and open notes, but do not use a computer.

This quiz is open book and open notes, but do not use a computer. 1. /15 2. /10 3. /10 4. /18 5. /8 6. /13 7. /15 8. /9 9. /1 10. /1 Total /100 This quiz is open book and open notes, but do not use a computer. Please write your name on the top of each page. Answer all

More information

CSC148 Fall 2017 Ramp Up Session Reference

CSC148 Fall 2017 Ramp Up Session Reference Short Python function/method descriptions: builtins : input([prompt]) -> str Read a string from standard input. The trailing newline is stripped. The prompt string, if given, is printed without a trailing

More information

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates Chapter 3 : Informatics Practices Class XI ( As per CBSE Board) Python Fundamentals Introduction Python 3.0 was released in 2008. Although this version is supposed to be backward incompatibles, later on

More information

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

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed Student Number: Last Name: First Name: UNIVERSITY OF TORONTO SCARBOROUGH Wnter 2016 EXAMINATIONS CSC A20H Duration 2 hours 45 mins No Aids Allowed Do not turn this page until you have received the signal

More information

CIS192 Python Programming

CIS192 Python Programming CIS192 Python Programming Data Types Joseph Cappadona University of Pennsylvania September 03, 2015 Joseph Cappadona (University of Pennsylvania) CIS 192 September 03, 2015 1 / 32 Outline 1 Data Types

More information

CS 1110 Prelim 2 November 13th, 2014

CS 1110 Prelim 2 November 13th, 2014 CS 1110 Prelim 2 November 13th, 2014 This 90-minute exam has 5 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

All programs can be represented in terms of sequence, selection and iteration.

All programs can be represented in terms of sequence, selection and iteration. Python Lesson 3 Lists, for loops and while loops Suffolk One, Ipswich, 4:30 to 6:00 Tuesday Jan 28 Nicky Hughes All programs can be represented in terms of sequence, selection and iteration. 1 Computational

More information

Visualize ComplexCities

Visualize ComplexCities Introduction to Python Chair of Information Architecture ETH Zürich February 22, 2013 First Steps Python Basics Conditionals Statements Loops User Input Functions Programming? Programming is the interaction

More information

CPSC 217 Midterm (Python 3 version)

CPSC 217 Midterm (Python 3 version) CPSC 217 Midterm (Python 3 version) Duration: 60 minutes 7 March 2011 This exam has 81 questions and 14 pages. This exam is closed book. No notes, books, calculators or electronic devices, or other assistance

More information

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

CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvani CIS192: Python Programming Data Types & Comprehensions Harry Smith University of Pennsylvania September 6, 2017 Harry Smith (University of Pennsylvania) CIS 192 Fall Lecture 2 September 6, 2017 1 / 34

More information

Introduction to Python

Introduction to Python Introduction to Python EECS 4415 Big Data Systems Tilemachos Pechlivanoglou tipech@eecs.yorku.ca 2 Background Why Python? "Scripting language" Very easy to learn Interactive front-end for C/C++ code Object-oriented

More information

Overview of List Syntax

Overview of List Syntax Lists and Sequences Overview of List Syntax x = [0, 0, 0, 0] Create list of length 4 with all zeroes x 4300112 x.append(2) 3 in x x[2] = 5 x[0] = 4 k = 3 Append 2 to end of list x (now length 5) Evaluates

More information

CSCI 101 Midterm Sample Questions

CSCI 101 Midterm Sample Questions CSCI 101 Midterm Sample Questions Note: you may bring one 8.5"x11" double-sided sheet of notes for your use during the exam (handwritten or typed). Otherwise, no notes, computers, calculators, phones or

More information

Python for C programmers

Python for C programmers Python for C programmers The basics of Python are fairly simple to learn, if you already know how another structured language (like C) works. So we will walk through these basics here. This is only intended

More information

2

2 2 3 4 5 6 Open a terminal window and type python If on Windows open a Python IDE like IDLE At the prompt type hello world! >>> 'hello world!' 'hello world!' Python is an interpreted language The interpreter

More information

Data Science Python. Anaconda. Python 3.x. Includes ALL major Python data science packages. Sci-kit learn. Pandas.

Data Science Python. Anaconda.  Python 3.x. Includes ALL major Python data science packages. Sci-kit learn. Pandas. Data Science Python Anaconda Python 3.x Includes ALL major Python data science packages Sci-kit learn Pandas PlotPy Jupyter Notebooks www.anaconda.com Python - simple commands Python is an interactive

More information

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

(IUCAA, Pune)   kaustubh[at]iucaa[dot]ernet[dot]in. Basics of Python - 2 by Kaustubh Vaghmare (IUCAA, Pune) E-mail: kaustubh[at]iucaa[dot]ernet[dot]in 1 of 54 Sunday 16 February 2014 05:30 PM Our First Program - Rewritten! Let us introduce the following

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 6A Spring 203 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

More information

Exercise: The basics - variables and types

Exercise: The basics - variables and types Exercise: The basics - variables and types Aim: Introduce python variables and types. Issues covered: Using the python interactive shell In the python interactive shell you don t need print Creating variables

More information

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

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python CS95003 - Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / 2014 Subjects 1) Beginning with Python 2) Variables 3) Strings 4) Basic arithmetic operators 5) Flow control 6) Comparison

More information

15-110: Principles of Computing Sample Exam #1

15-110: Principles of Computing Sample Exam #1 15-110: Principles of Computing Sample Exam #1 The following is a "sample exam" that you can use to practice after you have studied for the exam. Keep in mind that the actual exam will have its own questions,

More information

CS 1110 Prelim 2 November 10th, 2016

CS 1110 Prelim 2 November 10th, 2016 CS 1110 Prelim 2 November 10th, 2016 This 90-minute exam has 5 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

Iterators & Generators

Iterators & Generators Iterators & Generators Sequences A sequence is something that you can: Index into Get the length of What are some examples of sequences? Sequences We ve been working with sequences all semester! Examples:

More information

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

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 CS 133 - Introduction to Computational and Data Science Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 Introduction to Python II In the previous class, you have

More information

Python: Short Overview and Recap

Python: Short Overview and Recap 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

More information

Python Programming: Lecture 2 Data Types

Python Programming: Lecture 2 Data Types Python Programming: Lecture 2 Data Types Lili Dworkin University of Pennsylvania Last Week s Quiz 1..pyc files contain byte code 2. The type of math.sqrt(9)/3 is float 3. The type of isinstance(5.5, float)

More information

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

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1 Lecture #15: Generic Functions and Expressivity Last modified: Wed Mar 1 15:51:48 2017 CS61A: Lecture #16 1 Consider the function find: Generic Programming def find(l, x, k): """Return the index in L of

More information

Sequence Types FEB

Sequence Types FEB Sequence Types FEB 23-25 2015 What we have not learned so far How to store, organize, and access large amounts of data? Examples: Read a sequence of million numbers and output these in sorted order. Read

More information

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

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 CS 133 - Introduction to Computational and Data Science Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017 Introduction to Python II In the previous class, you have

More information

UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2014 EXAMINATIONS. CSC 108 H1S Instructors: Campbell and Papadopoulou.

UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2014 EXAMINATIONS. CSC 108 H1S Instructors: Campbell and Papadopoulou. PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2014 EXAMINATIONS CSC 108 H1S Instructors: Campbell and Papadopoulou Duration 3 hours PLEASE HAND IN Examination Aids: None Student

More information

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

Basic Scripting, Syntax, and Data Types in Python. Mteor 227 Fall 2017 Basic Scripting, Syntax, and Data Types in Python Mteor 227 Fall 2017 Basic Shell Scripting/Programming with Python Shell: a user interface for access to an operating system s services. The outer layer

More information

Comp Exam 1 Overview.

Comp Exam 1 Overview. Comp 170-400 Exam 1 Overview. Resources During the Exam The exam will be closed book, no calculators or computers, except as a word processor. In particular no Python interpreter running in a browser or

More information

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

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix CS 21 Final Review About the Final Saturday, 7-10pm in Science Center 101 Closed book, closed notes Not on the final: graphics, file I/O, vim, unix Expect Questions That Ask You To: Evaluate Python expressions

More information

MUTABLE LISTS AND DICTIONARIES 4

MUTABLE LISTS AND DICTIONARIES 4 MUTABLE LISTS AND DICTIONARIES 4 COMPUTER SCIENCE 61A Sept. 24, 2012 1 Lists Lists are similar to tuples: the order of the data matters, their indices start at 0. The big difference is that lists are mutable

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 61A Spring 2017 Structure and Interpretation of Computer Programs Test 1 Solutions INSTRUCTIONS You have 2 hours to complete the exam. The exam is open book, open notes, closed computer, closed calculator.

More information

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

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines. Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

More information

CSCI 150 Exam 2 Solutions

CSCI 150 Exam 2 Solutions CSCI 150 Exam 2 Solutions 1. Here are definitions of two classes. Give a main( ) function that creates 3 persons: Hagrid, who is a Person, and Harry and Hermione, who are both Students at Hogwarts school.

More information

(CC)A-NC 2.5 by Randall Munroe Python

(CC)A-NC 2.5 by Randall Munroe Python http://xkcd.com/353/ (CC)A-NC 2.5 by Randall Munroe Python Python: Operative Keywords Very high level language Language design is focused on readability Mulit-paradigm Mix of OO, imperative, and functional

More information

Python Programming Exercises 3

Python Programming Exercises 3 Python Programming Exercises 3 Notes: These exercises assume that you are comfortable with the contents of the two previous sets of exercises including variables, types, arithmetic expressions, logical

More information

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1

Lecture #16: Generic Functions and Expressivity. Last modified: Fri Feb 26 19:16: CS61A: Lecture #16 1 Lecture #16: Generic Functions and Expressivity Last modified: Fri Feb 26 19:16:38 2016 CS61A: Lecture #16 1 Consider the function find: Generic Programming def find(l, x, k): """Return the index in L

More information

Python Mini Lessons last update: May 29, 2018

Python Mini Lessons last update: May 29, 2018 Python Mini Lessons last update: May 29, 2018 From http://www.onlineprogramminglessons.com These Python mini lessons will teach you all the Python Programming statements you need to know, so you can write

More information

Introduction to Python. Data Structures

Introduction to Python. Data Structures Introduction to Python Data Structures Data Structures Encapsulation & Notion of an Object Data + a set of methods (functions) that operate on the data A.foo() Linear Data Structure: List, Strings, sequences

More information

CPSC 217 L01 Midterm

CPSC 217 L01 Midterm CPSC 217 L01 Midterm Duration: 50 minutes 4 March 2010 This exam has 55 questions and 10 pages. This exam is closed book. No notes, books, calculators or electronic devices, or other assistance may be

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 61A Fall 2012 Structure and Interpretation of Computer Programs Final Examination Solutions INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer,

More information

Introduction to Python

Introduction to Python Introduction to Python Efstratios RAPPOS efstratios.rappos@heig-vd.ch Slide 1 2016 HEIG-VD SNU Summer School Background Easy and popular programming language Interpreted: must have python installed to

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 6A Fall 25 Structure and Interpretation of Computer Programs Midterm 2 Solutions INSTRUCTIONS You have 2 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

More information

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6

CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6 CSSE 120 Introduction to Software Development Practice for Test 1 paper-and-pencil part Page 1 of 6 Name: Use this quiz to help you prepare for the Paper-and-Pencil portion of Test 1. Complete it electronically

More information

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

Lecture #12: Mutable Data. map rlist Illustrated (III) map rlist Illustrated. Using Mutability For Construction: map rlist Revisited Lecture #12: Mutable Data Using Mutability For Construction: map rlist Revisited Even if we never change a data structure once it is constructed, mutation may be useful during its construction. Example:

More information

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

UTORid: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell CSC 108H1 F 2017 Midterm Test Duration 50 minutes Aids allowed: none Last Name: UTORid: First Name: Lecture Section: (circle one): L0101 (MWF10) L0201 (MWF11) Instructor: Jacqueline Smith Jen Campbell

More information

Data Bootcamp: Code Practice #2 Revised: November 2, 2016

Data Bootcamp: Code Practice #2 Revised: November 2, 2016 Data Bootcamp @ NYU Stern Coleman & Lyon Data Bootcamp: Code Practice #2 Revised: November 2, 2016 Answer each of the questions below. What you hand in can be code or handwritten or something else, but

More information

CS Advanced Unix Tools & Scripting

CS Advanced Unix Tools & Scripting & Scripting Spring 2011 Hussam Abu-Libdeh slides by David Slater March 4, 2011 Hussam Abu-Libdeh slides by David Slater & Scripting Python An open source programming language conceived in the late 1980s.

More information

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 November 12th, 2015 CS 1110 Prelim 2 November 12th, 2015 This 90-minute exam has 6 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

CSCI 121: Anatomy of a Python Script

CSCI 121: Anatomy of a Python Script CSCI 121: Anatomy of a Python Script Python Scripts We start by a Python script: A text file containing lines of Python code. Each line is a Python statement. The Python interpreter (the python3 command)

More information

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction

ECE 364 Software Engineering Tools Lab. Lecture 3 Python: Introduction ECE 364 Software Engineering Tools Lab Lecture 3 Python: Introduction 1 Introduction to Python Common Data Types If Statements For and While Loops Basic I/O Lecture Summary 2 What is Python? Python is

More information

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009 Python A Technical Introduction James Heliotis Rochester Institute of Technology December, 2009 Background & Overview Beginnings Developed by Guido Van Rossum, BDFL, in 1990 (Guido is a Monty Python fan.)

More information

Comp 150 Final Exam Overview.

Comp 150 Final Exam Overview. Comp 150 Final Exam Overview. Resources During the Exam The exam will be closed book, no calculators or computers. You may bring notes on four sides of 8.5x11 inch paper (either both sides of two sheets,

More information

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1 EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1 COMPUTER SCIENCE 61A June 24, 2014 0.1 Warmup What Would Python Do? >>> x = 6 >>> def square(x):... return x * x >>> square(x) >>> max(pow(2, 3), square(-5)) -

More information

Sequences and iteration in Python

Sequences and iteration in Python GC3: Grid Computing Competence Center Sequences and iteration in Python GC3: Grid Computing Competence Center, University of Zurich Sep. 11 12, 2013 Sequences Python provides a few built-in sequence classes:

More information

Student Number: Comments are not required except where indicated, although they may help us mark your answers.

Student Number: Comments are not required except where indicated, although they may help us mark your answers. CSC 108H5 F 2014 Midterm Test Duration 50 minutes Aids allowed: none Last Name: Student Number: First Name: Lecture Section: L0101 Instructor: Dan Zingaro (9:00-10:00) Lecture Section: L0102 Instructor:

More information

Python a modern scripting PL. Python

Python a modern scripting PL. Python Python a modern scripting PL Basic statements Basic data types & their operations Strings, lists, tuples, dictionaries, files Functions Strings Dictionaries Many examples originally from O Reilly Learning

More information

CS 1110 Prelim 2 November 12th, 2015

CS 1110 Prelim 2 November 12th, 2015 CS 1110 Prelim 2 November 12th, 2015 This 90-minute exam has 6 questions worth a total of 100 points. Scan the whole test before starting. udget your time wisely. Use the back of the pages if you need

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL 2016 EXAMINATIONS CSC 108 H1S Instructor(s): Smith and Fairgrieve Duration 3 hours PLEASE HAND IN No Aids Allowed You must earn at

More information

CSCE 110: Programming I

CSCE 110: Programming I CSCE 110: Programming I Sample Questions for Exam #1 February 17, 2013 Below are sample questions to help you prepare for Exam #1. Make sure you can solve all of these problems by hand. For most of the

More information

Basic Python Revision Notes With help from Nitish Mittal

Basic Python Revision Notes With help from Nitish Mittal Basic Python Revision Notes With help from Nitish Mittal HELP from Documentation dir(module) help() Important Characters and Sets of Characters tab \t new line \n backslash \\ string " " or ' ' docstring

More information

Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA

Midterm #2a Spring minutes DO NOT WRITE IN THIS AREA 15-112 Midterm #2a Spring 2016 80 minutes Name: Andrew ID: @andrew.cmu.edu Section: You may not use any books, notes, or electronic devices during this exam. You may not ask questions about the exam except

More information

Test 2 Practice Problems for the Paper-and-Pencil portion SOLUTION

Test 2 Practice Problems for the Paper-and-Pencil portion SOLUTION Page 1 of 19 Test 2 Practice Problems for the Paper-and-Pencil portion SOLUTION Test 2 will assess material covered in Sessions 1 through 14 (but NOT material from Session 15). It will draw problems especially

More information

Python Review IPRE

Python Review IPRE Python Review Jay Summet 2005-12-31 IPRE Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing Elements Cloning Slices Mutable Types: Lists Dictionaries

More information

CS111 Jeopardy: The Home Version

CS111 Jeopardy: The Home Version CS111 Jeopardy: The Home Version The game that turns CS111 into CSfun11! Fall 2018 This is intended to be a fun way to review some of the topics that will be on the CS111 final exam. These questions are

More information

Spring 2017 CS 1110/1111 Exam 1

Spring 2017 CS 1110/1111 Exam 1 CS 1110/1111 Spring 2017 Exam 1 page 1 of 6 Spring 2017 CS 1110/1111 Exam 1 Bubble in your computing ID in the footer of this page. We use an optical scanner to read it, so fill in the bubbles darkly.

More information

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

Lecture #12: Mutable Data. Last modified: Sun Feb 19 17:15: CS61A: Lecture #12 1 Lecture #12: Mutable Data Last modified: Sun Feb 19 17:15:18 2017 CS61A: Lecture #12 1 Using Mutability For Construction: map rlist Revisited Even if we never change a data structure once it is constructed,

More information

Introduction to Python (All the Basic Stuff)

Introduction to Python (All the Basic Stuff) Introduction to Python (All the Basic Stuff) 1 Learning Objectives Python program development Command line, IDEs, file editing Language fundamentals Types & variables Expressions I/O Control flow Functions

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

CPSC 217 Midterm (Python 3 version)

CPSC 217 Midterm (Python 3 version) CPSC 217 Midterm (Python 3 version) Duration: 50 minutes 6 March 2009 This exam has 61 questions and 11 pages. This exam is closed book. No notes, books, calculators or electronic devices, or other assistance

More information

Python and Bioinformatics. Pierre Parutto

Python and Bioinformatics. Pierre Parutto Python and Bioinformatics Pierre Parutto October 9, 2016 Contents 1 Common Data Structures 2 1.1 Sequences............................... 2 1.1.1 Manipulating Sequences................... 2 1.1.2 String.............................

More information

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid: 1 STRINGS Objectives: How text data is internally represented as a string Accessing individual characters by a positive or negative index String slices Operations on strings: concatenation, comparison,

More information

24. Sorting a List. Topics: Selection Sort. Merge Sort

24. Sorting a List. Topics: Selection Sort. Merge Sort 24. Sorting a List Topics: Selection Sort Merge Sort Our examples will highlight the interplay between functions and lists Sorting a List of Numbers Before: x --> 50 40 10 80 20 60 After: x --> 10 20 40

More information

Beyond Blocks: Python Session #1

Beyond Blocks: Python Session #1 Beyond Blocks: Session #1 CS10 Spring 2013 Thursday, April 30, 2013 Michael Ball Beyond Blocks : : Session #1 by Michael Ball adapted from Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike

More information

CS 1110 Final, December 17th, Question Points Score Total: 100

CS 1110 Final, December 17th, Question Points Score Total: 100 CS 1110 Final, December 17th, 2014 This 150-minute exam has 8 questions worth a total of 100 points. Scan the whole test before starting. Budget your time wisely. Use the back of the pages if you need

More information

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science APRIL/MAY 2009 EXAMINATIONS CSC 108H1S Instructor: Horton Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number: Last (Family)

More information

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

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher PLEASE HAND IN UNIVERSITY OF TORONTO SCARBOROUGH December 2017 EXAMINATIONS CSCA20H3 Duration 3 hours PLEASE HAND IN Examination Aids: None Student Number: Last (Family) Name(s): First (Given) Name(s):

More information

ENGR 101 Engineering Design Workshop

ENGR 101 Engineering Design Workshop ENGR 101 Engineering Design Workshop Lecture 2: Variables, Statements/Expressions, if-else Edgardo Molina City College of New York Literals, Variables, Data Types, Statements and Expressions Python as

More information

STA141C: Big Data & High Performance Statistical Computing

STA141C: Big Data & High Performance Statistical Computing STA141C: Big Data & High Performance Statistical Computing Lecture 1: Python programming (1) Cho-Jui Hsieh UC Davis April 4, 2017 Python Python is a scripting language: Non-scripting language (C++. java):

More information

Spring 2017 CS 1110/1111 Exam 3

Spring 2017 CS 1110/1111 Exam 3 Spring 2017 CS 1110/1111 Exam 3 Bubble in your computing ID, top to bottom, in the footer of this page. We use an optical scanner to read it, so fill in the bubbles darkly. If you have a shorter ID, leave

More information

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

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals Midterm 1 Review Important control structures Functions Loops Conditionals Important things to review Binary numbers Boolean operators (and, or, not) String operations: len, ord, +, *, slice, index List

More information