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:/
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”)
]]>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:
]]> .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: “)
]]>
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.
]]>
The use of recursion for repetitive algorithms means calling a function inside the function, like in my example.
here is my code: https:/
Introducing a conditional with an else.
Here is my code: https:/
]]>
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.
]]>
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:/
]]>
Taken from: http:/