Category Archives: Python

Python virtual environments

Python allows you to install packages using PIP. This works fine for small projects, but when you want to create a portable application you might run into problems if your application depends on a version of a package that is different to the version that another application depends on. Because by default PIP will install packages in a folder similar to:

1
/usr/lib/python2.7/site-packages/

Read more »

Introduction to PIP

PIP(PIP Installs Packages) is Python’s recommended tool for package managing. Most modern operating systems come with Python and PIP installed. You can check if Python and PIP are installed using the which command:

1
2
which pip
which python

If PIP is not installed you can follow the documentation to install it.

Read more »

Writting Django applications for your project

For this post I am assuming you have already a Django environment setup.

Django apps

Once you have your environment set up, you will want to create apps for your project. Here is what Django documentations has to say about apps:

What’s the difference between a project and an app? An app is a Web application that does something — e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular Web site. A project can contain multiple apps. An app can be in multiple projects.

We can use manage.py to help us create our apps. Just go to your project folder and type this command in a terminal:

1
python manage.py startapp crud

I chose crud as the name of my app because I am just going to show a simple CRUD(Create, Read, Update, Delete) interface to a DB table.

Read more »

Setting up a Django work environment

In my journey to learn python, the next step is to learn Django. Django is a web framework powered by python, so to use it we need to make sure we have python installed:

1
2
adrian@my-xubuntu:~$ python -V
Python 2.7.3

Now we can go ahead and install the Django package

1
sudo apt-get install python-django

That installation makes Django automatically available to python, so you can do something like this:

1
2
3
4
5
6
7
adrian@my-xubuntu:~$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 4, 1, 'final', 0)

Read more »

Introduction to Object Oriented Programing in Python

In this post I am going to explain the syntax for Object Oriented Programming in Python. I am assuming you have already done it in other languages so if you have not done Object Oriented Programming previously you should probably look for another article about it’s principles.

At the beginning when I started learning python I found some things that I felt kind of weird, like the lack of braces and semicolons. Now that I started learning their syntax for OOP I realize that doing things weirdly is their standard. Python (kind of) allows you to implement all the principles of OOP but their syntax is not similar to the one of Java, C++, or PHP that I am familiar with. Anyway so far I don’t see any problem with their different way of doing things so I will just explain how things are done in Python.

Read more »

Creating functions with Python

We can create functions on Python using the def keyword.

1
2
def printSomething():
    print "something"

Functions in python can have arguments and return statements as in most languages.

1
2
def addNumbers(number1, number2):
    return number1 + number2

Read more »

Contributing to OpenHatch project

What is OpenHatch?

OpenHatch.org is a website where you can find ways to contribute to open source projects, mentor people who want to contribute to a project, or give a little more visibility to your open source project so people can help you.

In my opinion the most interesting features are the classification of some bugs as bitesize, which are good bugs for people who want to help but don’t have much experience. This is an awesome place to start. The other interesting feature is the ability to offer yourself as a mentor to fix a bug. If you are an expert on a subject but you don’t have time to fix an specific bug you can list yourself as a mentor and help other people get a bug fixed.

Read more »

Python Sequences – Part 2

This post is a continuation of Python Sequences.

Concatenating Sequences

You can concatenate sequences using the plus (+) operator:

1
2
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]

Multiplying Sequences

You can multiply a sequence by an integer number to repeat it the specified number of times:

1
2
>>> [1, 2] * 3
[1, 2, 1, 2, 1, 2]

Read more »

Python Sequences

Sequences is the name that python gives to a data structure that contains a “sequence” of elements, each with a numeric consecutive index. The sequence is very similar to an array in other programming languages. The first index number is 0 and they increment by one as elements are added.

Sequences can contain numbers, strings, other sequences, etc…

Creating a sequence

You can define a sequence like a list of elements separated by commas and listed inside square brackets ([]):

1
ages = [13, 15, 22, 23, 56, 12, 33]

Read more »

Python strings

Python has a lot of ways to represent strings, so it is useful to be familiar with them in case you ever find them in any python program.

Ordinary Strings

These are probably the most commonly used strings on python. You can enclose them using single or double quotes (‘ or “) and you can use a backslash to escape characters or to print special characters like a line break:

1
2
3
print "Hello\nWorld"
Hello
World
1
2
3
print 'Hello\nWorld'
Hello
World
1
2
print 'This is a backslash: \\'
This is a backslash: \

Read more »