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

  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" = "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): = 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
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")

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 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 = open('out.txt', 'r')

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 -


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


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 '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]) 

Sunday, July 19, 2015

Python on the go - Revision of Repetitions

To repeat a certain task a fixed number of times, a for loop is used:-

 for x in range(5): 
    print x

To repeat a task until a certain condition is met, a while loop is used:-

def countdown(n):
    while n > 0:
          print n
          n = n-1
    print 'Blastoff!'

Another way a process can be repeated is recursion  which can be difficult to implement :-

def countdown(n):
    if n <= 0:
       print 'Blastoff!'
         print n

A recursion  usually consists of
(1)  a base case (when to stop)
(2) a work towards a base case
(3) a recursive call that calls itself

Updated - 27-7-2015

Saturday, July 11, 2015

Python on the go - Revision of Conditional Execution

There are times in a program when there is a need to execute certain instructions after a certain condition is met  and this process may be carried out by using   "if"  statements as shown:-

num = raw_input('Input a number? ')
if num2>0:
   print "The number is positive"
elif num2==0:
     print "The number is zero"
     print "The number is negative"

elif is an abbreviation for  "else if"  and it can be used more than once. else is used for the possible remaining conditions and normally used at the end of the "if" statements.  However, there does not need to be one.