Sunday, July 24, 2016

First Step in Python


Open terminal window (shown below) and let's play around with programming basics.


 The terminal pops up. Type in word python and hit enter. Those three right angle brackets (>>>) show you that the Python interpreter is ready to accept your instructions. You are the commander. 

pi@tron:~ $ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Or better yet:


pi@tron:~ $ python3
Python 2.7.9 (default, Mar  8 2015, 00:52:26) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

This opens Python interpreter in your terminal. If you want to exit either you type exit(), or press ctrl-d (ctrl and d simultenously).

Let's tell the interpreter to print something on the screen. Not surprisingly, Python has a print command that does exactly that. Let's try it!

>>> print 'Hello there!'
Hello there!
>>>

NOTE!
If you are using Python 3 then:

>>> print ('Hello there!')
Hello there!
>>>


The characters (letters, numbers and other symbols) enclosed with either single quotes or double quotes will be called strings

Just like freshly washed shirt hanging outside must be protected with clothes pins so that it does not fly to our neighbor's garden, strings in Python must be enclosed with those single or double quotes.

Python can print numbers too. Check this out:
>>> print 2 + 6
8
>>> print 45 * 174
7830
>>>

NOTE!
If you are using Python 3 then:

 
>>> print(2 + 6)
8
>>> print 45 * 174
7830
>>>


This way Python interpreter can do calculations. Symbols and their meaning are pretty straighforward:

+ add 
- subtract
* multiply 
/ divide
% remainder of division (we'll come to this later)
** exponentiation (powers of) 

Numbers you will use are usually of two sorts:
int (integer)
float (floating point, number with dots, fraction part)

Let's try some of them:

>>> print 1.5 + 1.5
3.0
>>> print 45 / 5
9
>>> print 11 % 3
2
>>> 

NOTE!
If you are using Python 3 then:

>>> print(1.5 + 1.5)
3.0
>>> print(45 / 5)
9.0
>>> print(11 % 3)
2
>>> 

The last one show result 2, because 3 goes 3 times into 11 and the remainder is 2. (3 * 3 + 2 = 11).

Since this is getting boring to use Python as a calculator, let's try to solve some interesting problem.

Problem Solving
How many centimeters per nanosecond does the light travel?

Wow! You got me here Dad.

Don't worry. Each problem can be broken into smaller parts that will lead us to a final solution. Google can help answer these questions:

How fast does light travel (asking Google)?
299 792 458 meters per second (m/s)

How many centimeters in a meter?
1 m = 100 cm

What is a nanosecond?
1 second = 1 / 1 000 000 000

Now, we're good to go. Try to figure it out yourself before you look at the solution below.




Solution

>>> print 299792458 * 100 * 1.0 / 1000000000
29.9792458
>>>

NOTE!
If you are using Python 3 then:
>>> print(299792458 * 100 * 1.0 / 1000000000)
29.9792458
>>>

Notice, that we cannot use spaces in numbers and that I used 1.0 /1000000000 instead of 1/1000000000. There reason is that calculation has fraction part and I made Raspberry PI to provide better answer this way.

The answer is that light will travel around 30 cm in one nanosecond (1 billionth of a second).

Trivia
Admiral Grace Hopper explains why nanoseconds are important in communication (it's hilarious).

Last word in this post: soon we are going to switch to Python 3 as this is the recommended one (the one that will eventually replace Python 2).