Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, January 7, 2015

Python for the Absolute Beginner

If you’re a Programmer / Developer, chances are you’ve at least heard of this neat little program called Python. Those who know it sing it’s praises, and it can be found in some very powerful applications. If you’ve been curious about how to get started with Python I have built this tutorial to help you on your way to becoming a text manipulating ninja.
Ok, that sounded a little corny but Python is really, really good at parsing and manipulating text. It’s one of the duties you’ll see it doing often. Let’s get started.
Step 1:Get Python
I’m not going to go into great detail about installation, but you can get python for any of the big three (PC, Mac Linux) operating environments and the setup is pretty similar. In many versions of OSX and Linux Python is installed by default. One way to see if it’s installed is by opening up a command window, and simply typing “python” or “py”. If it’s installed you’ll see something like this:
[jeremy@localhost ~]$ python Python 2.4.3 (#1, Jul 27 2009, 11:32:33) [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information.
If not, the best place to get the latest version is from Python.org.
Step 2: Start Coding Here is how you should start building Python scripts:
  • Plain Text Editor
  • Running Python at the prompt
The editor I use the most is Kate in Linux. This is because Kate manages your files, and you have a command terminal below, allowing you to enter stuff. If I’m on a windowless machine Vim works very well too. For Windows machines I would recommend Notepad++ to edit. Both are free and there are tons of free text editors out there, especially in Linux.
Your first step is the obligatory “Hello world”. You’ll find it’s nearly as simple as it could possibly be in Python.
Create a file called test.py and put the following line in it:
print "hello world"
and save it. Now type:
python test.py
Amazing right? Now do something silly, to make it spit out an error, and remove the last quote. Type this exactly as you see it:
print "hello world
You should see the following message:
File “test.py”, line 1 print “hello world ^ SyntaxError: EOL while scanning string literal
Like every programming language you have to close everything you open. (With the exception of tags in PHP, but let’s not get into that).
So to clarify, our steps are:
  1. Put python code into a text file.
  2. run that text file with python ( python + filename )
  3. look at the results.
Seems simple enough, so let’s do some more stuff.
Comments
A quick note about comments: use them. When you become a better programmer you can decide whether “good code comments itself” but for now, let’s use comments to explain what’s going on. Basically a comment is any text that YOU can see as a programmer, but the computer can’t. This is so you can leave yourself notes, or notes for other programmers.
In Python, you use the # pound sign for commenting, like so:
# This is a comment
print "hello world"
But usually they’re explaining something a little more important:
# classroom.py

boys = 5 # Boys in the class
girls = 8 # Girls in the class
total = boys + girls # Add up the total

# print our message
print "There are", boys, "boys in the class and", girls, "girls in the class"

# print the total
print "There are a total of", total, "students in this class"
When you run classroom.py the computer (python interpreter to be exact) will ignore all the comments you insert, but they remain in the file for the next person to look at (which could be you).
Speaking of math, let’s do some!
As you saw above it doesn’t take much to do basic arithmatic in Python. Here is one way of performing mathmatic expressions:
# weeksinyear.py

print "How many weeks are in a year?", 365 / 7

See how easy that was? But this prints the answer at the end of the line. What if we want it in the middle of the sentence?

# weeksinyear.py

# figure out the total
weeksinyear = 365 / 7

print "There are", weeksinyear, "weeks in a year"
Notice how easy it was to insert the text into the sentence? That was done with a variable.
Here is what you should see:
There are 52 weeks in a year
You can do all kinds of things with the output, you can output just answers:
print 3+2
But why stop there?
print 3 + (2 + 4) / 2 * 34
You can group expressions and output the answer however you like, or feed it into a variable and use the variable.
Python also outputs boolean (true / false) as well:
print 3 + 3 < 10
Since 6 obviously less than 10 it will output true. This statement isn’t exactly useful, but once you start using variables and inputs it will.
Variables.
Variables are simply containers to put data in. Think of a set of mailboxes for an apartment building. Each mailbox contains someone’s mail (data) and each mailbox is labeled with a number (variable name). The variable is the mailbox, and the number on the mailbox is the variable name.
So let’s create some variables.
# aboutme.py
name = "Jeremy"
age = 34
currentyear = 2012
drivingyears = age - 16
alcyears = age - 21

print "Some facts about",name
print "Jeremy was born sometime around", currentyear - age
print "He's",age,"years
print "He has been driving around", drivingyears, "years"
print "He could has been able to drink alcohol for about", alcyears, "years"
Here you can see we’ve created some variables here to hold my name, age, and the current year. We also added in a couple of formulas to figure out how long I’ve been driving, and (legally) drinking alchohol. If you copy this script exactly (don’t!) you will get the following output:
Some facts about Jeremy Jeremy was born sometime around 1978 He’s 34 years old He has been driving around 18 years He has been able to legally drink alcohol for about 13 years
OK, so this script makes me feel pretty old. Create a similar script for yourself and feel free to modify it. Add in how many years you’ve been married or how long you’ve been in school. I encourage you to take these examples and run with them, modify and go crazy, because that’s the best way to learn. Experiment and don’t worry about breaking things.
Now it’s time to change it up a little.
radius = 2
pi = 3.14

print "The area of our circle is", pi * (radius * radius)
Notice something different? We’re now using a floating point number, which Python detected and used, then output another float. Python supports many different datatypes including integers, long, floats and complex.
In the next tutorial we’re going to get a little more in depth and start exploring some more math, and loops.

Tutorial: How to Connect to MySQL With Python

The more I jump into Python the more I like it. This tutorial is about one of the more basic parts of Python - connecting it to a MySQL database.
The reason I chose MySQL is purely because of ubiquity, I figure this will be the one people will be connecting to the most if they’re using Python especially in a web environment.

Get The Database Setup

If you’re following this excersize exactly, you’ll want to create a table on your MySQL database that holds names. This is just a simple dumb table for this excersize.

SQL to create the table for this tutorial.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE  TABLE `test`.`name` (
  `nameid` INT NOT NULL AUTO_INCREMENT ,
  `firstname` VARCHAR(45) NULL ,
  `lastname` VARCHAR(45) NULL ,
  PRIMARY KEY (`nameid`)
);

INSERT INTO `test`.`name`
(`firstname`,`lastname`)
VALUES
("Cookie","Monster"),
("Guy","Smiley"),
("Big","Bird"),
("Oscar","Grouch"),
("Alastair","Cookie");

Now that you have your highly sophisticated database set up, let’s connect to it and start playing around.

Create Your Python Script

The first step of course is to create your Python Script. Create a file called “datademo.py” (or whatever you want to call it).

Your Initial Python Script.
1
2
3
#!/usr/bin/python
# filename: datademo.py 
# a simple script to pull some data from a MySQL table

Connect to the Database

The first thing you’ll need do is import the MySQL modules with the following line:

1
import MySQLdb

This assumes you have MySQLdb installed. If not, don’t worry it’s a quick install.
Now that you have that set up, let’s get connected:

1
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")

With this string you can connect using your MySQL credentials. If you want you can store these credentials in variables elsewhere.

The Cursor

Python uses a “cursor” when dealing with data. A cursor is a simple data structure that transverses the records in the database. Cursors perform CRUD ( Create Read Update Delete ) operations on the database.

1
2
#create a cursor for the select
cur = db.cursor()

This intializes the cursor so you can use the “cur “object wherever needed. So the next thing we need to do is come up with an SQL command.

1
SELECT firstname,lastname FROM test.name;

This of course selects a first and last name from our database. We want to stuff that SQL command into a parameter for the execute method of the cursor:

1
2
#execute an sql query
cur.execute("SELECT firstname,lastname FROM test.name")

Iteration and Display

The next part of this is iterating through the database result and displaying it.

1
2
3
4
5
6
7
8
9
# loop to iterate
for row in cur.fetchall() :
      #data from rows
        firstname = str(row[0])
        lastname = str(row[1])

      #print it
        print "The first name is " + firstname
        print "The last name is " + lastname

Pretty simple huh? The for loop iterates through the data and produces an array, in this case it’s “row”. You then select the index of that row to get the data from it.
When you run it you should see this output:
The first name is Cookie
The last name is Monster
The first name is Guy
The last name is Smiley
The first name is Big
The last name is Bird
The first name is Oscar
The last name is Grouch
The first name is Alastair
The last name is Cookie
This is just a straight dump of the database. Let’s clean it up little.

1
2
3
4
5
6
7
8
# loop to iterate
for row in cur.fetchall() :
      #data from rows
        firstname = str(row[0])
        lastname = str(row[1])

      #print i
        print "This Person's name is " + firstname + " " + lastname

This obviously is a cleaned up version of the same thing. Just remember, for iterates but the cursor is the important part.
Your output will look like this:
This Person's name is Cookie Monster
This Person's name is Guy Smiley
This Person's name is Big Bird
This Person's name is Oscar Grouch
This Person's name is Alastair Cookie
You can also simply print out the row and look at the raw data:

1
2
3
# loop to iterate
for row in cur.fetchall() :
  print row

Your output will look something like this:
('Cookie', 'Monster')
('Guy', 'Smiley')
('Big', 'Bird')
('Oscar', 'Grouch')
('Alastair', 'Cookie')
This allows you to look at the data structure to determine what you want to grab.

Closing it all up

One quick way to bring down a server is leaving your connections open. Since there are persistent connections, when you end your script that doesn’t mean the database session ends with it, generally it does not. So here is how you close it up:

1
2
3
4
5
# close the cursor
cur.close()

# close the connection
db.close ()

Notice how we call the close() method for both objects. closing them. You are actually closing two things: the cursor and the connection. It’s actually a good thing you have to do them separate, as opposed to one function. There may be a need to close a cursor yet leave the connection open. This is why we do it in two steps.

The full script

Here is the full code for this article, in case you are one of those people who skip down to the code, then download it and play around.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/python
# datademo.py 
# a simple script to pull some data from MySQL

import MySQLdb

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="test")

#create a cursor for the select
cur = db.cursor()

#execute an sql query
cur.execute("SELECT firstname,lastname FROM test.name")

##Iterate 
for row in cur.fetchall() :
      #data from rows
        firstname = str(row[0])
        lastname = str(row[1])

      #print 
        print "This Person's name is " + firstname + " " + lastname

# close the cursor
cur.close()

# close the connection
db.close ()

There it is, easy as that! In the next article I’ll be diving in a little deeper and we’ll build some tests to demonstrate the MySQL usage.
Good luck!