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

 

 


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: 


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

 


Creating you own functions

--Originally published at Codebuster

So as said before, functions are pretty useful, but what if we need one that does not come prepackaged? or even better, what if we want to create one our own, its fairly simple.

The first thing we need to do to start wrtting a function is define it, using “def” a space and the name you want to assign to your function. You want to be careful how you name your function, so it doesnt get mixed up with other values,

Next you should write the parameters for your function between parentheses so like this:

def function1 (parameter):

here you would write what you want your function should do

 

Here is an actual function to calculate the volume of a figure

function-atom

Which would give back:

function-cyg

That’s as easy as it is, enjoy!


Functions

--Originally published at Codebuster

Functions are one of the most useful aspects of programming, we can view them as a routine, that is to say a set of actions to accomplish something. A way to think about it is this: every morning before leaving for school I: change my clothes, brush my teeth, pack my bag, and spray some perfume, this individual actions all fall in the “function” of getting ready.

Generally, functions “take in data” (input) process it and finally “return” a result box.gif(output).

A big advantage of functions is that they can be reusable. Just like try to avoid using plastic bottles not to contaminate the environment we try not to use the same lines of codes over and over again, and instead we use a function.

Luckily, Python (like most programming languages) comes with a prepackaged, prewritten set of functions, which you can access by clicking the package.

Calling a function basically means to tell it to run. To call a function you need to type its name and the parameters in the brackets.

>>print (parameter):

 


Basic input

--Originally published at Codebuster

For us, input can mean something someone else says, and in coding it is not so different. Input is a way to interact with the uer by asking them to assign a value to something.

There can be different kinds of input according to the variable it will store, which can be integers, floats, simple string or so on. You can define what kind of input your user will introduce by defining it when you are writting a code, as follows:

inputatom.PNGinputcygwin

Likewise, if you are going to be using decimals you can specify it by using float

input2atom.PNGinput2cyg

Regardless, for basic handling of the input, like printing it, you can simply write it as

variable= input(“Whatever you want to put”)

thats all folks.gif