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!'
    else:
         print n
         countdown(n-1)


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? ')
num2=float(num)
if num2>0:
   print "The number is positive"
elif num2==0:
     print "The number is zero"
else:
     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.


Sunday, July 5, 2015

Python on the go - Revision of Functions

A function is a block of code which performs a computation. They can be reused in a program.
Here are some functions that can be used on lists

https://docs.python.org/2/tutorial/datastructures.html

These are inbuilt functions.  Certain functions like maths functions can be imported into the program by typing

from math import* 

We can also create our own functions. The 'Hello World' program can be written in a function as shown below.

def hello():
      print  "Hello World"

To call and run the function type  hello()  in the python shell

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

From https://docs.python.org/2/tutorial/datastructures.html


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 - Revision of Variables

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? ')
age2=float(age)
print "You are ",age2,"  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


temp=31.0

print type(temp)

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


updated 12-7-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)