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


The basics of output

--Originally published at Codebuster

Fun fact: “Anything you view in your computer monitor is output”. That sounds a little bit intimidating, but in reality the basics of output can be fairly simple.

One of the first (if not the first) built-in classes people learn in any programming language is print. In fact, we have already worked with it in the Hello World program. Output is basically the computer communicating to us the results of what we aked the code to do. That easy program we made, already taught us output through print.

Print is useful when you want a value, variable, statement etc. in text. All you have to do is call the built-in class print and write what you want to be displayed (dont forget to put this between parenthesis). If it is a simple statement you should write it between brackets, but you can also define a variable and later ask the program to print it (notice that when we print variables we don’t use brackets). Even better, you can print several things at once:

output example1.PNG

And we get:

output in cygwin

Fun fact No.2, when you are using print if you write you code like this:

print (“Something”+variable)

The message will appear with no spaces between the words. However, if you write it as:

print (“Something”,variable)

5.gif
Automatically a space is left between the two values.


When you try your best, but you dont succeed

--Originally published at Codebuster

In order to attack and avoid them, you need to know your enemy, in this case the types of errors that could avoid your program running. Errors are commonly divided in three categories:

  • Syntax errors:

A syntax error occurs in the structure of a program, so like grammatic rules in English. A computer can’t execute a program if it has one of this erors. If a program is syntatically wrong, Python will kindly let you know by returning an error message.

  • Runtime errors:

Another type of error is called runtime error, given that it only appears when you run the program. They usually mean something important has gone wrong.

  • Semantic errors:

Finally we have semantic errors. This are tricky ones, because the program will run without displaying an error message, but it won’t do what you meant it to. So basically you succesfully wrote a program, just not the one you needed.


Right around the corner

--Originally published at Codebuster

Before you start programming you should probably consider, idk, actually downloading a program.

Download Python3

Besides this, you will need a terminal, Mac already comes with a great terminal, but for windows users its best if you download you own.

  • Cygwin (if you are downloading cygwin it’s best to download python from this package)

And a text editor


The Zen of Pi

--Originally published at Codebuster

Each programming language comes with a set of coding conventions, which are basically a 101 for recommended programminh styles, file organization, programming principles and such. Their main function is for people to improve readability of the source code making software maintanance easier.

This cinventions for python are appropiately called the Zen of Python and they are 20 (sometimes considered 19) principles written by Tim Peters around June 1999, and read as follows.

 

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!

I took the liberty of highlighting in pink my favorite ones, and the ones I feel represent python the most (at least for me), since python’s most distinctive feature its simple way of coding and readability, as once Harold Abeson said…

“Programs must be written for people to read, and only incidentally for machines to execute.” – Harold Abeson, Structure and interpretation of computer programs

 

tumblr_me3zq62GuN1r6h22v.gif

 

 


Lord of the Comments

--Originally published at Codebuster

As you navigate your way trhough programming you will acquire more knowledhe, and will create more complex programs each time, this programs can become hard to read, thus comments are used. Comments are the notes that you leave in a program to remind yourself, or to explain to another person what the program is doing. Since you can’t reach from the other side of the screen, comments are a pretty important deal. The very best thing about comments is, they are completely ignored by the interpreter, so feel free to use them as often as needed to make your program more readable.

There are two types of comments, if it’s a one liner,

comment i g.PNG

To put a comment just type a #before what you wish to say

Should’nt be hard, since we constantly navigate a #infected world in twitter and instagram.

Or in case of long comments,

other comment

If you have a lot to say, just use “”” before the comment “””

 

 

 

 


Becoming a polyglot

--Originally published at Codebuster

Just as us, computers have different languages. Their aim is the same, a platform where you can code, but there are differences in their expressions, statements, and so on. Programming languages are known as fromal languages, and can be classified as low-level and high-level.

Low-level languages are all of the machine or assembly languages. They are encoded in binary, so the computer can execute them imediately. So then why the need for high-level languages?

High-level languages are simply easier for us, they are faster to writer, easier to read and less likely to have errors. A great advantage is that they are portable, so they can run on several computers without the need of modifications, which is why they are most commonly used. The ten languages every aspiring progammer  should learn (or at least understand) according to mashable are:

  • Java
  • C language
  • C++
  • C#
  • Objective-C
  • PHP
  • Python
  • Ruby
  • Javascript
  • SQL

Python is highlighted since this is the language this blog is based on.


Shell mode or editor

--Originally published at Codebuster

So you’ve got your idea and finally gather the wits to start to write your very first code, but… how?

Well, in python (and must other languages) you can choose from two alternatives, depending on what your goal is.

First we have the shell, to open the shell all you have to do is open the terminal you are working with, type “python3” (or the version you are using) on the command line:

opening shell.PNG

And you can immediately start writing code, but do keep in mind that the shell is in real time, best for quick calqulations or operations.

basic shell operation

However, if your aim is to write a longer code, or to use loops or such, you’re better off using a text editor, make sure to save the file with a .py

Then, just call it on the command line of your terminal and you are all set.