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

 

 


#WSQ13 – Cars

--Originally published at マルコ

What To Do

File input/output is our access to permanent stores of data that last beyond the run time of our application.

Write a program that opens and reads the file 93cars.dat.txt and produces the following data:

  • average gas mileage in city (City MPG)
  • average gas mileage on highway (Highway MPG)
  • average midrange price of the vehicles in the set.

You can find the data set at the following link from the Journal of Statistics Education.

W13.1

Featured image:

https://www.pixiv.net/member_illust.php?mode=medium&illust_id=3061999


#WSQ12 – Estimating e

--Originally published at マルコ

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

W12.1

W12.2

Featured image:

https://www.pixiv.net/member_illust.php?mode=medium&illust_id=1969853


#WSQ11 – Go Bananas

--Originally published at マルコ

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.

W11.1

W11.2

Featured image:

https://www.pixiv.net/member_illust.php?mode=medium&illust_id=52121901


#WSQ10 – Babylonian Method

--Originally published at マルコ

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.

10.1

10.2


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