Course Review

--Originally published at Codebuster

Here we are, at the end of the race, the literal last classes of the semester. As always, it feels a little bitterweest. The undeniable yearning for classes and finals to end mixd with the knowledge that a whole semester has passed in the blink of an eye. So it’s time to look back and reflect on this past months, what we’ve learned and we could have done better.

Specifically in programming class there are a couple of things I would have tried to improve. Juggling the other courses that had a more conventional way of grading was sometimes easier and sometimes I believe I did procrastinate in the creation of blog posts. However that does not mean I did’nt learn. On the contrary, I feel like I learned a lot.

I did learn the course materials, I had my fair share of mistakes when programming, still do, but I feel like I learned the concepts, not just applying them in certail problems.

On the other hand, I feel like I learned a lot about what the  learning process should actually be, I learned to look for resources myself, especially at this age where all we need is a cellphone or computer to access worldwide information. It was a fun,different, sometimes nervewraking experience. Below I leave a quick video summarizing this.


A strong independent programmer

--Originally published at Codebuster

Creating your own own modules or libraries in python can sound very intimidating, but it isn’t quite so. But, why would you want to create a library or module when you have so many accessible in the web?

Well, even though it might not sound it, creating your own libraries can be quite convinient, since you can define your OWN functions to do what you want them to do.

Here I show and example of how to define and import a very simple library

andrew2

Then import them:

andrew

 

 


Like a guitar

--Originally published at Codebuster

Strings, strings, strings. Such a little word for the concept it holds. Strings are one of the most basic types on python, and therefore they make up a big part of any programmers’ life.

Strings literally mean a chain of characters, any variable that is text-based. It can be letters, words and even numbers. However, numbers in a string format are taken as labels, so you can’t do any mathematical operation with them.

In order to use strings you just need to do is declare it, and declaring it literally only takes quotation marks, as such:

string1= “Hello folks”

Even though they are pretty easy, you can do a number of things with strings, the simplest things you do with them are:

  • Measuring length: You can count how many characters are in a string, in a simple way:
    print len(string)
  • Adding two strings: If you ever want to add two strings, in order to creat a new string you just need to use the “+” symbol:
    NewString=string1+string2
  • Slice a string: Since strings are a number of characters strung together you can check a certain character in that string, for example:
    lastLetter= string[0]
  • Repeat a string: If you ever want to duplicate the string, all you need to use is the “*”:
    Doublestring: String*2

 


To the market!

--Originally published at Codebuster

Lists and tuples are two very powerful tools in python. Both of them are a variable in which you can store different values. This values can be from strings, characters or even numbers, but you can’t have two distint types in the same list.

Lists:

Declaring new lists is pretty easy, you just have to chose a name of the list, open brackets and write the different values that you want to store, closing brackets afterwards, as such:

myHobbies=[“reading”, “writing”, “netflix”, “music”]

 

An important part to remember is that when you create a list each item gets assigned a place in the list, an index number. This starts from cero, so the first element will actually be the number 0. This index allows you to use specific values inside the list, since you can bring it out with

listname[index]

So if I want to remember which was the first hobby I wrote down, I would put:

myHobbies[0]

 

Modifyin lists is actually quite simple, two easy things you can do with them is adding or substracting an item, you can do that like this:

To add: myHobbies.append(“eating”)

To delete: del myHobbies[2]

Tuples

Tuples are very similar to lists. The difference relies in that tuples can’t be altered, so once you write the values down you will not be able to modify them. Declaring tuples is quite similar to declaring lists, it goes as such:

familyMembers=(“Araceli”, “Ricardo”, “Hannia)

Indexes work the same way.

thinkinggggg.gif

 

 


The right one

--Originally published at Codebuster

Okay, so we have all this incredibly useful loops, but how will we know which to use in each program?

No, its not decided by random, nor is it something to panic over. In order to know which repetition to use all you have to do is go back to what they mean, and analize what you want to achieve with your program, what kind of algorithm you want to use, so its important to remember:

  • While: is used when you want to do something until a condition is met, but you don’t really know how many times the loop will repeat itself. It is very common when using it with input from the user, since you don’t know how many tries it will get them to meet your demand.
  • For: For is usually present when we want to do something to every character, string, item and so forth. So as you see, you generally already know how many times the loop will repeat itself

So there it goes, not as hard at all.

iejfioejiofw.gif

Ps: here is a kind of long (but nice) video discussing differences between for and while looks: 


Bird set free

--Originally published at Codebuster

Nesting in programming is defined as:

“Where different logic structures sequence, selection and loop are combined (i.e., nested in one another)

So what this basically means is that nesting is a function within a function. Therefore, a nested conditional would imply a conditional (if/else/elif) inside another conditional (if/elif/else). The funny thing is, a lot of times we use nested conditionals without realizing it, since it can help us break down a function.

nesting-code

As you can see, we use several ifs and elifs inside the main function, which would give us:

nesting-py

You can find some great examples here:

Although nesting can be good for evaluating different conditions at the same time, it can also hinder the program if we use too many of this conditionals. It’s important to remember the zen of python, flat is better than nested. 

 


While you were sleeping

--Originally published at Codebuster

Repetitions are a very important part of any programmers life. It is one of those cheat things you can do when you need to repeat an action, instead of writing the condition over and over again.

One of this loops is “while”. It is my personal favorite because you can use it in various forms. You use the while loop mainly when you want a condition to be reviewed until it becomes true, but (and here is the deal) you don’t really know how many times it will be repeated.

Using while loops is very simple, all you have to do is write “while” and then write the conditions.

x=15

while x<100:

print (x)

x+=5

You can click the image below to see a tutorial for while loops:

while.gif


Bigger than Alexandria

--Originally published at Codebuster

Personally, I love libraries. There is nothing I appreciate more than a good book, and in libraries you can get this books for free, for a certain amount of time. Well, libraries in python (or really, programming) are much better than that.

A library is a collection of pre-writton function. You can import them into your code, so instead of writting a completely new function, you can use the ones that have already been defined. Therefore making your codes shorter, simpler and prettier.

In order to import one, all you need to do is literally call the library’s name, so if I wanted to import the math library (the most basic of all) all i’d do is:

import math

Here you can find the libraries in python. 

 


Conditionals

--Originally published at Codebuster

Another important thing are conditionals, conditionals are a way to check for something, they evaluate true or false and execute something.

The “if” statement evaluates when a parameter given is true, if it is it will do what your specified.

If can evaluate things like

> (less than)
< (greater than)
>= (less or equal than)
<= (greater or equal than)
== (equals)

The “else” statement is a conditional that only executes if the expression “if” results false.

But there is another conditional that lies between these two, called “elif”, this conditional is used when we want to evaluate if another condition is true, in the same block as the first if function, as such:

if  5>6:
print (“Yes”)

elif 7>6:
print(“Yup”)

else 8>6:
print (“Ok”)

In that code, the result would be “Yup” because the code would run and stop after elif, since the condition was true.

Here is another the example I worked on with Max (go check out his blog, it’s a cool one).

code-max

So here’s what would happen if you did pass

max-yes

Oooor, if you didn’t make it…

max-sucks

thats-all-folks

 

P.d. Here is a great video that talks about conditionals

https://www.youtube.com/watch?v=mQrci1kAwh4