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

 

 


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.

 


Hello, world

--Originally published at Codebuster

Almost like a fraternity initiation, hello world is the first program the majority of us creates in a new language, because it is the most basic of the commands. Some people actually judge the quality of the programming language by  seeing how easy is it to do this “Hello World”, which would mean, by this standard, that python is top notch.

We can do this in an editor and then run it:

hello world codehello world.PNG

Or we can even do this simple passing of the torch in the shell mode:

hello world shell


Lost in translation

--Originally published at Codebuster

I’ve met several foreigners in my life and the’ve all told me the same thing. Spanish is such a hard language to learn- we have so many idioms and slang varying from state to state. Well, if its hard for people, it sure as hell is worse for computers. Italian, German, English and such are all natural languages, but computers can’t run on this. They use what is called a formal language, this is an important difference.

In a formal language, a statement has one -and only one- meaning, despite redundancy.  They need to be very concise and specific, since they literally mean what is written, a misplaced comma or spelling error could be fatal.

literally.gif

Lastly, because of the way they are written, formal languages shouldn’t be read like we read a normal book. Instead, we should learn to break up the program in our head, interpreting the structure.


A bug’s life

--Originally published at Codebuster

A big tremendous part of any programmer’s life, whether we like it or not is debugging.

Debugging sounds like a repulsive word when in reality, although frustrating, it is one of the most challenging parts of programming.  As much as we want to, humans aren’t perfect. So it is only natural that since programming is done by us, the not perfect humans, errors happen.

This errors are called bugs, and as such, tracking them down and getting rid of them is what is known as debugging.

yelling

There are three types of errors: syntax errors, runtime errors and semantic errors.

So how exactly do you debug? Well, if you fancy yourself a Sherlock Holmes, you are in luck. In order to debug, you gather clues and have to infer what led to the error. Once you cluing for looks.gifhave a thought of where things went wrong, you modify your programm and try anew. If you were wrong, you simply have to come up with a new hypothesis of what the problem might be, but if you happen to be correct, that takes you closer to your dream program.

 


The chosen Py.

--Originally published at Codebuster

Python is generally considered a great first programming language, but what makes it stand out from the rest languages? Is it the fastest? the most used? the best?

  1. It is easy
    For real tho, if you are just getting started on writting programs, hearing about input, output, loops, functions and such can send a chill down your spine – but fear not – Python designers had this in mind, so much so that one part of the Zen of Py literally says it ought to be simple.
  2. It is a trampoline
    By learning Python you learn the basics of most programming languages, so that later you can jump into them deeper. Python is object oriented just like Javascript, C++, Perl, Ruby and such, so you can use your knowledge of Python as a foundation for them.
  3. Recognition
    Python is currently one of the most used languages by tech companies. Yahoo!, Google, Nokia and IBM are all known users, and the community keeps growing.
  4. Get online
    We all know The New York Times or The Guardian, and if you don’t, then at least you’ve heard of Pinterest and Instagram; but did you know that Django, the web application they are foundated in, is written in Python?
    wicked.gif