martes, 20 de octubre de 2015

Python Programming Language in Raspberry Pi

Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.
The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site,https://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.
The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.
This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.
For a description of standard objects and modules, see The Python Standard LibraryThe Python Language Reference gives a more formal definition of the language. To write extensions in C or C++, read Extending and Embedding the Python Interpreter and Python/C API Reference Manual. There are also several books covering Python in depth.



Here, the Python version that we are going to use and where we are going to make the programs is Python3, which have a few differences in comparision to Python2.

In our case, we use python with the ILDE, in our Raspberry Pi, but this exercises also can be solved in the ILDE for Windows, which is completely free.

VARIABLES:
In the first block, calle "Variables", we learnt to use variables and some basic operations in Python. For example, the first exercise asks to calculate the arithmetic mean between two numbers. We can see the solution here:

print("Calculate of the mean of two numbers:")
number1 = float(input("Type a number: "))
number2 = float(input("Type another number: "))
mean=(number1+number2)/2
print("The arithmetic mean between", number1, "and", number2, "is", mean)


Where:
print() is a function to print something on the screen. it can be a variable or a string.
input() is a function to keep something on the script.
float(input()) is a function to keep a float number on a variable, like for example, number1.
+ is the sum
- is the rest
* is the multiplication
/ is the division
// is the quotient
% is the rest



CONDITIONALS:
The second block is called "Conditionals". There, we can learn to use the "if" structure. Basically is that if the function fulfills a condition, it do something. if it doesn't fulfill the first condition, but it fulfills another one, it do the other thing programmed. Finally, if it doesn't fulfill none of the previous conditions, it do the last thing programmed.
For example, the first exercise ask you two numbers and it tells you if the numbers are divisibles.


print("Number divider")
dividend = int(input("Type the dividend: "))
divisor = int(input("Type the divisor: "))

if dividend % divisor == 0:
    print("The division is exact. Cocient:", dividendo // divisor)
if dividendo % divisor != 0:
    print("The division is not exact. Cocient:", dividendo // divisor, "Rest:", dividendo % divisor)
Where: if is the first condition. elif are the next conditions. else is what the program have to do if any of the previous conditions have been accomplished. == is if the number of the left is equal to the one of the right. >= is if the number of the left is larger or equal to the one of the right. > is if the number of the left is larger than the one of the right. <= is if the number of the left is smaller or equal to the one of the right. < is if the number of the left is smaller than the one of the right. RANGE:
The third block is called Range. In Python, a list is a group of elements. They are represented typing the elements between square brackets and separated by commas. A variable can save a list. For example, this exercise ask you to make a list which goes from 500 to 0, with an increment of 100.

print(list(range(500, -100, -100)))

Where:
list() is the function which creates a list. range() is a function which creates a list which goes from the first number to the second one, with an increment of the third number.

FOR:
The fourth block is called For. For is an structure of control which repeats a block of instructions a determinated number of times. That block is also called loop. For example, this example types on the screen "Hello" 3 times.
print("Comienzo")
for i in [0, 1, 2]:
    print("Hola ", end="")
print()
print("Final")
Where:
for is the call to the structure
i is the parameter
in says the parameter its value
[0,1,2] is a list. Here, to say the value of the i, you have to introduce a list and the loop is repeated as many times as the lenght of the list. Each time, i gets the next value of the list.

LISTS:
The fifht block are lists. Python is based on them. It's a group of elements, which can be anything: numbers, words or even other lists. For example, this program asks you for the elements of a list:
number = int(input("Tell me how many words does this list have: "))

if number < 1:
    print("Impossible!")
else:
    list = []
    for i in range(number):
        print("Tell me the word", str(i + 1) + ": ", end="")
        word = input()
        list += [word]
    print("The list created is:", list)
Where:
list=[ ] creates an empty list, to write things on it
list +=[ ] adds to the list a new place, with the value between the sqare brackets
list[n] goes to the element in the posicion n in the list called "list"


WHILE:
The sixth block is called While. While is a control structure which only stops when the condition is over. For example, the next program types on the screen the numbers from 1 to 3:
i = 1
while i <= 3:
  print(i)
  i += 1
print("End")
Where:
i is the variable
i=i+1 increases the value of i
and while the value of i is smaller or equal to 3, the loop continues repeating itself.


FUNCTIONS:
The last block is called Functions. You can call a function at any time of the script and they are used to don't have to repeat many times the same structure. At the beggining of the script, you type the function blocks and then you can call it at any time in the rest of the program. For example, this program calls a function:
def license():
    print("Copyright 2015 Marcos Gonzalez Perez")
    print("License CC-BY-SA 3.0")
    return

print("This program doesn't do anything interesting.")
license()
print("Program finished.")

Where:
def license(): is used to begin to create the function
return is used to say that the function is over
license() is used to call the function

You also could type license(arguments), changing arguments for things that you are introducing to the function. You can introduce as many arguments as you want.


You can find a complete manual of python with many solved exercises on this site: http://www.mclibre.org/consultar/python/index.html

Most of the problems in that page are solved by me, with many comments which can help you to understand completely how to program in Python.

Programs to download:
Variables
Conditionals
Range
For
Lists
While
Functions

No hay comentarios:

Publicar un comentario