Módulos

--Originally published at Site Title

Una de las características de python es la importación de paquetes/módulos. Antes de explicar cómo funciona, crea esta estructura.

Para esto se tiene que crear una estructura:

1 series/
2  __init__.py
3  fibonacci.py
Donde:

  • series: es un directorio (paquete)
  • __init__.py: es un script de python en blanco (necesario para identificar un directorio como paquete)
  • fibonacci.py: es un script de python (módulo) con funciones (o lo que quieras)

Usar import

Se puede usar import de esta forma para importar un paquete o módulo:

1
importpackage[.subpackage.[.module]]

Para importar el paquete series, ejecuta:

1
>>>>importseries

Con esto se ha importado el paquete series, pero es poco útil, ya que aún no se tiene acceso al contenido del módulo fibonacci.

Para tener acceso al contenido de fibonacci, importa de esta manera:

1
>>> importseries.fibonacci

De esta forma, es posible acceder a las funciones de fibonacci:

1
2
3
4
5
6
7
8
>>>>importseries.fibonacci
>>> series.fibonacci.fibo(10)

Esto es, en introducción, lo que es un módulo y como se usa.

 

Fuente: https://auraham.wordpress.com/2012/07/19/python-como-usar-import/


WSQ12

--Originally published at Site Title

Estimating e

 

What To Do

In this assignment you will estimate the mathematical constant e. You should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.

You will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).

 

Code/Código:

codepreci

 

Python:

precipy

 

 

Gracias a mi compañero Marco por ayudarme con este problema. Pueden encontar su blog en el siguiente enlace:

https://eosthel.wordpress.com/2017/05/02/python-conventions/

 


WSQ11

--Originally published at Site Title

GO BANANAS!

What To Do

Write a function called find_bananas which receives a single parameter called filename (a string) and returns a positive integer which is the number of times the word (string) “banana”  (or “BANANA” ) is found in the file. The banana can be any case (‘BaNana’ or ‘BANANA’ or ‘banana’, etc) and they can be “stuck together” like “banAnaBANANA” (that counts as two). Create your own test file (plain text) to check your work.

 

Code/Código:

11

Python:

bananapy

 

 


WSQ10

--Originally published at Site Title

Background

In this assignment you will write a function to calculate the square root of a number using the Babylonian method. You can search for that method, it will be easy to find.

What to Do

The function should receive a number and return floating point number. Obviously you should test your function, so create a main program that asks the user a value, calculates the square root and displays that.

 

Code/Código:

codebab

 

Python:

pybab


WSQ09

--Originally published at Site Title

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

 

Code/Código:

codetxt

Text/Texto:

pruebatxt

 

Python:

txtpy


WSQ04

--Originally published at Site Title

SUM OF NUMBERS

What to Do:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero (why?) and then we add each number in the range provided by the user. Just for fun, what is the mathematical formula to do this calculation?

 

Qué hacer:

Escriba un programa que solicite un rango de números enteros y luego imprime la suma de los números en ese rango (inclusive).

Puedes usar una fórmula para calcular esto por supuesto pero lo que queremos que hagas aquí es practicar usando un bucle para hacer un trabajo repetitivo.

Por ejemplo, la suma de 6 a 10 sería 0 + 6 + 7 + 8 + 9 + 10.

Observe que nuestra suma comienza con cero (¿por qué?) Y luego agregamos cada número en el rango proporcionado por el usuario. Sólo por diversión, ¿cuál es la fórmula matemática para hacer este cálculo?

 

Code/Código:

 

sumacode

 

Python:

 

sumapy

 

 


WSQ04

--Originally published at Site Title

SUM OF NUMBERS

What to Do:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

Notice our sum starts with zero (why?) and then we add each number in the range provided by the user. Just for fun, what is the mathematical formula to do this calculation?

 

Qué hacer:

Escriba un programa que solicite un rango de números enteros y luego imprime la suma de los números en ese rango (inclusive).

Puedes usar una fórmula para calcular esto por supuesto pero lo que queremos que hagas aquí es practicar usando un bucle para hacer un trabajo repetitivo.

Por ejemplo, la suma de 6 a 10 sería 0 + 6 + 7 + 8 + 9 + 10.

Observe que nuestra suma comienza con cero (¿por qué?) Y luego agregamos cada número en el rango proporcionado por el usuario. Sólo por diversión, ¿cuál es la fórmula matemática para hacer este cálculo?

 

Code/Código:

 

sumacode

 

Python:

 

sumapy

 

 


WSQ03

--Originally published at Site Title

PICK A NUMBER

What to Do:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

 

Qué hacer:

Escriba un programa que elija un número entero aleatorio en el rango de 1 a 100.

Hay diferentes maneras de hacer que suceda, usted elige cuál funciona mejor para usted.

A continuación, solicita al usuario una estimación del valor, con indicaciones de ‘demasiado alto’ o ‘demasiado bajo’ del programa.

El programa continúa funcionando hasta que el usuario adivina el entero. Usted podría hacer algo adicional aquí incluyendo decir allí al usuario cuántas conjeturas tuvieron que hacer para conseguir la respuesta correcta.

Es posible que desee comprobar que su programa no siempre utiliza el mismo número aleatorio es elegido y también debe dividir su resolución de problemas en partes. Tal vez sólo generar el número aleatorio e imprimir que como un primer paso.

 

Code/Código:

randomcode

 

Python:

randompy

 


WSQ02

--Originally published at Site Title

TEMPERATURE

What to Do:

Write a program that will prompt the user for a temperature in Fahrenheit and then convert it to Celsius. You may recall that the formula is C = 5 ∗ (F − 32)/9.

 

Qué hacer:

Escriba un programa que le pedirá al usuario una temperatura en Fahrenheit y luego lo convertirá en Celsius. Puede recordar que la fórmula es C = 5 * (F – 32) / 9.

 

Code/Código:

tempcode

 

Python:

tempy

 

 

 


WSQ-01

--Originally published at Site Title

Fun with numbers

What to Do

Ask the user for two integer values, then use those two values to calculate and show the following:

  • The sum of the two numbers.
  • The difference of the two numbers.
  • The product of the two numbers.
  • The integer based division of the two numbers (so no decimal point). First divided by second.
  • The remainder of integer division of the two numbers.

 

 Qué hacer:

Pida al usuario dos valores enteros, luego,  use esos dos valores para calcular y mostrar lo siguiente:

  • La suma de los dos números.
  • La diferencia de los dos números.
    El producto de los dos números.
  • La división basada en enteros de los dos números (no hay punto decimal). Primero dividido por segundo.
  • El resto de la división entera de los dos números.

 

 

Code/Código:

code

 

Python:

py