Extrablog: ECOAS

--Originally published at AgCoding

Las ECOAS son una encuesta que se le hace a los alumnos al final de cada semestre, en las cuales se les pregunta sobre diferentes aspectos de la escuela, desde los servicios brindados hasta como notaste a los maestros.  Y mi maestro ken piensa que las ECOAS son bastante importantes para los maestros, por lo tanto el día de hoy explicare el porque lo son, o al menos desde mi punto de vista.


Reading

--Originally published at Coding with jared

Just learned how to open .txt files on the terminal using python 3, it is very easy to use, you will feel like a hacker just to open a file for reading it.

the comand we need is open(“filename.txt”)

VERY IMPORTANT!

The .txt files and the .py file must be in the same directory in order to proceed.

Screenshot from 2016-11-20 20-13-25.png

My .py and .txt files are in my Desktop, you cans see that I got two books, the Bible and a book of Harry Potter, now let´s move on to the code

Screenshot from 2016-11-20 20-14-45.png

Here I am making a menu of the books available, and also I am asking the user to enter not the number, but the name of the book. if the name of the book is equal to Harry Potter or The Bible, the code will continue, we must name a variable, I called it “text” equal to open(“nameofbook.txt”).

After that wee need to print the file in the terminal so we write this, print (text.read()).

Here is how it works on the terminal…

Screenshot from 2016-11-20 20-15-15.png

Screenshot from 2016-11-20 20-15-31.png

Now I can read Harry Potter and the chamber of secrets on my ubuntu terminal..

Watch where i learned about opening filess.


You can´t do that

--Originally published at Coding with jared

Today´s blog is about validation of the user input, before starting with this topic, we need to know what is data validation.

data validation: Making sure that a program operates based on clean, correct and useful data.

data validation in programming is needed to continue procedures, for example if a program is asking for an integer, and a the user gives a string as answer the program, may crashed or send an error message that the answer given is nott a correct option in order to continue the program.

The errors are handled with try and except.

Try works as the follow

try: ´´´ Instructions …´´´

except : ´´´Ínstructins…´´´

first all the instructions from try to except are executed, but if no exception is find that block ends and now we continue to the block except.

For example, in this program of my calculator in Spanish, i am giving the user the option to select an what operations he/she wants to do, but there are only four options, numbered from 1 to 4, and to select it you just have to select the number and press enter, but what if the user is a smart ass and instead of give as answer the given options he/she decides to enter another number…

Screenshot from 2016-11-20 19-06-11.png

know i am able, to outsmart the smartass

 

Screenshot from 2016-11-20 19-12-21.png

 

This is where I learned about validations, try and except statements :9

https://docs.python.org/3/tutorial/errors.html


Dictionaries

--Originally published at Hector Martinez Alcantara

A dictionary is like the lists, is a list of elements of different types, or the same type identified by a special name.

This is the sintaxis:

dict = {'identifier1': 'Element1', 'identifier2': 2, 'identifier3': 'Element3'}

You can access to a dictionary by the identifier of the element.

print(dict['identifier1'])  #Result: "Element 1"
print(dict['identifier2'])  #Result: 2

You can update a dictionary by rewriting it, or by simply assigning a value to the element:

dict['identifier1']= "New value"

To delete an element of the dictionary, you have to use the reserved word del or the method clear to delete all the elements in the dictionary:

del dict['identifier1']
dict.clear()

Python includes the following dictionary functions −

Function with Description
cmp(dict1, dict2)

Compares elements of both dict.

len(dict)

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

str(dict)

Produces a printable string representation of a dictionary

type(variable)

Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type.

Python includes following dictionary methods

Methods with Description
dict.clear()

Removes all elements of dictionary dict

dict.copy()

Returns a shallow copy of dictionary dict

dict.fromkeys()

Create a new dictionary with keys from seq and values set to value.

dict.get(key, default=None)

For key key, returns value or default if key not in dictionary

dict.has_key(key)

Returns true if key in dictionary dictfalse otherwise

dict.items()

Returns a list of dict‘s (key, value) tuple pairs

dict.keys()

Returns list of dictionary dict’s keys

dict.setdefault(key, default=None)

Similar to get(), but will set dict[key]=default if key is not already in dict

dict.update(dict2)

Adds dictionary dict2‘s key-values pairs to dict

dict.values()

Returns list of dictionary dict‘s values

Thanks for reading, and special thanks as usual to Tutorialspoint who always have good information.


Ranges in python

--Originally published at Hector Martinez Alcantara

A range is a list of elements from one point to another.

The sintaxis is:

range(condition)

You can do a range to be from some number to another, and with some condition

Examples:

var=range(9) #Result:0,1,2,3,4,5,6,7,8
var=range(3,7) #Result:3,4,5,6
var=range(0,100,10) #Result:0,10,20,30,40,50,60,70,80,90

In the first example the range begins in 0 and finishes in 8 returning a list from 0 to 8.

In the second example the first number represents the first number of the range, and the second number the finish, but always the number of finish will be one less than the specified.

In the third example, is the same as the second, the first number indicate the beginning and the second number indicates the finish, but the third number indicates how many numbers separate one number from another.

Thanks for reading, comment.


Reading and writing of text files

--Originally published at Hector Martinez Alcantara

To write or to read something from a text file, first you have to open a file.

This is the sintaxis of how to open a file:

file object = open("file_name_with_extension_and_directory","type_of_open")

Here is a list of the different modes of opening a file −

Modes Description
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
Continue reading "Reading and writing of text files"

Validation

--Originally published at Hector Martinez Alcantara

A validation is when you request an specific type of value from the user, so, you have to force the user to type what you want.

Let’s do an example:

var=input("Type a number\n")
while str.isnumeric(var)== 0:
 var=input("That's not a number, please type a number\n")
print("%s is the number you typed"%(var))

First, you require a string from the user, if the string is not a number, the program will request the number again.

That’s the main idea of a validation, depends on you how many conditions you want in your validation.


Strings

--Originally published at Hector Martinez Alcantara

A string is an array of characters like a word or a phrase which have some properties or specific functions to modify or analyze the string.

First, how to inicialize it?

var="Hello world"

All the characters between the quotes are part of the string, but how to access it?

print(var) #The result is 'Hello world'
print(var[3]) #The result is 'l'
print(var[0:4]) #The result is 'Hello'

You can modify the strings like this:

var="Another value to the string" #Modifying all the string
#Result: Another value to the string
var= var + " with something more" #Add characters to the string
#Result: Another value to the string with something more
a="Something"
a=a * 3 #Repeating the string
#Result: SomethingSomethingSomething

You can also access to strings into another string made by a print with the operator %s:

var="Another value to the string" 
a="Something"
print("There are %s i want to say, like: %s" %(a,var))
#Result: "There are Something i want to say, like: Another value to the string"

As you can see, you access the strings in the %() in order that they were write

There’s a lot of types of values you can acess in a string, like the table below.

Format Symbol Conversion
%c character
%s string conversion via str() prior to formatting
%i signed decimal integer
%d signed decimal integer
%u unsigned decimal integer
%o octal integer
%x hexadecimal integer (lowercase letters)
%X hexadecimal integer (UPPERcase letters)
%e exponential notation (with lowercase ‘e’)
%E exponential notation (with UPPERcase ‘E’)
%f floating point real number
%g the shorter of %f and %e
%G the shorter of %f and %E

And also there are some functions prefurbished to the strings listed in the table below:

Methods with Description
capitalize()
Capitalizes first letter of string
center(width, fillchar)

Returns a string padded with fillchar with the

Continue reading "Strings"