Tag Archives: #66757f

Cars

                                                                                                                     @PablO_CVi

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.

Here is my code: https://github.com/PablOCVi/WSQ/blob/master/WSQ16.py

User input (text based) in Python (basic)

                                                                                                              @PablO_CVi

An user input is to give values to the variables, this inputs are in the programs that need the user to interact with it, asking numbers to do operations or actios, to do this you have to type the variable you are going to give a value like this x=, then you have to use the comand input followed by parenthesis and in the middle of this any phrase to request the user to type a value. Example: x=input(“Ingresa un valor”)

Reading and writing of files in Python

                                                                                                                     @PablO_CVi

For reading and writing of 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.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

I have learned all of these here: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python.

Validated user input in Python

                                                                                                                      @PablO_CVi

This is very useless, because helps to make a good and clean code without too many error to fix, like this example:

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: “)

 

When to use what type of repetition in a program

                                                                                                                      @PablO_CVi

The for statement iterates through a collection or iterable object or generator function.

The while statement simply loops until a condition is False.

It isn’t preference. It’s a question of what your data structures are.

Often, we represent the values we want to process as a range (an actual list), or xrange (which generates the values). This gives us a data structure tailor-made for the for statement.

Generally, however, we have a ready-made collection: a set, tuple, list, map or even a string is already an iterable collection, so we simply use a for loop.

In a few cases, we might want some functional-programming processing done for us, in which case we can apply that transformation as part of iteration. The sorted and enumerate functions apply a transformation on an iterable that fits naturally with the for statement.

 

If you don’t have a tidy data structure to iterate through, or you don’t have a generator function that drives your processing, you must use while.

 

 

Use of recursion for repetitive algorithms

                                                                                                                      @PablO_CVi

The use of recursion for repetitive algorithms means calling a function inside the function, like in my example.

here is my code: https://github.com/PablOCVi/Mastery/blob/master/Mastery21.py

Use of “else” with a conditional

                                                                                                                          @PablO_CVi

Introducing a conditional with an else.

Here is my code: https://github.com/PablOCVi/Mastery/blob/master/Mastery16.py

                           

Creating and using a Python module

                                                                                                                       @PablO_CVi

For creating a module you have to create a function, save the function in pithons location and import the module before calling the function in the future, as you can see in my video.

 

Importing and using Python modules

                                                                                                                       @PablO_CVi

Modules helps the programmer and makes easy to make some operations, because modules has some functions ready to be used, before call this functions, you have to import the module. Here is the link where you can find allt he modules that pyhton has: https://docs.python.org/3/py-modindex.html

 

Basic types and their use in Python

                                                                                                                       @PablO_CVi

Types are a category for things within Python with which Python will work. Types are:

 

integers 
Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.
float 
Short for “floating point number,” any rational number, usually used with decimals such as 2.8 or 3.14159.
strings 
A set of letters, numbers, or other characters.
tuples 
A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.
lists 
A list without a fixed number of elements. ie x=[1,2,3] note the square brackets, a list
dictionaries 
A type with multiple elements i.e. x = {1: ‘a’,’b’: 2,3: 3} where you address the elements with, e.g., a text..

Taken from: http://en.wikiversity.org/wiki/Python/Basic_data_types