Monday, June 29, 2015

Python on the go - List Comprehensions

Back to what I have been doing. Here is a way of simplifying some of the things that are done with lists


5.1.4. List Comprehensions

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
For example, assume we want to create a list of squares, like:
>>> squares = []
>>> for x in range(10):
...     squares.append(x**2)
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
We can obtain the same result with:
squares = [x**2 for x in range(10)]
This is also equivalent to squares = map(lambda x: x**2, range(10)), but it’s more concise and readable.
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and ifclauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Friday, June 26, 2015

Python on the go

Variables in Python can be declared on the go. For example if you input your age with raw_input which inputs your age as a string , it can be converted into a number by declaring it as a float. eg float(age)    as shown below

age = raw_input('How old are you?')
print "You are ",float(age),"  years old"

Variables  can be  declared as numbers usually in the form  of a float or integer.  For accuracy, numbers are usually declared as floats.

But you don't always have to declare variables in Python . Sometimes Python recognizes it for you automatically. If you type  a variable like temp=31.0 (short form of temperature) ,  Python recognizes it as a float. In otherwords, any variable with  a decimal number assigned to it,  will be recognized as a float in Python. You can check this with the program as shown


print type(temp)

The program will print the type of variable temp is - which is a float.

updated 29-6-2015

Wednesday, June 24, 2015

Python on the go

Those five points from 'Think Python'  lie the basis of most computer programs. I could not think of simplifying it much more than that.

What has been done so far is to output data  with  print.

There are generally two types of data - words and numbers but in the programming world, words are described by strings or characters while numbers are described by integers, floats etc.  "Hello World!" is a string.

To input a string, use raw_input()

name = raw_input('What is your name?')
print name

The above, stores the input  into a variable called name and then prints the name.

What is stored in a variable,  can be changed in the program.

Tuesday, June 23, 2015

Python on the go

In Python, a simple “Hello, World!” program would look like this

 print 'Hello, World!'

In C, it will look like this

One can see how much simpler it is, to write certain programs in Python. 

What is in most programs are generally the same. They have the following:-

input: Input data from the keyboard, a file, or some other device. (eg. raw_input)
output: Display data on the screen or send data to a file or other device. (eg print)
math: Perform basic mathematical operations like addition and multiplication. (eg  +   -     *      /   and %)
conditional execution: Check for certain conditions and execute the appropriate code. (eg. if, elif, else)
repetition: Perform some action repeatedly. ( eg. for, while and  the process of recursion)

Python on the go

I have been trying out three introductory courses  from Harvey Mudd College, MIT  and University of Michigan. The first two from edX and the last one from Coursera.

The hardest ones  seem to be the ones from edX - Harvey Mudd and MIT - but the content for those courses are very good and those who are up for the challenge, should probably try them out. For those who are starting out programming  and want to learn it in a more relaxed manner,  the one from Coursera - University of Michigan may be the better option. Generally all three so far, appear to be good enough courses to gain some knowledge  in programming. Professor Charles Severance from the Coursera one,  University of Michigan has written a remixed version of the 'Think Python' book.
Professor Zachary Dodds from Harvey Mudd  has  teamed up with others to also write a book about Python while Professor Eric Grimson    from MIT has won awards for his research in computer vision.

Python is made from C so, according to this article, it is good to learn as many languages as possible.

Unlike Python, getting a C compiler can be a headache in itself as there are many out there and some may not run properly . CodeBlocks  is relatively easy to set up and seems to have not much problems in running the programs.

Sunday, June 21, 2015

Python on the go

I have been trying out some courses on this programming language for the last few weeks and have found it to be an interesting one.

It's less than  20 MB in download size and it is text based but the language itself is quite simple - probably the simplest I have seen so far. I have had to set it as  - use as admin - as it was crashing quite a few times at the beginning.

In Windows, Python is run through Idle. The version that is used in most courses is Python 2.7.  I read that the reason is due to incompatibility of the code of 2.7 in version 3.

 Idle in Windows, is found by clicking  Start , All Programs , look for  the Python 2.7 folder and click on Idle. Programs are usually written in the Idle text editor and saved as .py files before running them.  The .py files can be edited by right clicking on them.(A simple text document (.txt) can also be used by Idle to run the programs.  Idle can save them in the .py files by typing .py at the end of the filename.)

The programs are run by pressing F5. Idle  will usually automatically prompt you to save the file before it runs.

Here are some resources about Python:  (updated 29-6-2015)

Tutorial:-  The Python Tutorial

Books : -
Think Python by Allen B. Downey
 Python for Informatics by Charles Severance - a remixed version of the above book
CS for All by Christine Alvarado (UC San Diego), Zachary Dodds (Harvey Mudd), Geoff Kuenning (Harvey Mudd), Ran Libeskind-Hadas (Harvey Mudd)

Debugging: - Online Python Tutor by Philip Guo

The courses that I have been trying out are also very good. The one from Coursera  goes at a more gentle pace while the other two from edX   seem to have quite a lot of content in them.

Edx -UWashingtonX: ECFS311x Becoming a Resilient Person: The Science of Stress Management and Promoting Wellbeing

The title of the course is a little bit condescending but the course itself is  something to reflect on and to ask ourselves whether certain topics of it, apply to us in our daily lives. So I think the course is worth a look. There are not many stress management courses in edX that deal with what can be a very complex health issue.

I think the second week may have something in it - that we tend to live our lives without  values or long term goals - hence the lack of meaning in our lives. Education is one of the key values mentioned along with relationships, which can be more important than education . Health and  leisure are the other two key  values that are mentioned.

As for the third week, I noticed  that  at times I  actually wasn't thinking about what I was eating! The bottom line about this is to reflect on it and ask ourselves if it applies to us and if it does, implement it and see if it works. The course is not difficult as the questions can be repeated as many times as you want and the assignments are more or less optional, but I sense that the main purpose of  this course  is not to pass exams, but to learn how to cope with  one of the most complex health issues, in this day and age.

 Here is an additional website on stress management that has some similarity to the course