Python Programming Exercises 3

Similar documents
Flow Control: Branches and loops

LOOPS. Repetition using the while statement

Chapter 6: List. 6.1 Definition. What we will learn: What you need to know before: Data types Assignments

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

Types, lists & functions

DM502 Programming A. Peter Schneider-Kamp.

Python Intro GIS Week 1. Jake K. Carr

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

Using Lists (Arrays) Notes

University of Texas at Arlington, TX, USA

18.1. CS 102 Unit 18. Python. Mark Redekopp

At full speed with Python

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

Introduction to programming using Python

An Introduction to Python

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Topic 7: Lists, Dictionaries and Strings

The Practice of Computing Using PYTHON

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

python 01 September 16, 2016

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

Statements 2. a operator= b a = a operator b

ENGR/CS 101 CS Session Lecture 12

Python for Non-programmers

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Condition Controlled Loops. Introduction to Programming - Python

You Need an Interpreter! Comp Spring /28/08 L10 - An Interpreter

Variable and Data Type I

Part III Appendices 165

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 14

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

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

CS1110 Lab 6 (Mar 17-18, 2015)

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

Introduction to Python, Cplex and Gurobi

If Statements, For Loops, Functions

Beyond Blocks: Python Session #1

PLT Fall Shoo. Language Reference Manual

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

Programming Fundamentals and Python

Introduction to Python! Lecture 2

Python Problems MTH 151. Texas A&M University. November 8, 2017

Introduction to Programming with Python Session 5 Notes

Python workshop. Week 4: Files and lists.

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

Programming Training. Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving

Begin to code with Python Obtaining MTA qualification expanded notes

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Jarek Szlichta

CSCA20 Worksheet Strings

CS1 Lecture 11 Feb. 9, 2018

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

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

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

1.2 Adding Integers. Contents: Numbers on the Number Lines Adding Signed Numbers on the Number Line

Overview of List Syntax

Scientific Computing: Lecture 3

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

1 Strings (Review) CS151: Problem Solving and Programming

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Introduction to Python Code Quality

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

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Lists How lists are like strings

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Variables and literals

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

Python lab session 1

MITOCW watch?v=se4p7ivcune

[Ch 6] Set Theory. 1. Basic Concepts and Definitions. 400 lecture note #4. 1) Basics

Lab 09: Advanced SQL

(Refer Slide Time: 01:12)

Advanced Python. Executive Summary, Session 1

Any Integer Can Be Written as a Fraction

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Intro. Scheme Basics. scm> 5 5. scm>

The Dynamic Typing Interlude

ISE 101 Introduction to Information Systems. Lecture 3 Objectives: While loops Strings

Spring INF Principles of Programming for Informatics. Manipulating Lists

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

Control Flow Structures

Chapter 1. Data types. Data types. In this chapter you will: learn about data types. learn about tuples, lists and dictionaries

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

An introduction to Python

Agenda. Strings 30/10/2009 INTRODUCTION TO VBA PROGRAMMING. Strings Iterative constructs

Data Structures. Lists, Tuples, Sets, Dictionaries

CMSC201 Computer Science I for Majors

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

Programming for Experimental Research. Flow Control

APCS Semester #1 Final Exam Practice Problems

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

The Big Python Guide

Introduction to Computer Programming for Non-Majors

To illustrate what is intended the following are three write ups by students. Diagonalization

Python - Variable Types. John R. Woodward

C++ Reference NYU Digital Electronics Lab Fall 2016

SD314 Outils pour le Big Data

Transcription:

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 expressions, conditional statements and defining functions. If you did not finish the previous set of exercises you really should finish those before moving on to this set. 1. Lists are declared using square brackets with the elements of the list separated by commas. Lists can hold an arbitrary number of values which can be of different types. We use the len( ) function to discover the number of elements in a list. Try these examples: >>> len([]) >>> len([0, 1, 2, 3, 4]) >>> len(["a", 1, []]) 2. Lists can be added together and multiplied by an integer: >>> x = [1, 2, 3] + [4, 5, 6] >>> y = ["a"] * 3 We can use the inline versions of addition and multiplication if the list has already been declared: >>> x += [7, 8, 9] >>> y *= 2 3. We can append individual items to an existing list using the append( ) method: 1

>>> x = [] >>> x.append(1) >>> x.append(2) Multiple items can be added using the extend( ) method: >>> x = [] >>> x.extend([1, 2]) 4. You must be careful with assignment of lists because, unlike variables that contain simple values, the contents of the list is not copied, just a reference to the list: >>> x = [] >>> y = x >>> x.append(1) >>> x == y If it is not your intention to have two variables pointing to the same list, then you must use the copy( ) method: >>> x = [] >>> y = x.copy() >>> x.append(1) >>> x!= y It is very important you understand the difference between these two examples! 5. We can find out if a list contains a value using the in operator, just like we did last time for strings: >>> x = [1, 1, 1, 2, 3, 3, 5] >>> 2 in x >>> 4 in x and find the number of occurances of a value with count( ): 2

>>> x.count(1) Write a function called append non dup(mylist, element) that accepts two arguments, a list (mylist) and another variable (element) that can be of any type. The function should only append element to mylist if it is not already present in the list (so the list does not contain any duplicate values). Does your function return the list? Does it matter to the result? Is returning the list a good idea? 6. The ordering of elements in a list does not change unless we reorder them explicitly. This is important because we want to use integers to index elements in the list directly. Programming languages tend to start counting from zero, so the first element in the list is found at index 0, the second element at index 1, etc: >>> x = [1, 2, 3, 4, 5] >>> x[0] >>> x[1] In Python you can additionally access elements with negative numbers, for example, the last element in the list has index -1. This is useful because then we do not need to calculate the length of the list to calculate the correct index: >>> x[-1] >>> x[len(x) - 1] # unnecessary! Your answer from the previous question prevents duplicates from being appended to a list. Rewrite your function to reject consecutive duplicates, i.e. it should work like this: x = [] append_non_dup(x, 1) append_non_dup(x, 2) append_non_dup(x, 2) append_non_dup(x, 1) if x == [1,2,1] : print("correct!") 3

7. Elements can be removed from a list by value using the remove( ) method or by index using the del keyword: >>> x = [1,1,2,2,3,3] >>> x.remove(3) # removes first 3 in the list >>> del x[-1] # removes the element at index -1 (the last one) What happens if you try to remove a value not present in the list or using an index outside of the range of the list? 8. As well as single values we can access subsets of the list. In Python these are called slices. Assume we have a list called x: >>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and two integers to use as indices: slice start and slice end: >>> slice_start = 3 >>> slice_end = 6 We can extract the slice from index slice start (inclusive) to the end of the list: >>> x[slice_start : ] From the start of the list up to index slice end (exclusive): >>> x[ : slice_end] And, finally, from slice start (inclusive) to slice end (exclusive): >>> x[slice_start : slice_end] Make sure you understand how this works by doing a few more examples yourself. The following code helps to demonstrate why the start of the slice is inclusive and the end of the slice is exclusive (because using the same index will neatly split the list in two non-overlapping lists): 4

>>> x = [0,1,2,3,4,5] >>> x == x[:3] + x[3:] 9. All Python scripts have a predefined list called argv in the sys module, which contains all command line arguments given to a script when it is run: import sys print(len(sys.argv), "command line arguments") print(sys.argv) Copy the code above to a file (I have called my version argv.py ) and run it with some arguments: python3 argv.py hello world 1 2 3 Change the script above to only print out the command line arguments after the name of the script (to understand what I mean, run the example). 10. There are two methods to sort a list into order. For both methods to work all elements in the list must be orderable (i.e. values that can be compared with (==, <, <=, >, >=). The sort( ) method will sort a list in place, whereas the sorted( ) function returns a new list: >>> x = [2,5,1,3,4] >>> y = x.copy() >>> y.sort() >>> z = sorted(x) Both methods accept the reverse parameter to sort in reverse order: >>> sorted([2,5,1,3,4], reverse=true) 5

Python already has two functions called min( ) and max( ) that (as you would expect) returns the minimum and maximum values, respectively, from a list. Reimplement min( ) and max( ) using one of the sorting procedures. Which one is better for this problem: sort( ) or sorted( )? Why? 11. For loops in Python should be read as for each element in the sequence. The body of the for loop is executed once per item in the sequence (unless we interrupt it, see next exercises). numbers = [0,1,2,3,4] for i in numbers : Change the example above to only print out even numbers (Hint: you will need to use the modulus % operator). 12. Iterating over ranges of integers is such a common need that Python provides a special function called range( ) for that purpose: for i in range(10) : Change the code above to print out a triangle of asterisks, like this: * ** *** **** ***** 13. The range( ) function can accept two integers, a starting value (inclusive) and a stopping value (exclusive) : 6

for i in range(3, 10) : range( ) can also optionally take a third argument specifying a step value (i.e. only return every n th value in a given range). Try an example using a step value of 2. 14. Suppose we want to be able to associate the i th element in a list with the index i. Iterating through the list does not give us the index, but we can use len as the argument to range to generate a list of indices for use in our list: x = ["a", "b", "c", "d", "e", "f"] for i in range(len(x)) : print(i, x[i]) Change the example to only print out values at even numbered indices. Do not use an if statement, use the step argument in range( ). 15. Sometimes we do not want to iterate over all elements in a list, but stop when some condition is met (break out of the loop): x = [0,1,2,3,4,5,6,7,8,9] for i in x : if i > 4 : break Or skip the rest of the statements in the body of the loop (continue from the beginning): 7

x = [0,1,2,3,4,5,6,7,8,9] for i in x : if i > 3 and i < 6 : continue Play around with these two examples to ensure you understand what code is executed and why. In the case of a for loop, break and continue can always be replaced by different conditional statements, but using them can make your code more readable. Rewrite both examples without using either break or continue to prove this to yourself. 16. The sum( ) function returns the summation of a list of numbers: >>> sum([1,2,3]) Use the sum function to write a function called mean(x) that accepts a list of numbers as an argument and returns the arithmetic mean. Hint: the arithmetic mean for the values a 1, a 2,..., a n is 1 n ni=1 a i 17. Write your own version of sum( ) (you should call it something else). 18. Write a function that accepts a list of numbers as an argument and returns a new list where each value has been normalised (i.e. all elements sum to 1.0 in the same relative proportions as the input list). >>> normalise([1, 1, 2]) == [0.25, 0.25, 0.5] 8

19. Here we use a function called randint(x, y) from the random module which returns a random integer between x and y (inclusive) to implement a simple guessing game: import random r = random.randint(1, 10) print("guess a number between 1 and 10 (you have three guesses).") for i in range(1, 4) : guess = int(input("guess " + str(i) + ": ")) if guess == r : print("correct!") break else : print("wrong.") print("the number was " + str(r)) Write a new version of this program, but where the computer attempts to guess the number you are thinking of. 9