Monday, August 31, 2015

edX - HKUSTx: ELEC1200.1x A System View of Communications: From Signals to Packets (Part 1)

Am in  the second week of the course and has been quite interesting for the most part so far. Usually communication systems courses are tough but this one has been quite comprehensible so far. I am quite impressed by it. There is  Matlab programming which I am not really that fond of as I had only just completed some Python courses but I do realize, it is used quite a lot in  the workplace. The wonderful thing about these courses is that there are sandboxes where you can try out these softwares and take your time to learn them if you want.

However, datelines for this course is quite short as it is not really self paced - if you are not too worried about earning a cert but just want to learn something  then this is fine.

Sunday, August 23, 2015

Python on the go - Revision of Class and Inheritance

Referred from https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Classes


  A class can be defined as a template for creating objects such as functions. For example the program below provides a description of a shape  and what operations you can do with the shape (that is, the functions). The  __init__()  function is always run when the class is called.

class Shape:

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.description = "This shape has not been described yet"
        self.author = "Nobody has claimed to make this shape yet"
        
        
    def area(self):
        return self.x * self.y

    def perimeter(self):
        return 2 * self.x + 2 * self.y

    def describe(self, text):
        self.description = text

    def authorName(self, text):
        self.author = text

    def scaleSize(self, scale):
        self.x = self.x * scale
        self.y = self.y * scale



rectangle = Shape(100, 45)      #calling the class Shape
print rectangle.area()                # the area of the rectangle:

print rectangle.perimeter()       #the perimeter of it:

rectangle.scaleSize(0.5)         #makes it 50% smaller
print rectangle.area()              #the new area of the rectangle


It is possible to add extra features to a new class by using inheritance. The example below shows how a new class has inherited some of the features of the old one but its  __init__() function has changed.


class Square(Shape):
    def __init__(self,x):
        self.x = x
        self.y = x
        
square1=Square(10)
print square1.area()


Sunday, August 16, 2015

Python on the go - Revision of Files

One of the simplest ways for programs to maintain their data is by reading and writing
text files.

To write a file, you have to open it with mode 'w' as a second parameter:

file = open('out.txt', 'w')
file.write("Hello there.\n")
file.close()


If the file already exists in the same directory as the program, opening it in write mode clears out the old data . If the file doesn’t exist, a new one is created.


To read a file, you have to open it with mode 'r' as a second parameter:

file = open('out.txt', 'r')
print file.read()


file.read() returns a string with all the characters in the file. With print, the output can be seen in the Python shell.

The above two programs can be combined in one program to write and read a file.


file = open('out.txt', 'w')
file.write("Hello there.\n")
file.close()

file = open('out.txt', 'r')
print file.read()

Sunday, August 9, 2015

Python on the go - Revision of Tuples and Dictionaries

Tuples are very similar to lists but they are not modifiable and they are not used as often as lists. The items in a tuple are enclosed by round brackets instead of the  square brackets that are used in lists.

days=("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",  "Sunday")

Dictionaries have keys and values. Each value in a dictionary can be obtained by using a key. These keys and values are enclosed in curly brackets.

 phonebook={'Jon':456782,  'Peter':2345233,  'Fred':5678123}

Dictionaries can be useful for finding values of certain keys such as the phone  number in the case above. For example, to find the phone number or value of the key 'Jo' , type in the python shell -

phonebook["Jon"]

An additional key and value can be added to the dictionary by typing in the program-

phonebook["Jack"]=546773


Sunday, August 2, 2015

Python on the go - Revision of Lists

Lists are similar to arrays and in Python, it is quite easy to build them. Here is a simple program that adds a list of numbers using the built-in function sum.


mylist = [1,2,3]
print 'sum of the list is',sum(mylist)


Sometimes we may wish to take certain items from a list and use them for a certain purpose, for example  item 1 minus item 3  or adding the first two items of a list .


mylist = [1,2,3]
print 'sum of the list is',sum(mylist)
print

print 'item 1 minus item 3 of the list is ',mylist[0] - mylist[2]
print 'sum of first two items of the list is ' ,sum(mylist[0:2])