Programming Python, Part I
A blog is not a blog if we can't add new posts, so let's do that:
>>> blog = blog + ["A new post."] >>> blog ['My first post', 'Python is cool', 'A new post.']
Here we set blog to a new value, which is the old blog, and a new post. Remembering all that merely to add a new post is not pleasant though, so we can encapsulate it in what is called a function:
>>> def add_post(blog, new_post): ... return blog + [new_post] ... >>>
def is the keyword used to define a new function or method (more on functions in structured or functional programming and methods in object-oriented programming later in this article). What follows is the name of the function. Inside the parentheses, we have the formal parameters. Those are like variables that will be defined by the caller of the function. After the colon, the prompt has changed from >>> to ... to show that we are inside a definition. The function is composed of all those lines with a level of indentation below the level of the def line.
So, where other programming languages use curly braces or begin/end keywords, Python uses indentation. The idea is that if you are a good programmer, you'd indent it anyway, so we'll use that indentation and make you a good programmer at the same time. Indeed, it's a controversial issue; I didn't like it at first, but I learned to live with it.
While working with the REPL, you safely can press Tab to make an indentation level, and although a Tab character can do it, using four spaces is the strongly recommended way. Many text editors know to put four spaces when you press Tab when editing a Python file. Whatever you do, never, I repeat, never, mix Tabs with spaces. In other programming languages, it may make the community dislike you, but in Python, it'll make your program fail with weird error messages.
Being practical, to reproduce what I did, simply type the class header, def add_post(blog, new_post):, press Enter, press Tab, type return blog + [new_post], press Enter, press Enter again, and that's it. Let's see the function in action:
>>> blog = add_post(blog, "Fourth post") >>> blog ['My first post', 'Python is cool', 'A new post.', 'Fourth post'] >>>
add_post takes two parameters. The first is the blog itself, and it gets assigned to blog. This is tricky. The blog inside the function is not the same as the blog outside the function. They are in different scopes. That's why the following:
>>> def add_post(blog, new_post): ... blog = blog + [new_post]
doesn't work. blog is modified only inside the function. By now, you might know that new_post contains the post passed to the function.
Our blog is growing, and it is time to see that the posts are simply strings, but we want to have a title and a body. One way to do this is to use tuples, like this:
>>> blog =  >>> blog = add_post(blog, ("New blog", "First post")) >>> blog = add_post(blog, ("Cool", "Python is cool")) >>> blog [('New blog', 'First post'), ('Cool', 'Python and is cool')] >>>
In the first line, I reset the blog to be an empty list. Then, I added two posts. See the double parentheses? The outside parentheses are part of the function call, and the inside parentheses are the creation of a tuple.
A tuple is created by parentheses, and its members are separated by commas. They are similar to lists, but semantically, they are different. For example, you can't update the members of a tuple. Tuples are used to build some kind of structure with a fixed set of elements. Let's see a tuple outside of our blog:
>>> (1,2,3) (1, 2, 3)
Accessing each part of the posts is similar to accessing each part of the blog:
>>> blog 'New blog' >>> blog 'This is my first post'
This might be a good solution if we want to store only a title and a body. But, how long until we want to add the date and time, excerpts, tags or messages? You may begin thinking you'll need to hang a sheet of paper on the wall, as shown in Figure 1, to remember the index of each field—not pleasant at all. To solve this problem, and some others, Python gives us object-oriented programming.
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
|Non-Linux FOSS: Seashore||May 10, 2013|
|Trying to Tame the Tablet||May 08, 2013|
- Introduction to Named Pipes
- Red Hat's Dell Deal, Waiting for Cowpland
- SSHFS: Super Easy File Access over SSH
- How to Be Cute on All Desktops with Qt
- How's The Weather?
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Writing a Linux Driver
- Still Searching
- Linux System Administration: A User's Guide
- The Neuros MP3 Digital Audio Computer
- This is the easiest tutorial
57 min 42 sec ago
- Ahh, the Koolaid.
6 hours 36 min ago
- git-annex assistant
12 hours 35 min ago
- direct cable connection
12 hours 58 min ago
- Agreed on AirDroid. With my
13 hours 8 min ago
- I just learned this
13 hours 12 min ago
13 hours 42 min ago
- not living upto the mobile revolution
16 hours 34 min ago
- Deceptive Advertising and
17 hours 9 min ago
- Let\'s declare that you have
17 hours 10 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi
It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.