Sunday, July 24, 2016

Taking Input



Before we take an input from a user, a little explanation regarding the Python versions installed on a Raspberry PI.

There are two of them: one is Python 2.7.x and the other is Python 3.4.x. (as of writing this post).

There are differences between them, so from now on let's use the higher version as it is supposed to be the version that replaces the older Python version 2.

What's different about it? We did not learn much so we should not see much of a difference. In previous posts, you noticed that we printed stuff using parentheses () when using Python3 and we did not use them using Python (which is version 2).

Open a text editor (you can use Leafpad text editor from a tool bar).


In the text editor let's type in the following:
print('Hello, and welcome. What is your name?')
name = input()
print('It is nice to meet you', name)
print('I hope you are digging Python programming!')

Save it as hello.py file in your home directory. Then execut this from the terminal using the following command:

pi@tron ~ $ python3 hello.py

Watch what happens!
 
pi@tron ~ $ python3 hello.py
Hello, and welcome. What is your name?

And now the computer awaits for your answer. Type in a name (for instance Jim) and watch what happens next.

The command input():
name = input() 
awaits for the user to type some text and then it is going to store it in the variable (name). Only then it proceeds to the next command we typed in our hello.py file

print('It is nice to meet you', name) 

Then, next step the computer does is to display text enclosed in quotes followed by , (comma separator) followed by the variable name.

print('It is nice to meet you', name)

Those display the text stored in variable name

Problem Solving
Write a code that displays similar greeting. Omit pieces of code such as closing parentheses or quote mark. See what errors they generate.

Variables


Raspberry PI is a computer. Computers have memory. This is where the program (code) is stored as well as the data that it uses (strings, integers, float numbers etc.). You can try to imagine computer's memory as (nearly) endless cabinet with drawers. Each drawer has its unique number which is called 'memory address'. In those drawers computer stores data and code we write.

A variable is a name that will point to a string or number that is stored somewhere in the depths of computer's memory (one of those drawers). Consider this:

>>> name1 = 'Alice'
>>> print name1
Alice
>>>

NOTE!
If you are using Python 3, use:

>>> name1 = 'Alice'
>>> print(name1)
Alice
>>>


The equality sign = assigns string 'Alice' to a variable name. So now, a variable is stored in of of those drawers but you (programmer) don't have to know the drawer address (number). You use variable with the name name1 and computer knows which drawer to look into to pull out the string 'Alice' from.

>>> age = 10
>>> print age
10
>>> age = age +5
>>> print age
15
>>> 

NOTE!
If you are using Python 3, use: 
>>> age = 10
>>> print(age)
10
>>> age = age +5
>>> print(age)
15
>>> 

First the variable age points to the number 10 (again kept in this magical computer drawer with a particular address). Then the program assigns something else to the same variable age: it is a current value of what age points to (10) and adds (+ operator) number 5 to it.

age = age + 1, could be translated into: 

In the drawer labeled age is number 5. Take 5 out and replace with 6 (5+1). Put it back in the drawer labeled age. Now, if you read what's in the drawer labeled age, you will find number 6 there.

Problem Solving 

What is going to be the final value of the variable apples when this code runs?
>>> apples = 7
>>> apples = apples - 5
>>> apples = apples * 4
>>> 

Run this code on your own adding print apples as the last expression and see if you were right!  

Now, write a code that counts the number of minutes in a week. Use a few variables to create this code. Use variable for days, hours, and minutes.

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).

Enter Raspberry PI

Previous | Home | Next


Children have truly inquisitive minds. My Mom tells me that when I was young, I had beeb asking a million questions a day. Children tend to do that. It is not easy to provide kids with answers that will be interesting to inspire them. But let's give it a shot, shall we?

Ask away!

What's the difference between toaster and Raspberry PI?
Unlike a toaster, Raspberry PI can do more than one thing It's because Raspberry PI is a computer.

What do computers do?
Computers run computer programs.

What is a computer program?
It is a recipe which computer carries out step by step. Instructions are understood by computer (you must use a programming language). It is by no means different from giving instructions to a person. For instance:
  1. Sit comfortably on the couch.
  2. Turn on TV
  3. Grab the black controller.
  4. If in your hands: press this button. Otherwise you must have dropped it, then pick it up and press this button.
  5. Move cursor to this menu.
  6. Click.
  7. Play
  8. Turn off PlayStation.
  9. Turn off TV.
  10. Put controller back on the shelf.  
  11. Repeat tomorrow. 
 Writing computer program is similar to this.

How do I write a computer program?
Just like English or any other language is used to communicate with other people, you can use programming language to tell computer what to do. It has rules that you need to learn. We will run it in the text editor and execute (execute = tell the computer to run it).

Which programming language are we going to use?
We are going to use Python. Google engineers use it, NASA uses it. It's really cool and powerful.

Look at the Raspberry PI desktop now. This is what you are going to use. You can access Python interpreter clicking Menu/Programming/Python2 (idle) or Python3 (idle). This opens Python interpreter. You can use it to type commands and accepting them with Enter. We will do that shortly.

Or you can click terminal window on a tool bar and type python (or python3). I will show you that later.

What's Python interpreter?
It is a program that translates your program into language understood by your computer (called machine language).


Ready for the ride?


Let's write our first program (go next).  

Previous | Home | Next