WSQ13 – Cars

--Originally published at Elu's Blog

For this assignment these were my instructions:

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.

This is what I came up with:

Captura de pantalla 2017-04-24 a la(s) 21.08.37.png

Captura de pantalla 2017-04-24 a la(s) 21.08.50.png


Mastery Topics (Blog’s Index)

--Originally published at Elu's Blog

In this blog I wrote about 22 mastery topics in order to cover most of the basics of Python3.

Here they are:

  1. Use of comments – https://elusblog.wordpress.com/2017/04/20/use-of-comments-in-python3/
  2. Python conventions (Zen of Python but others for other languages) – https://elusblog.wordpress.com/2017/04/20/python-conventions-zen-of-python/
  3. Basic types and their use – https://elusblog.wordpress.com/2017/01/18/basic-types-python3/
  4. Basic output (print) – https://elusblog.wordpress.com/2017/01/18/wsq01-basic-output-and-user-input/
  5. Basic user input (text based) – https://elusblog.wordpress.com/2017/01/18/wsq01-basic-output-and-user-input/
  6. Calling functions – https://elusblog.wordpress.com/2017/01/31/functions-python3/
  7. Creating functions – https://elusblog.wordpress.com/2017/01/31/functions-python3/
  8. Importing and using modules/libraries – https://elusblog.wordpress.com/2017/02/14/wsq03-libraries-and-ranges-importing-and-using-moduleslibraries-and-creation-and-use-of-ranges-in-python-python3/
  9. Creating and using your own modules/libraries – https://elusblog.wordpress.com/2017/04/20/creating-and-using-your-own-moduleslibraries-python3/
  10. Use of the conditional “if” – https://elusblog.wordpress.com/2017/01/30/wsqo2-conditionals-use-of-if-else-and-elif-python3/
  11. Use of “else” with a conditional (and elif for Python) – https://elusblog.wordpress.com/2017/01/30/wsqo2-conditionals-use-of-if-else-and-elif-python3/
  12. Nesting of conditional statements – https://elusblog.wordpress.com/2017/01/30/wsqo2-conditionals-use-of-if-else-and-elif-python3/
  13. Use of loops with “while” – https://elusblog.wordpress.com/2017/02/14/wsq06-while-loops-python3/
  14. Use of loops with “for” – https://elusblog.wordpress.com/2017/02/14/wsq04-for-loops-python3/
  15. Use of recursion for repetitive algorithms – https://elusblog.wordpress.com/2017/04/20/recursion-python3/
  16. When to use what type of repetition in a program – https://elusblog.wordpress.com/2017/04/21/when-to-use-what-type-of-repetition-in-a-program-python3/
  17. Creation and use of Lists/Tuples in Python – https://elusblog.wordpress.com/2017/02/23/wsq07-lists-unfinished/
  18. Creation and use of strings – https://elusblog.wordpress.com/2017/04/21/strings-python3/
  19. Validated user input (ensure correct/expected data entry) – https://elusblog.wordpress.com/2017/04/21/validated-user-input-ensure-correctexpected-data-entry/
  20. Reading and writing of text files – https://elusblog.wordpress.com/2017/03/06/wsq09-multipart-data-and-files-unfinished/
  21. Creation and use of ranges in Python – https://elusblog.wordpress.com/2017/02/14/wsq03-libraries-and-ranges-importing-and-using-moduleslibraries-and-creation-and-use-of-ranges-in-python-python3/
  22. Creation and use of dictionaries in Python – https://elusblog.wordpress.com/2017/04/21/dictionaries/

Dictionaries

--Originally published at Elu's Blog

A dictionary in Python3 is a type of data that contains a lot of information (that doesn’t have an order) and unique tags (called keys) for every piece of information.

Here is an example of defining a dictionary: dic = {‘house1′:’red’, ‘house2′:’blue’, ‘house3′:’green’}

As we can see, the dictionary variable is ‘dic’ and it has three elements, the first one has the key ‘house1’ and what’s in it is the word ‘red’, the second element has the key ‘house2’ and contains the word ‘blue’, finally the key ‘house3’ contains the string ‘green’. Remember that the elements are not in order.

If I wanted to know the color of the house1, I just write dic[house1] and it will return ‘red’.

So dictionaries let you store information that let you access it according to the keys.

Here is an alternative explanation:

Python 3 Programming Tutorial – Dictionaries by sentdex


Validated user input (ensure correct/expected data entry)

--Originally published at Elu's Blog

As programmers we would like that the user always does what they are told so the program runs perfectly. Sadly this doesn’t happen all the time, so we have to create fail-safes in order for the program runs well.

Here is an example:

Captura de pantalla 2017-04-20 a la(s) 19.19.42.png

As we can see in the image above I am telling the user to enter a positive integer, but what if the user doesn’t want? As we can also see, if the user enters a negative integer, the while loop will repeat until it gets a positive integer.

This is just a simple example and a reminder that we a s programmers always have to think about ‘what could the user input in order for the program to fail?’

Here is an alternative explanation:

Validate Users input in Python3 by Eric Israel Reyes Cruz


Strings (Python3)

--Originally published at Elu's Blog

Strings are a basic type of data in Python3. These consist of chains of characters put together. Outside of the computer sciences, they are called ‘words’. But in a program a string could be just ‘lasdlk9(&(=lkasd3smds8098*^sdokads’.

If you have a variable x which has the value 390 in it (x = 390). To transform it to a string, it is very easy, just use the function str(). Like this: x = str(x). This will make it so x is ‘390’ instead of 390. The difference is that when 390 is a string you can’t do ‘x + 3’ and expect it to return a 393, it won’t work because the 3 and the ‘390’ are different types.

Sum and multiplication of strings

If you have a variable x = ‘egg’ and the variable y = ‘foo’, if you write ‘x + y’ the result will be ‘eggfoo’, the strings combine to make a new string.

If you have a variable x = ‘egg’ and write x *= 3, the result will be ‘eggeggegg’, it will add the same string three times.

Here is an alternative explanation:

Python 3 Tutorial: Print Function and Strings by sentdex


When to use what type of repetition in a program (Python3)

--Originally published at Elu's Blog

We have three repetitive functions in Python3:

  1. For loops. In Python3 ‘for loops’ are best used when you want to edit or analyze a string, list, tuple or dictionary which number of things in it is defined. In other languages like C, ‘for loops’ and ‘while loops’ are used by preference of the programmer, they are interchangeable.
  2. While loops. They are used when the times of the repetition vary.
  3. Recursion. They are used when the solution to a problem is defined by the solution to smaller instances of the same problem.

As easy as that, you want to analyze a list? Use a for loop. Want to print as many ‘Hello World”s as the user wants? Use a while loop. Want to get a fibonacci number in a series? Use a recursive function.

Here are links to explanation of every repetitive function:

For loops: https://elusblog.wordpress.com/2017/02/14/wsq04-for-loops-python3/

While loops: https://elusblog.wordpress.com/2017/02/14/wsq06-while-loops-python3/

Recursive functions: https://elusblog.wordpress.com/2017/04/20/recursion-python3/

 


Recursion (Python3)

--Originally published at Elu's Blog

As Ameer Fazal said in a video “recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem”. Note: that video is linked at the end of this post.

Here is an example of a recursive function:

captura-de-pantalla-2017-03-02-a-las-10-45-29.png

As we can see in the fibonacci function the fibonacci function is used. Wait what? Fibonacci-ception? It is very interesting and yet confusing. The way to understand it is by trying to use the function with a few examples. The first example, what happens if n = 0? It returns 0, ok easy. Second example, what happens if n = 1? It returns 1. Third example, what if n = 2? To answer that let’s go step by step:

  1. Identify line 4: ‘else: return (fibonacci(n-1)+fibonacci(n-2))’. As we can see it will return the sum of ‘fibonacci(n-1)’ and ‘fibonacci(n-2)’.
  2. ‘fibonacci(n-1)’ n = 2, so n – 1 = 2 – 1 = 1. And we already established that fibonacci(1) returns 1 so we got the first part of the sum.
  3. ‘fibonacci(n-2)’ n = 2, so n – 2 = 2 – 2 = 0. And we already established that fibonacci(0) returns 0 so we got all parts of the sum.
  4. At this moment, line 4 should hypothetically look like this: ‘else: return (1 + 0)’
  5. ‘else: return (1)’ and the function overall returns 1.

Recursive functions look like tree when processed as we can see in the image below:PSCLH.png

I know this is a hard topic to understand, especially if read and not heard or seen in another graphic way, so here is an alternative explanation:

Recursion : Python Tutorial #13 by Ameer Fazal


Creating and using your own modules/libraries (Python3)

--Originally published at Elu's Blog

A library is a Python3 file that contains functions. We have already seen how to create a function (here is a link to the post were I explained them: https://elusblog.wordpress.com/2017/01/31/functions-python3/). The way to create a library is just to create a Python3 file and add functions. Then to use them in another code, the ‘import’ method is required. It’s very important that the two files (the library and the code you are working on) are in the same folder.

Here is an example:

  1. First, a random file with functions in it. (This is what I am using as a library): Captura de pantalla 2017-04-20 a la(s) 18.21.20.png
  2. Then a file where I use a function from my ‘library’:Captura de pantalla 2017-04-20 a la(s) 18.21.05.png

As you can see first I wrote ‘from’ then the name of the library file (in this case ‘SPE17’), then ‘import’ and to finish the function that I want to use.

There is another way, where you write ‘import’ and the library filename. This will import all the functions from that library but to use them you will have to write ‘[library filename].[function]’. In the example above I would’ve wrote ‘import SPE17’ in the first line, and when would’ve wanted to use the function I would’ve wrote ‘SPE17.phrase_palindrome()’.

Here is an alternative explanation:

Python 3 Programming Tutorial – Making Modules by sentdex


Python Conventions (Zen of Python)

--Originally published at Elu's Blog

The Zen of Python is a list of ‘rules’ that you should follow when coding in Python3 in order to have the best code.

This is it:

Captura de pantalla 2017-04-20 a la(s) 18.06.56.png

It can be found in the Python’s webpage: https://www.python.org/dev/peps/pep-0020/

The conventions are pretty straightforward “Simple is better than complex.” for example. In a few words, what the Zen of Python is saying, is that you have to write your code in a way that another person can easily understand it without explanation.

Here is a more in-depth explanation:

PEP 20 Presentation – Zen of Python by Maximilian Harris, Jessica Lynch and Noah Dillon


Use of comments in Python3

--Originally published at Elu's Blog

Comments are just notes that you can write on your code. When you have a code with 100 lines, comments are very helpful for knowing what each part of the code does. To write a comment you just write the ‘#’ symbol and then write your comment. As easy as that.

Here is an example:

Captura de pantalla 2017-04-20 a la(s) 17.47.22.png

Captura de pantalla 2017-04-20 a la(s) 18.00.15.png

Here is an alternative explanation:

How to Write Comments in Python