Author Archives: Manuel Madrigal

Mastery 25. Creation and use of ranges in Python

Range is a very useful built in function in Python. It allows us to create lists with arithmetic progressions.

The syntax is the following:

     range (start, stop, step)

Where the arguments have to be integer numbers. We can omit the “start” argument and Python sets it to 0 by default. We can also omit the “step” argument and it will be set to 1 by default.

It is also important to remark that the “stop” argument is never taken into account as part of the generated list.

Here are some examples:

>>range(5)

[0,1,2,3,4]

 

>>range(1,7)

[1,2,3,4,5,6]

 

>>range(20,100,10)

[20,30,40,50,60,70,80,90]

 

Mastery 24. Creation and use of tuples in Python

In this post I will explain you what tuples are and how to use them.

If we remember from an old post, we saw that we could group items by using lists. Tuples are just like lists, but the main difference is that once you had created a Tuple, you can not modify it, as we could with lists.

The syntax of a Tuple, different from lists where we used brackets, is the following:

     Tuple=(item1,item2,item3,itemN)

Where item1, item2, item3 and itemN can take any value, it doesn’t matter if it is a string, integer number, or even a list or another tuple, etc.

We can refer to values of a tuple as we refer to values of a list. The syntax is the following:

>>Tuple[2]

item3

Where 2 referes to the third item because it starts counting from 0.

 

We may not be able to modify Tuples but we can always modify a list inside the tuple, as soon as we dont wanted to put up any other value instead of the list itself.

 

Quiz09

For this post I had to upload the correct solutions for my partial 2 exam. As I had all the problems correct, I didnt have to do nothing than uploading them to my GitHub account.

Here are the links to my solutions:

q1: https://github.com/Manuelmv94/Quizz09/blob/master/q1.py

q2: https://github.com/Manuelmv94/Quizz09/blob/master/q2.py

q3: https://github.com/Manuelmv94/Quizz09/blob/master/q3.py

q4: https://github.com/Manuelmv94/Quizz09/blob/master/q4.py

 

WSQ15. Final Dash

There are only 3 and a half weeks left until the end of this course, and we want to ensure that all of the work is done by May 6th, that’s why here I am going to make a list of what am I missing and a schedule to achieve the missing work.

I have done week by week the WSQ’s that Ken assign us and I plan to keep doing it that way, so i dont miss any of them. As soon as they are released I will start working on that.

Talking about the masteries, I am missing 7 of them and 8 points, so I have to do at least one video for one of them. I am planning to do all of them on the next two weeks, so, at the end, I dont get stressed because of other projects from another classes.

The masteries that I am missing are:

Mastery 18. Nesting of conditional statements

Mastery 24. Creation and use of tuples in Python

Mastery 25. Creation and use of ranges in Python

Mastery 26. Creation and use of strings in Python

Mastery 27. Creation and use dictionaries in python

Mastery 29. Validated User input in Python

Mastery 30. Reading and writing of files in Python

 

I hope that I can do at least four of them by this weekend and the rest by the next week.

 

 

WSQ14. Calculate e

For this wsq I had to create a program that calculate the arithmetic value for “e”, using the infinite series.

First I created a function for calculating the factorial of a number and then another function for calculating the number “e” in which I used the function I created first.

The way of calculating “e” is adding 1+ 1 over the factorial of 1 + 1 over the factorial of 2 and so on until the last two numbers were the same. So, I created a while loop to recreate this behavior.

Also, the function asked for certain accuracy of the number, so using the built in function “round” I could return the number  “e” with certain number of numbers after the decimal point.

Here is the link to my code:https://github.com/Manuelmv94/TC1014/blob/master/wsq14.py

WSQ13. Babylonian method

For this wsq I had to create a program that asks the user for a floating point or integer number and returns the square root of the number.

The challenge of the program was to perform this task without using the mathematic function for the square root, so, using a bunch of loops we could first give an approximation of the square root of the number, then, getting the average between the approximation and the number divided by the approximation, we get a new aproximation, and we keep doing the same proccess until the new approximation is equal to the last one. There is when we have found our square root of the number provided.

Here is the code for my program: https://github.com/Manuelmv94/TC1014/blob/master/wsq13.py

WSQ12. Greatest Common Divisor

For this wsq I had to make a program that asks the user for two positive integer numbers and returns the greatest common divisor between those two numbers.

First of all, I defined a function with wich, using Euclid’s algorithm I could accomplish the objective of the task, I just created a recursive function using some if’s and elses according to the criteria.

Outside the function, in the actual coding, I created some loops to verify that the input users were the intended inputs, avoiding errors of data-type.Also, I created an extra loop containing the whole code to keep asking for more values until the user decides to stop.

Here is my code: https://github.com/Manuelmv94/TC1014/blob/master/wsq12.py

Mastery14. Creating and using a Python module

Modules are Python files that contain pre-built functions that are very useful. Python already has some modules that come with it at the moment you download it but we can create our own modules too.

To create a modules we have to create a new file with the extension .py and save it on the main folder of python because there is where python looks after we call a function. The direction of this folder can be found on C:Python34 but this may be different depending on what version of Python you have.

In this file we have to create the functions that we are going to use after importing the module. If you dont know how to create a function you can check a post I did where I explain it in here: https://manuelmadrigal.withknown.com/2015/mastery12-creating-python-functions

After this file is created we have to import it in the new file where we want to use this functions. You can also check my post about Importing and using python modules in here: https://manuelmadrigal.withknown.com/2015/mastery13-importing-and-using-python-modules

If you want to learn more about Creatin and Using Python functions you can check this video:https://www.youtube.com/watch?v=xRQA1Fy1HFQ

 

Mastery09. Basic Types in Python and their use

Data-types are different types of ways we can store data. The most basic Data-types are the numbers (which we can divide in integers and Floating point numbers) and strings.

Numbers:

>>>An integer is a whole number form the range of negative infinity to infinity, e.g., -5, 0, 17, etc.

>>>A floating point number is any number that has digits after a decimal point, e.g., 2.4, 132.8441, 3.333, etc.

Strings:

A string is a piece of text that can be a set of letters, numbers or other characters. To include a string in our program, we have to sorround it with quotes or double quotes. e.g.:

>>> x= “This is a string with double quotes”

>>> y= ‘This is a string with simple quotes’

And the work exactly the same.

 

There are some more complex Data-types such lists, tuples and dictionaries.

Lists:

A list stores a bunch of values and keeps them in order for the user; the user can add, delete or modify any item from the list whenever he wants. A list doesnt have a fixed number of values, the user can modify it as he wants. Lists are characterized because of the square brackets. For example:

x=[1,2,3,4,5]

Tuples:

Tuples are a lot like lists but the do have a fixed number of values that can not be modified. Different from lists, tuples are defined with parentheses instead of square brackets. For example:

x=(1,2,3,4,5)

Dictionaries:

Dictionaries contain a key and a value, so ypu can look up for the value later using the key. We can use dictionaries to build complex nested data structures or just to store values. The syntax is the following:

x={‘key1’: ‘value1’, ‘key2’: ‘value2’}

 

You can learn more about data-types in here: http://www.voidspace.org.uk/python/articles/python_datatypes.shtml

 

mastery04. Submit work via Blog RSS and Github

To submit work via Blog RSS I selected withknown which seems a pretty good Blog. So far it has been very easy to use and it looks very clean with a good interface.

To submit work via Github we have to install the client in our computer, which can be downloaded for free direclty from their page. Also we have to create a repositorie to submit our python programs. Once it is installed we just have to place our work in the corresponding folder and and synchronize the repositorie on the github client.

 

Here is a video from our professor where is explained better: http://youtu.be/YQmlksGFZWY