Author Archives: Gilberto Rogel García

#mastery26 Creation and use of strings in Python

Python does not support a character type; these are treated as strings of length one, thos also considered a substring.

To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring.

     Example:

var1= “Hello World”

print (“var1[0]:”, var1[0])

     When the above code is executed, it produces:

var1[0]: H

Gilberto Rogel García

For more info visit:This link

26

1014

#mastery25 Creation and use of ranges in Python

The range() Function

If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates lists containing arithmetic progressions, e.g.:

 

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

The given end point is never part of the generated list; range(10) generates a list of 10 values, exactly the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative):

 

>>> range(5, 10)
[5, 6, 7, 8, 9]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(-10, -100, -30)
[-10, -40, -70]

To iterate over the indices of a sequence, combine range() and len() as follows:

 

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
...     print i, a[i]
... 
0 Mary
1 had
2 a
3 little
4 lamb

My example:

x=[‘mastery25′,’tc1014’]

for i in range (len(x)):

     print (“#”,x[i])

>>>25

>>>1014

25

1014

GILBERTO ROGEL GARCÍA

SOURCE: Here

#WSQ16 Cars

 

I wrote a program that opens and reads the file 93cars.dat.txt which reads a series of data from certain cars and prints their CityMPG, HighwayMPG and their average mindrange price.

I got help from my classroom partners to make this

Link for the code: Here

16 Cars

1014 

Gilberto Rogel García

 

#mastery24 Creation and use of tuples in Python

A tuple is an unchangeable sequence of values.

x=(“Gilberto”,18,”ISC”)   tuple is written with ()

When you do this you create a tuple with three elements. You can access these elements individually by typing the variable and the then inside brackets directly to the right of the variable type the number of the element to which you are referring.

print (x[o])

>>>Gilberto

Python starts numbering at 0 so Gilberto=0,18=1 and ISC = 2

Packing and Unpacking:

In tuple packing, the values on the left are ‘packed’ together in a tuple:

x=(“Gilberto”,18,”ISC”)

In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:

x=(“Gilberto”,18,”ISC”)

(Name,Age,Studies) = x

print(Name,Age,Studies)

>>>Gilberto 18 ISC

Sources for info. about tuples: Link

 

1014

24

Gilberto Rogel García

#mastery09 Basic types and their use in Python

Types are a category for things within Python with which Python will work. Types are integers,float,strings,tuples,lists and dictionaries.

Integers 

Whole numbers from negative infinity to infinity, such as 1, 0, -5, etc.

Integers are numeric values and can be stored, manipulated, and expressed inside variables without quotes.

Example: 

x=5

print (x)

>>>5

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.

Strings are a type. They are the basic unit of text in Python and all the other types except integers may contain strings.

Example:

x=”Hello World”

print (x) 

>>>Hello World

Tuples 

A list with a fixed number of elements. ie x=(1,2,3) parentheses makes it a tuple.

A tuple is an unchangeable sequence of values.

Example:

x = (“element 1”, “element 2”, “element 3”)

Lists

A list is a changeable sequence of data. A list is contained by square brackets i.e. [1,2,3]

Example:

x=[1,2,3]

print (x)

>>>[1,2,3]

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.

Example:

x={‘Hello’:’hola’, ‘Bye’:’adios’}

 

print(x)

>>> {‘Hello’:’hola’, ‘Bye’:’adios’}

Source with more info.: http://en.wikiversity.org/wiki/Python/Basic_data_types#quiz0

1014

09

Gilberto Rogel García

 

 

#mastery08 Python conventions (Zen of Python)

The zen of python is, according to long time Pythoneer Tim Peters, a set of rules you have to follow or at least to take into account while coding in python which are:

Source: https://www.python.org/dev/peps/pep-0020/

08 

1014

Gilberto Rogel García

#quiz11 Gone bananas for files

Codes here

-The first code uses a file named random_numbers.txt

-The second code uses a file named banana.txt and finds the word “banana” in that text and then shows you how many times did it find that word.

 

Gilberto Rogel García

a01630171

#WSQ15 Final Dash

I still have to do 9 masteries and 2 wsqs so my plan is to finish every WSQ and MASTERY before Sunday 3 so i can have plenty of time to study for the final exam on thursday 7th 

Gilberto Rogel García

#ECOS #TC1014

 

Every student must evaluate their teachers in order to know who are the best teachers so that the incoming students know who should they put their classes with, so they can learn more and better that will be useful for them in the future.

Gilberto Rogel García

#QUIZ10 Wish Lists

Useful link: HERE

Link for the codes: HERE

 

Gilberto Rogel García A01630171