Skip to main content

Gil

I suck at programming.
I'm not a good student.

#mastery26 Creation and use of strings in Python

1 min read

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

#mastery25 Creation and use of ranges in Python

1 min read

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])

>>>#mastery25

>>>#tc1014

GILBERTO ROGEL GARCÍA

SOURCE: Here

#mastery24 Creation and use of tuples in Python

1 min read

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

 

Gilberto Rogel García

#mastery09 Basic types and their use in Python

2 min read

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

Gilberto Rogel García

 

 

#mastery08 Python conventions (Zen of Python)

1 min read

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:

Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!

 

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

 

Gilberto Rogel García

#mastery22 When to use what type of repetition in a program

1 min read

In program i think it's better to make iterative solutions, i'll show you why:

Recursive solution:

The difference is that when using a recursion it takes alot of time to print as you can see in the picture, iterative solution printed the answer when recursive solution is still calculating the fibonacci of 100.

It is also convenient to use recursion because an infinite loop occurs with iteration if the loop test never becomes false.

The difference is that iteration ends terminates when the loop condition falls whereas recursion terminates when a base case is recognized.

Here are some of the differences of both: http://malhar2010.blogspot.mx/2010/12/difference-between-recursion-and.html

Gilberto Rogel García

#mastery21 Use of recursion for repetitive algorithms

1 min read

A recursion is when you make a function and call it inside of itself. The recursion has a condition, this condition is that the recursive function has to have an end (to terminate) in order to be in the program.

A common example of this is the fibonacci sequence:

Gilberto Rogel García 

#mastery20 Use of loops with “for”

1 min read

"i" is the variable that is often used in "for" loops (an iterative loop which means you keep doing something over and over again

For each of the numbers or the things in the range make "i" refer to them and each of those loops is going to refer to each one of those numbers or things

What this program does is that it assigns "i" to every element in that range without including the last number (10)

#mastery19 Use of loops with “while"

1 min read

The "while" loop is a loop (duh) that allows us to repeat actions as long as the condition is true or until the condition becomes false.

This code shows a little example of a while loop:

x is equal to cero and what the "while" means is that when x is lower than three, it will print x and it will turn x into an accumulator that adds one every time that the "while" makes a loop unti x becomes equal or greater than three.