Object oriented programming is a very useful tool for computer scientists. Python (and other languages) have built-in objects or classes which give the user simple data types to work with. Examples of these are integers, strings (which function as sentences), and floats (numbers with decimals), as well as more complex structures such as lists of objects, and dictionaries (one type of object is paired with another type or a list). Object oriented programming allows a user to create their own classes/objects, which can provide more detailed and user friendly programs. A programmer can also write specific methods for their own classes, which can also make these data structures easier to access and manipulate. These classes use the basic python built in data structures, but can combine them more easily, keeping all the data together in one convenient place.
An example of a user created class that we saw in lab was a Patient Roster for a doctor. This class was created with a setup for a dictionary of the actual patients, but also built in restrictions (gender ratio and maximum number of patients). Additionally, we wrote a method to add patients to our class which checked if the gender ratio or maximum number was violated before adding the patient. This eliminated a lot of steps that a user would have to take themselves when using basic data structures, and made things much more simple.
In addition to creating classes, a user can create subclasses, which are a part of the superclass. These subclasses will inherit the methods which are written for the superclass, and they can have separate specific methods written for themselves. An example of this we saw in lecture was the superclass Shape, with its subclasses Triangle and Square. Shape was initialized with a position, and had a method to change its position. These properties were inherited by both Triangle and Square, but they each had individual methods called draw and get_area, which were specific to themselves.
When a subclass inherits a method from a superclass, there are 3 things that can happen. The method can be: inherited, extended, or overridden. Normal inheritance was discussed above, where the method in the superclass works for the subclass. The method can also be extended, meaning it does everything it does in the superclass, but also some more. Lastly, the method can be overridden, meaning the method in the subclass is completely different than the one in the superclass.
All these properties of object oriented programming allow for a computer scientist to create very specific structures and methods. This specificity is extremely helpful for future use of the program, and one would not be able to replicate the specificity using the built-in Python data types.
Tuesday, 17 February 2015
Sunday, 8 February 2015
In the last week, we learned about recursion, recursive functions, and how to trace the action of a recursive function.
Recursive functions are functions which use themselves within their own definition. At first it was a bit of a weird concept to wrap my head around. If someone were to ask for a definition of a word, and you used that word, it wouldn't help them at all, but with recursive functions they can actually work.
The key to how recursive functions work is the base case. We have been working with functions that deal with nested lists (lists inside lists), where the base case is anything that isn't a list. The code written splits the cases into 2 options: if it encounters a list it uses itself on the list and returns an output (for example, tracking the items in the list), and if it encounters anything else it does something (usually returns a number, like 0 or 1). This non-list item is the base case for functions dealing with lists.
What helped me understand how recursion works was actually tracing the action of the function. To trace how the function works, you start with the base case, a non-list. The function acts on the base case to do something, and then returns that. Then you go onto the next simplest case, a list of depth 1. When the function acts on the list, it calls itself on each item of the list. Since all the items are the base case, it performs its action on each of them, which then allows it to give an output for that list. Then if we look at the next simplest case, a list of depth 2, we see that once we enter the list, the remaining items are either lists of depth 1 and non-lists, both of which we know the output for since we already traced them. This can continue to infinitely complicated levels, or infinitely nested lists, because we can always trace them down to a base case, and then work backwards to get the output.
Talking about how recursion works can be very abstract and confusing. I'm sure my description of how it works has been vague and hard to follow, but that was how I felt while learning it. The act of tracing through how the function works was the key to understanding recursion. Simply walking through the steps from the base case upwards will demonstrate how the function works, and facilitate understanding.
Recursive functions are functions which use themselves within their own definition. At first it was a bit of a weird concept to wrap my head around. If someone were to ask for a definition of a word, and you used that word, it wouldn't help them at all, but with recursive functions they can actually work.
The key to how recursive functions work is the base case. We have been working with functions that deal with nested lists (lists inside lists), where the base case is anything that isn't a list. The code written splits the cases into 2 options: if it encounters a list it uses itself on the list and returns an output (for example, tracking the items in the list), and if it encounters anything else it does something (usually returns a number, like 0 or 1). This non-list item is the base case for functions dealing with lists.
What helped me understand how recursion works was actually tracing the action of the function. To trace how the function works, you start with the base case, a non-list. The function acts on the base case to do something, and then returns that. Then you go onto the next simplest case, a list of depth 1. When the function acts on the list, it calls itself on each item of the list. Since all the items are the base case, it performs its action on each of them, which then allows it to give an output for that list. Then if we look at the next simplest case, a list of depth 2, we see that once we enter the list, the remaining items are either lists of depth 1 and non-lists, both of which we know the output for since we already traced them. This can continue to infinitely complicated levels, or infinitely nested lists, because we can always trace them down to a base case, and then work backwards to get the output.
Talking about how recursion works can be very abstract and confusing. I'm sure my description of how it works has been vague and hard to follow, but that was how I felt while learning it. The act of tracing through how the function works was the key to understanding recursion. Simply walking through the steps from the base case upwards will demonstrate how the function works, and facilitate understanding.
Wednesday, 4 February 2015
Overview of the First Couple weeks
So far this course has been a lot different than I expected. I took CSC108 last semester and I was expecting a similar type of course. However, CSC148 is much different in 108 in the teaching style and the expectations of the course, which has been an interesting change.
CSC148 is run much differently than CSC108 was. I really enjoyed the "inverted classroom" method of teaching, where the course content was introduced in videos before the week, and the lecture time was spent on review and questions about the material. Additionally, the exercises after the videos at the end of the week were a good way to reinforce what we learned in class. This was a good way to teach the basics of python and keep us using it, which seemed like a good way to introduce a completely new subject to people. However, CSC148 is a very different course. We have more standard lectures, no videos, and no quizzes to keep the material fresh. Though it runs like a more traditional course, after taking 108 it feels strange for a computer science course.
One of the main differences I've found so far is that things are a lot more abstract. For CSC108, there were very specific answers which were expected and we were provided with a lot of help to lead us in the right direction. The best example of this is the starter code and instructions given to us for assignments. However, for CSC148, the first assignment had vague instructions and no starter code. This was a big shock to me, and at first I didn't know where to start. However, at the end it felt really satisfying to have built the entire program from scratch with my partner, knowing that the end product was entirely our work and ideas.
So far I would say I've enjoyed the course, and I hope that I continue to learn and improve my computer science skills.
CSC148 is run much differently than CSC108 was. I really enjoyed the "inverted classroom" method of teaching, where the course content was introduced in videos before the week, and the lecture time was spent on review and questions about the material. Additionally, the exercises after the videos at the end of the week were a good way to reinforce what we learned in class. This was a good way to teach the basics of python and keep us using it, which seemed like a good way to introduce a completely new subject to people. However, CSC148 is a very different course. We have more standard lectures, no videos, and no quizzes to keep the material fresh. Though it runs like a more traditional course, after taking 108 it feels strange for a computer science course.
One of the main differences I've found so far is that things are a lot more abstract. For CSC108, there were very specific answers which were expected and we were provided with a lot of help to lead us in the right direction. The best example of this is the starter code and instructions given to us for assignments. However, for CSC148, the first assignment had vague instructions and no starter code. This was a big shock to me, and at first I didn't know where to start. However, at the end it felt really satisfying to have built the entire program from scratch with my partner, knowing that the end product was entirely our work and ideas.
So far I would say I've enjoyed the course, and I hope that I continue to learn and improve my computer science skills.
Subscribe to:
Posts (Atom)