Programming in Business Analytics Lecture 5 Notes PDF

Summary

These lecture notes cover programming concepts in Business Analytics, focusing on Python. Topics include CSV files, shallow vs deep copy, and variable argument functions. The document provides an overview of object-oriented programming and different important programming concepts, such as nested lists and tuples, along with explanations and examples.

Full Transcript

Programming in for Business Analytics (BMAN73701) Lecture 5 – CSV files, shallow vs deep copy, variable number of arguments in functions Dr. Xian Yang [email protected] Last Lecture Object-oriented programming...

Programming in for Business Analytics (BMAN73701) Lecture 5 – CSV files, shallow vs deep copy, variable number of arguments in functions Dr. Xian Yang [email protected] Last Lecture Object-oriented programming (OOP) Conventional program based on functions An object-oriented program is based on classes and a collection of interacting objects, which can receive messages, process data, and send messages to other objects 2 Object-oriented programming (OOP) Some OOP terminology Objects: Python’s abstraction for data. Everything in Python (e.g. an integer, list, module, function, etc) is an object and can be assigned to a variable. Class: A structure for defining a type of object. Method: A function that is associated/part of a class Instance method vs Class method vs Static method Instance: An object that is of a particular class. Attribute: Variables or properties of a class or an instance Class attributes vs Instance attributes self: Refers to the instance calling the method 3 Create a class called BankAccount to represent a bank account. Quizz time Instances of this class are initialized by a customer name, account number, and the balance on the account. Each bank account should also have a fixed interest rate of 0.1 (it should be able to access this value from instances of the class or the class itself). Add the method withdraw() which decreases the balance of the account. Make sure this method does not allow the account to go to a balance of less than £100. Add a second method add_interest() which adds interest to the balance (the interest should be the interest rate multiplied by the current balance). 4 Important principles in OOP Encapsulation (Information hiding) Restriction of access to variables and methods Purpose is to prevent data from being modified by accident + improve control of data flow in a program Achieved via imposing restrictions on variables (public vs private vs protected variables) + setter and getter methods 5 Important principles in OOP Inheritance The transfer of the characteristics of a class to other classes that are derived from it Subclass vs superclass Inherited methods/attributes with the same name are overwritten Can be indirect but not circular 6 Important principles in OOP Polymorphism Define “abstract method” which needs to be implemented by subclasses 7 Agenda Nested lists Deep vs shallow copy Read data from file / print data to a file Variable number of arguments in a function 8 Nested lists So far, we only investigate simple lists: But we can also have nested lists: 9 Nested lists Iterate over a list: How to iterate over a nested list such that every item is printed out? This could be a 3x3 matrix → Matrices (data frames) will be focus of next lecture 10 Nested lists How to iterate over a nested list such that every item is printed out? 11 Nested lists How to print out a specific item in a nested list, e.g. ‘Hello’? 12 Nested lists append() extend() 13 Nested lists: Example 14 Other Nested Compounds Dictionaries Tuples 15 Agenda Nested lists Deep vs shallow copy Read data from file / print data to a file Variable number of arguments in a function 16 Assignment Why? 17 Assignment What is a normal assignment doing when copying a simple data type, such as an integer? x 3 y 4 Identity: Object’s address in memory (check with id()and compare with is (x is y)) 18 Assignment What is a normal assignment doing when copying a mutable object, such as a list or a String dictionary? List object object colors1 ‘red’ colors2 ‘blu e’ ‘roug e’ ‘vert ’ 19 Assignment and modification What is a normal assignment doing when copying a mutable object, such as a list or a dictionary, and changing part of the object? The list colors1 has been changed automatically as well because we don't have two lists. We have only two names (references) for the same list! colors1 ‘red’ colors2 ‘green ‘blu e’’ 20 Slicing (Shallow copying) Are there ways of copying mutable objects completely? The slice operation allows us toand colors1 completely colors2 are two distinct list objects but contain copy shallow list structures references to the same string objects ('red' and 'blue’). Modifying colors2 to 'green', does not affect the corresponding element in colors1. colors ‘red’ colors 1 2 ‘blu e’ ‘green 21 Slicing (Shallow copying) What happens if we apply the slice operator to a nested list? The slice operation copies the shallow list but not the sublists, only the references to the sublists are copied. ‘c’ lst ‘a’ lst 1 2 ‘b’ ‘a b’ ‘b ‘d’ a’ 22 deepcopy() Is there a way to copy a (nested) list completely? Yes, there is! → deepcopy()function from module copy The module copy has also a method copy(), which is doing a shallow copy, i.e. identical to the slice operator [:] ‘c’ lst ‘a’ lst 1 2 ‘b’ ‘a b’ ‘d’ ‘b 23 Quiz 1 Which of the follow codes give the output: a) b) c) d) 24 Quiz 1 Ans: b) and c) 25 Deep vs shallow copy The difference between shallow and deep copying is only relevant for compound objects A shallow copy constructs a new compound object and then inserts references into it to the objects found in the original. A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. Note, a shallow copy constructs a new object, while an assignment will simply point the new variable at the existing object. 26 Agenda Nested lists Deep vs shallow copy Read data from file / print data to a file Variable number of arguments in a function 27 Reading and writing data Example csv file How to read in such files, modify them and print data 28 Using Python’s inbuilt module csv If you want to import or export spreadsheets and databases for use in the Python interpreter, use the csv module (Comma Separated Values). The csv module includes various built-in functions but we will focus on two only: – csv.reader – csv.writer The two functions allow you to edit, modify, and manipulate the data stored in a csv file Alternative approaches: Read CSV files Using NumPy loadtxt() method 29 Reading data from a csv file open(…) csv.reader( List with File …) string object Reader objects object The open(name_of_csvfile, newline=‘’) function returns a file object To pull data from a file object, use the reader function csv.reader(file_object). This will generate a reader object Each row read from the reader object is returned as a list of strings. No automatic data type conversion is 30 Reading data from a csv file Example: Let’s read in the data from Projects- year1_Partial.csv and increase the revenue of each project by £100. Then, for each project, print out “project ID, updated Each row from revenue”. the reader object is returned as a list of Header cannot be converted strings. to float 31 Reading data from a csv file Example: Let’s read in the data from Projects-year1_Partial.csv and increase the revenue of each project by £100. Then, for each project, print out “project ID, increased revenue”. Let’s also store the project IDs and revenues in a list. 32 Writing data to a csv file open(…,’w’, csv.writer( csv.writer(…).writerow(…) …) …) Write list as a row of comma File object Writer separated items object The open(name_of_csvfile, ‘w’, newline=‘’) function returns a file object. ‘w’ indicates the write mode which is used to edit and write new information to the file To write data to a file, use the function csv.writer(file_object). This will return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. writerow(row)writes the row item to the writer’s file object 33 Writing data to a csv file Example: Print out the header, project ID and updated revenues 34 Agenda Nested lists Deep vs shallow copy Read data from file / print data to a file Variable number of arguments in a function 35 Arguments in a function Often it is desirable to call the same function with a variable number of arguments (not just different values) For example, the function range can be called in several different ways: range(m, n, s): for example, range(4,11,2) would result in the sequence 4, 6, 8, 10 range(m, n): equivalent of range(m, n, 1). We say that the default value of step is 1 range(n): equivalent of range(0, n) or 36 1. Default arguments Default arguments allow you to make some arguments of functions as optional and use default values if the user does not want to provide values for such arguments Specify default argument values using argument_name = default value in the function definition The value of a default argument should be immutable, e.g. a constant 37 Default argument values In Python, when passing a mutable value as a default argument in a function, the default argument is mutated anytime that value is changed Solution: 38 Default argument values Important: In the function definition, you cannot have a default argument before an argument without a default value. Valid Invalid 39 2. Positional vs Keyword Arguments Positional Arguments: Values are assigned based on the position/order in the function call. Keyword Arguments: Arguments are passed by explicitly naming them, allowing you to pass them in any order. 40 *args, **kwargs Syntax: def function_name(*args, **kwargs): # you *args: This allows Function body to pass a variable number of positional arguments to a function. Example: **kwargs: This allows you to pass a variable number of keyword arguments to a function. Example: 41 *args, **kwargs We can combine the use of both *args and **kwargs in the same function. It allows the function to accept both a variable number of positional arguments and a variable number of keyword arguments. In a function call, positional arguments must always come before keyword arguments. 42 Quiz 2 1. What is the output of the following code: a) 45 b) 25 c) Error 2. What is the output of the following code: a) 1 2 (3, 4) {'x': 5} b) 1 10 (3, 4) {'x': 5} c) Error 43

Use Quizgecko on...
Browser
Browser