Author Archives: Efrain Duarte

mastery18 Nested if statements

well for this mastery I made a video in wich I explain what is nested if statements and when we used it. For do the video interactive I create a very simple code and run in the terminal.

here is the link: https://www.youtube.com/watch?v=n8kGRnubEoc

 

Quiz11 Gone Bananas for Files

For this quiz I had to create 2 function the first that open a file and read it to get total value,the numbers of the values,average and the standard deviation. I used a for loop for read line by line in my program.

In the second function open and read a file and after that I create a for loop for read line by line and I used the lower case to identify the word “banana”

here are my solutions;

question 1: https://github.com/EfrainDuarte95/TC1014/blob/master/quiz11.py

question 2: https://github.com/EfrainDuarte95/TC1014/blob/master/2quiz11.py

there is a link that help me a lot: http://www.tutorialspoint.com/python/string_find.htm

 

QUIZ10 wish lists

For the first one I just creat a function to check all the elements of the list if they are divisibles of three and the numbers that are, it will be adding.

For the second first a Ihave to be source that the lists are the same size then if they are  I use a for loop with using len operation of the list that was the range of my loop.

here is the links of my codes for both questios of the quiz10:

https://github.com/EfrainDuarte95/TC1014/blob/master/q10.py

https://github.com/EfrainDuarte95/TC1014/blob/master/2q10.py

 

Mastery 30 Reading and Writing of files in python

In this mastery I will show you how to read and write files in python.

The first step is to get a file object, and how we do this? well using the open” function.

The open function has two parameter, the first one is the name of the file that we are creating and the other parameter is going to check if you are going to read(r) or write(w) the file. Now for write inside of the file.write the name of the object we created followed by a dot with the word “write” adn you open parenthesis where you are going to write the text.Also is important to mark that the end line character is given by “/n”.And for save it we have to close our object.

this is the syntax:

>> file = open(“newfile.txt”, “w”)

>>file.write(“hello world in the new filen”)

>>file.write(“and another linen”)

file>>file.close()

 

the object name is file and the text file is example.

Now we know how to use write(), I can explain read().

usisng read() is use it for read the text file. the syntax is very simple like in write() method, just where we wrote “w” now we have to write “r”.

this is the syntax of this:

>>file = open(‘newfile.txt’, ‘r’)

>>print file.read()

>>file.close()

In this example show us the two parameters, the first one tell us what text file is going to open and the second parameter tell us what is going to do, in this case “r” means that is going to read.And print mean that when you are in the terminal will print the text that is in the file. In this case it will print:

>>hello world in the new file
>>and another line

We can also specify how many characters the string should return, by using
file.read(n), where “n” determines number of characters.

This reads the first 5 characters of data and returns it as a string.

this will print : hello

There are more operation of this, is just like the operation with strings and line.

this link is very helpful:http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python.

WSQ16 Cars

For this wsq I had to know how to ride and write files in python. After that I create a program that read line by line of the file, but in reality I jus want to take some values of strings of some lines, so inside of the for loop is a code to read one line yes and the next no.

I had to show the average  so I had to know how many cars there are and inside of the loops count the cars.

so here is my code: https://github.com/EfrainDuarte95/TC1014/blob/master/wsq16.py

this link is very helpful to know how to read and write files.

http://learnpythonthehardway.org/book/ex16.html

http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

Bonus quiz

here is a screen shot that show that I already complete the evaluation of my teachers:

ECOS

Mastery 29 Validated User input in Python

Sometimes in our code we want an special type of value and in this case we have to be ensure that the user enters the correct value. So with something we can be ensure, like in the next example is just for that:

while  True:
        try:
            q=int(input(“Please enter a non-negative integer number: “))
            break
        except ValueError:
            print(“this is not a integer number, try again: “)

while q<0:
        try:
            q=int(input(“this is not a positive number,try again: “))
        except ValueError:
            print (“this is not a integer number, try again:  “)
            while True:
                try:
                    q=int(input(“Please enter a non-negative integer number: “))
                    break
                except ValueError:
                    print (“this is not a integer number, try again: “)

 

In this example we want a value positive and integer. so first check that the value that the user enters has to be integer if enters an integer number the value would be through to next condition, and if is not a integer number it will be through to an except ValueError that  just when the user enter the correct value it will through the value to the next condition.

Ok let see, the next condition say that the number it has to be positive if is not, the value will be through to except ValueError again until the value enter for the users be correct.But also the user in this case can enter a positive number but no integer so we have to be ensure again of this with another while true that does the same operations of the last one.

Until the value complete all of the conditions the value will be through to  the funtion or operation that we want to do with them.

this is just an example, it depends of what value you want, you just has to write the statements of “try” and “except” in order of what you want.

 

 

Mastery 26 Creation and use of strings

In this mastery I will write about strings.What are strings? Strings are characters that are enclosing in quotes. As simple as this:

st1= “Hello World”

we can do many operations with strings beginig with print that maybe is the most use it.

we can know the lenght of our string using the len() statement like this:

1.-print len(st1)—> this will give us an 11, because the string has 11 characters including spaces

 Another operation with strings is performed by the index staement like this:

2.-print st1.index(“o”)—>this will give us a 4, because the letter tell us to where we will count, star of 0.

other is this one

3.- print st1[1:3]—>this will give us “el” starting countig from 0 and end in the position 2, like I say in the last post of range, that mean stop that will stop in (stop-1)

 

Another interesting functions are the following:

4.-print string1.upper()
5.-print string1.lower()

Which will print a new string with all letters converted to uppercase and lowercase respectively.

Also we can create a new string with other string but different, what I mean? just like this:

st1 = “Hello World”

print st1[:6] + “python”—–>this will give us “hello python”.

In this link you can found more operations and information about strings: http://www.tutorialspoint.com/python/python_strings.htm

 

Mastery 24 Creation and use of ranges in Python

in this mastery I will write about function range. first what does it do? it generates a list of numbers, which is generally used to iterate over with for loops.

function has two sets of parameters, as follows:

range(stop)

wich it will print numbers beginig from zero to (stop-1). For example:

range(4) == [0,1,2,3]

the other parameters is:

range(start,stop,step)

>start it means in wich number it will start

>stop it means  when is going to stop but like this(stop-1)

>step it means the difference between each number in the sequence

examples of using range:

1.-range(0,11,2) == [0,2,4,6,8,10]

2.-range(3,9)==[3,4,5,6,7,8]

this is a link that its very helpful to understand : http://www.pythoncentral.io/pythons-range-function-explained/

mastery24 Creation and use of tuples in Python

In this mastery I will talk about tuples.A tuple is a sequence of immutable Python objects, is like lists, but they have on especially diference, that ones yu create a tuple you can not modify but in lits you can. Also the sintax of tuples are parentheses and lists use square brackets.for example:

tuple1=(“efrain”,”duarte”,”lopez”,20)

you can write strings,numbers is just like lists.

be careful when you just write one thing inside a tuple you have to write it like this:

tuple2=(“ice”,) ——> such is just one thing you have to add a comma to the end

To acces values in tuples  you have to use square brackets.for example:

tuple1=(“efrain”,”duarte”,”lopez”,20)

print tuple[0]——–>the result will be “efrain”

print tuple[0:2]—–>the result will be [“efrain”,”duarte”,”lopez”]

this is counting beging form 0.

You cant modify a tuple but you can create a new tuple with other tuples like this:

tuple3=tuple1+tuple2

tuple3 will be>>>(efrain”,”duarte”,”lopez”,20,”ice”)

Also the tuples respond to the + and * operators much like strings, and this operations will create a new tuple not just strings .For example the basic tuples operations:

tuple4=(1,2,3,4)

print  lent(tuple4)——>resutl= 4

also we can use Concatenation,Repetition,Iteration.

an important point is that we can modify a list inside a tuple, but the tuple we can n ever touch it.

for more information here is a link its very helpful:

http://www.tutorialspoint.com/python/python_tuples.htm