Tag Archives: #252525

#mastery24 Creation and use of tuples in Python

A tuple is an unchangeable sequence of values.

x=(“Gilberto”,18,”ISC”)   tuple is written with ()

When you do this you create a tuple with three elements. You can access these elements individually by typing the variable and the then inside brackets directly to the right of the variable type the number of the element to which you are referring.

print (x[o])

>>>Gilberto

Python starts numbering at 0 so Gilberto=0,18=1 and ISC = 2

Packing and Unpacking:

In tuple packing, the values on the left are ‘packed’ together in a tuple:

x=(“Gilberto”,18,”ISC”)

In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:

x=(“Gilberto”,18,”ISC”)

(Name,Age,Studies) = x

print(Name,Age,Studies)

>>>Gilberto 18 ISC

Sources for info. about tuples: Link

 

1014

24

Gilberto Rogel García

Mastery08

Python conventions (Zen of Python)

The philosophy of Python is summarized by the document “PEP 20 (The Zen of Python)”

The Zen of Python

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren’t special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one– and preferably only one –obvious way to do it.

Although that way may not be obvious at first unless you’re Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it’s a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea — let’s do more of those!

 

08 1014

 

#mastery09 Basic types and their use in Python

Types are a category for things within Python with which Python will work. Types are integers,float,strings,tuples,lists and dictionaries.

Integers 

Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.

Integers are numeric values and can be stored, manipulated, and expressed inside variables without quotes.

Example: 

x=5

print (x)

>>>5

Float

Short for “floating point number,” any rational number, usually used with decimals such as 2.8 or 3.14159.

Strings 

A set of letters, numbers, or other characters.

Strings are a type. They are the basic unit of text in Python and all the other types except integers may contain strings.

Example:

x=”Hello World”

print (x) 

>>>Hello World

Tuples 

A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.

A tuple is an unchangeable sequence of values.

Example:

x = (“element 1”, “element 2”, “element 3”)

Lists

A list is a changeable sequence of data. A list is contained by square brackets i.e. [1,2,3]

Example:

x=[1,2,3]

print (x)

>>>[1,2,3]

Dictionaries 

A type with multiple elements i.e. x = {1: ‘a’,’b’: 2,3: 3} where you address the elements with, e.g., a text.

Example:

x={‘Hello’:’hola’, ‘Bye’:’adios’}

 

print(x)

>>> {‘Hello’:’hola’, ‘Bye’:’adios’}

Source with more info.: http://en.wikiversity.org/wiki/Python/Basic_data_types#quiz0

1014

09

Gilberto Rogel García

 

 

#WSQ17 – Scilab

Para este WSQ primero lei el tutorial para conocer mas acerca de scilab:
http://www.scilab.org/content/download/849/7901/file/Scilab_beginners.pdf

 

Después descargue scilab del siguiente enlace: https://www.scilab.org/

Scilab es util para calculos numericos, pero tambien podemos usarlo para las derivadas de funciones polinomiales. Además de que ya cuenta con funciones matemáticas y se puede integrar programas de los lenguajes de programacion mas comunes ( java, c++ ).

Por lo que leí Scilab puede ser una herramienta muy util, espero y me ayude en un futuro.

1017  17

Mastery 19

¿Como hacer un while?

Ejemplo 1:

El contador Indica que hasta que este llegue a el total de 10 entonces se detendrá y ya no se realizará el código contenido dentro de la sentencia while , de lo contrario mientras el “contador” sea menor a 10 entonces el código contenido se ejecutará desplegando hasta 10 veces “Hola Mundo” en pantalla.

19 1017

Mastery 15

Las sentencias de decisión o también llamadas de CONTROL DE FLUJO son estructuras de control que realizan una pregunta la cual retorna verdadero o falso (evalúa una condicion) y selecciona la siguiente instrucción a ejecutar dependiendo la respuesta o resultado.

¿como se usa un if?

un if sigue el siguiente codigo

este codigo utiliza una condicion que va secuenciada con punto y coma

If(Inicial;Condición;Aumento)

15 1017

Basic types and their use in Python

                                                                                                                       @PablO_CVi

Types are a category for things within Python with which Python will work. Types are:

 

integers 
Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.
float 
Short for “floating point number,” any rational number, usually used with decimals such as 2.8 or 3.14159.
strings 
A set of letters, numbers, or other characters.
tuples 
A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.
lists 
A list without a fixed number of elements. ie x=[1,2,3] note the square brackets, a list
dictionaries 
A type with multiple elements i.e. x = {1: ‘a’,’b’: 2,3: 3} where you address the elements with, e.g., a text..

Taken from: http://en.wikiversity.org/wiki/Python/Basic_data_types

Solution for q3.cpp

This program will print out the fibonacci series of a number given by the user. In order to make this program better, it should print out all the fibonnacci values until geting the one the user entered. Here is an example:

fibonacci of 10:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
the 10th value of your number in the fibonacci series is: 55.

In order to do this, we will need 4 variables:

  • long a=1;
  • long b=0;
  • long fibo;
  • long value;

For this program to work, frist, lets remember how fibonacci works… Solution for q3.cpp photo by: http://en.wikipedia.org/wiki/Recurrence_relation

In order to follow this mathematical formula, we will use a “for” loop, that sums our variables a and b (fibo= a+b) after giving “a” the value of “b” and “b” the value of “fibo” for many times. 

Here is how the loop should look:

for(int i=0; i<value; i++){

  fibo=a+b;

  a=b;

  b=fibo;

  cout<<fibo<<endl;

 }

Remember that the variable “i” in the loop, will work until its equal  or bigger than the initian value. 

The (cout<<fibo<<endl;) part inside the loop, is created in order to print out all values of the fibonacci series until it gets the one given by the user.

After creating this loop, you only have to print out “fibo” which will work as your result.

Here is a picture of how the program should run:

solution for q2.cpp

 

Here is how to do exponentiation asking the user for two numbers:

The program should ask the user for two numbers “a” and “b”, and then, raise “a” to the power of “b”. In order to do this, we will be using 4 variables:

  • long a;
  • long b;
  • long power=1;
  • long counter=0; 

We will be using a “do, while” loop, therefore, we need a variable to use as a counter and asign to it the value of 0. 
After asking the user for both numbers a and b, you will create a “do while” loop that works (while counter < b), because we will need the loop to work until we get to the value of b.

Lets remember the formula for exponentiation: solution for q2.cpp  photo by:  http://en.wikipedia.org/wiki/Exponentiation

Our do while loop, will multiply “a”, “b” times, which means that the loop should multiply “a x a” as many times as the variable “b” states.

Here is how it should look:

  do{

power=power*a;

counter=counter+1;

 

       }while(counter < b); 

After that you can print out the new value of your variable “power” in order to show your result. 

Here is a picture of the code and how it should run:

Arrays/Vectores

En programación, una matriz o vector (llamado en inglés array) es una zona de almacenamiento continuo, que contiene una serie de elementos del mismo tipo, los elementos de la matriz. Desde el punto de vista lógico una matriz se puede ver como un conjunto de elementos ordenados en fila (o filas y columnas si tuviera dos dimensiones).” Esta es una definicion de lo que serian vectores, aqui esta mi codigo para poder ver un ejemplo de esto….lo unico es que mi programa esta delimitado a solo poder aceptar 90 valores: https://github.com/A01229754/Tc107/blob/master/wsq10.cpp

La informacion donde saque la definicion: http://es.wikipedia.org/wiki/Vector_(inform%C3%A1tica)