You've worked with both functions and methods, but there's a different type of function, the anonymous function. Anonymous functions are just functions without names, and in Python, they are handled with the lambda operator.

Anonymous functions allow you to use the behavior of a function without having to create a function. This is especially good when you only need to use a function once or twice. It might sound like an unusual occurrence, but it happens fairly frequently when a function or method takes a function as a parameter.


Jut about any program of a decent size needs to be able to read and write from files. At very least, it needs to be able to generate logs. Python is also tightly integrated into Linux system administration and scripting. Again, reading and writing are important for managing a system.


Whenever you need some additional functionality in Python, you turn to the import keyword to pull in extras from Python modules. You've used common ones like the math module plenty of times.

Now, you will learn how to create your own Python modules and packages to compartmentalize your code. Modules are sort of like classes in that they make your code modular. While classes make code modular within a program and serve as the blueprints for objects, modules make all of your code modular across all programs and are utilities to be used just as they are.

Through the use of modules, you can create your own toolbox with all sorts of parts and pieces that you commonly use. Modules can include anything from variables and constants to functions and even classes. Because of this versatility, you can set yourself up to have everything that you need at the beginning of any project.


Polymorphism is yet another important aspect of Object Oriented Programming. No Warcraft fans, it doesn't have anything to do with turning people into sheep. That'd be much cooler. Instead, Polymorphism allows methods and functions to use classes with similar functionality in the same way.

Polymorphism cuts down on the amount of code that you need to write by eliminating redundancy in a logical and meaningful way. It relies on you, the programmer, to be clever in your design and harness similarities in objects.


Inheritance is yet another key concept in Object Oriented Programming, and it plays a vital role in building classes. It allows a class to be based off on an existing one.

When you first started writing Python classes, you were told to just put "Object" in the parenthesis of the class definition and not think too much about it. Well, now's the time to start thinking about it.

"Object" is actually the base class that all Python classes inherit from. It defines a basic set of functionality that all Python classes should have. By inheriting from it when you create a new class, you ensure that that class has that basic functionality.

In short, inheritance is a nice way of categorizing classes and making sure that you don't needlessly repeat yourself.


Encapsulation is one of the fundamental aspects of Object Oriented Programming. It allows programmers better control of how data flows in their programs, and it protects that data. Encapsulation also makes objects into more self-sufficient and independently functioning pieces.

The concept of encapsulation builds on what you did in the last two guides with classes and constructors. Constructors usually are usually used in close conjunction with encapsulation and actually aid in making encapsulation work seamlessly.


By now, you should be familiar with the way basic classes work in Python. If classes were just what you've seen, they'd be fairly rigid and not all that useful.

Thankfully, classes are much more than just that. They are designed to be much more adaptable and can take in information to shape the way they look initially. Not every car starts off exactly the same, and neither should classes. After all, how awful would it be if every car was an orange 71' Ford Pinto? That's not a good situation.

Writing A Class

Start off by setting up a class like the one in the last guide. This class will evolve over the course of this guide. It will move from being a rigid, photocopy-like, situation to a template that can generate multiple unique objects within the outline of the class.

Write the first line of the class, defining it as a class and naming it. This guide is going to stick with the car analogy from before. Don't forget to pass your class object so that it extends the base object class.


Classes are the cornerstone of Object Oriented Programming. They are the blueprints used to create objects. And, as the name suggests, all of Object Oriented Programming centers around the use of objects to build programs.

You don't write objects, not really. They are created, or instantiated, in a program using a class as their basis. So, you design objects by writing classes. That means that the most important part of understanding Object Oriented Programming is understanding what classes are and how they work.

Python is a big deal. It is one of the fastest growing and most in-demand programming languages in the world today. Python is so flexible and versatile that it is used for nearly every job that a programming language can do.

Python powers desktop applications, is used for Linux system scripting and automation, acts as a glue language between Linux applications, is a supplemental scripting language in large programs like GIMP, Blender, and Autodesk Maya, performs crucial scientific and mathematical calculations, and runs some of the web's largest sites like Quora, Reddit, and Dropbox.

Python is also very easy to learn and read. Top computer science programs like the one at MIT rely on Python to teach fundamental computer science and programming concepts to their students. Python can be read a lot like plain English and its structures and flow are consistent with many other programming languages. Essentially, it is the perfect balance between readability and user friendliness and programming power.


Code would quickly become an ugly and unruly mess if there wasn't a way to easily repeat and reuse it. You've already seen some of that with loops. They're great for repeating the same task multiple times right away. What if you wanted to reuse a block of code whenever you wanted? Well, that's where functions come in.

Here's another trip back to math class. If you remember, functions took in a number, did something to it, then outputted the new value. They were often represented in tables with the input on one side and the output on the other. Functions in programming are similar. Sometimes they take input. Sometimes they don't. Much of the time they return a value as well, but they don't always have to. In every case, they are used to repeat an operation whenever they are used, and that's the greatest similarity with the math concept.


You've already gotten acquainted with dictionaries, but just like the other data structures Python supports, there are methods and more powerful ways to use them. There aren't as many methods for working with dictionaries as there are for lists, but that's because dictionaries just don't need them. Plus, many of the ones that do exist, work to break down dictionaries into lists and tuples to make them easier to manage. So, those list methods can be used in conjunction with the dictionary ones to create an efficient machine for handling data.

Dictionary Methods

Items, Keys, and Values

These methods work to break down dictionaries into other data structures to make working with them much more manageable. Doing so also gives access to the methods of those data structures. Through these combinations of methods and loops, you can access and manipulate data with ease.


Somebody hit lists with gamma rays. Okay, so Dictionaries aren't the Incredible Hulk, but they are supercharged in what they can do. In other languages, dictionaries are referred to as hashes, associative arrays, and associative lists. It's probably best to think of them as associative lists because that's exactly what they are. Dictionaries are lists that associate two values with one another. To think of it in terms of an actual dictionary, they associate a word, or key with a definition, or value. They function sort of like a list with custom indexes.


There is yet another type of loop. That loop is designed for iterating sets of data. That's right, lists. Unlike while loops, these for loops have a defined length based on the data set that they are iterating over.

Generally, for loops are used to access and modify each element in a list. To do this, they temporarily represent each element as a new variable used only within the loop.

for loops have a slightly different structure than while loops do. They begin with the word for, which is followed by the temporary variable being created for the loop. Then there is the keyword in specifying the set of data being used, followed by the data set itself and, ultimately, a colon.

For With Range

There is a method called range() that either takes a single number and behaves like a list of numbers going from zero until the number before the one specified or takes two numbers separated by a comma and acts like a slice starting at the first number and listing all numbers until the number before the last one.


Many times in programming, you will need to repeat the same task many times. In fact, looping through and repeating an operation is one of the cornerstones of programming. After all, one of the things that computers are way better than humans at is performing repetitive tasks without getting tired or making mistakes.

One of the simplest ways to make a program repeat the same task is to use a while loop. A while loop repeats the same block of code while a condition is true. When that condition becomes false, the loop will break, and the regular flow of code will resume.

The structure of a while loop is similar to what you encountered in the last guide with if. A while loop begins with the word while followed by parenthesis containing the condition of the loop and a colon. The following lines are indented and will execute in the loop.

Infinite While

Check out this while loop. Try it yourself in your interpreter to see exactly what it does. You might be somewhat surprised.
# Import time for sleep
import time

# While loop
What happened? Rather, what is happening? If you haven't figured out how to stop it yet, just press Ctrl+C. A while loop will run indefinitely as long as the condition that it is given remains True. The loop above was given True as its condition, which will never not be true.


How can a program make a decision? Can a program choose between two or more options. Actually, it can. This isn't some kind of advanced AI concept, it's just a matter of evaluating whether or not certain conditions have been met and choosing a response.

The way that a program can evaluate a condition comes down to true and false. If something is true, do this. If it isn't true do, that. The if statement is the structure for a program to pose these questions and evaluate whether or not they are true. if statements can check multiple conditions and provide multiple responses. They can be used to divert code down one path or another and control the overall flow of a program. They can also be used as a gating mechanism to determine whether certain blocks of code run. Have you ever gotten a message telling you that you needed to log in to continue? That was the result of if.

Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in: