Python Exception Handling

Introduction

Python will let you know when you get your syntax wrong. It’ll immediately fail and not allow your program to run.

What about when your code has a different type of problem? Those are called exceptions, and they tend to be harder to catch. It’s up to you to recognize situations where hey might come up and catch them to prevent your program from crashing altogether.

Imagine a scenario where you need user input. Do you want your program to crash every time a user mistypes something or enters something erroneous? That’s far from ideal. Since you know there could be a problem there, you can tell Python to look out for one, and recover gracefully.

Read more

Python Packages and Modules

Introduction

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.

Read more

Python Inheritance

Introduction

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.

Read more

Python Variables

Introduction

Do you remember variables from math class in school? Variables in programming are actually very similar. Variables are just symbols that that represent a value and that value can be changed; thus the name variable. Unlike in math, variables in programming can be much more free form. Variables don’t just have to be a letter. Variables can be a single character, but they are more commonly a word or a short descriptive phrase in lower case with words separated by underscores. It’s actually best to name variables something descriptive so the you and anyone else that you’re working with knows exactly what that variable is, even much later on in the code.

Types of Variables

Python is a dynamic duck typed language. Don’t worry too much about the terminology, but that means that Python doesn’t force you to specify which types variables are when you create them. Oh yeah, there are types of variables. Even though you don’t necessarily have to specify their type when you create them, it’s a good idea to know what type you want them to be. Later on, having the wrong type of variable will invariably get you into big trouble.

Read more

Working With Number Variables In Python

Introduction

Obviously working with numbers in programming is important. Python as excellent mathematical capabilities, and there are tons of additional libraries available to extend Python’s built in functionality for even the most advanced calculations. Of course, the basics are important too, and numbers and some basic calculations come into play when controlling the flow of programs and making selections. That’s why knowing your way around working with numbers in Python is especially important.

Read more

Python Classes

Introduction

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.

Read more

Python String Basics

Introduction

Strings are called what they are because they are strings of characters. It doesn’t matter if those characters are letters, numbers, symbols or spaces. They are all taken literally and not processed within a string. That’s why strings are sometimes referred to as string literals.

String Basics

If you’ve been following along with the previous guides, you’ve already experimented with some strings. You’ve typed in some, and you’ve printed them back out. What about getting user input when the Python script runs? Python has built in functionality to take in user input and assign it to a variable. Try it out.

user_input = input("Please enter some text: ")
print(user_input)

Read more

Python Constructors

Introduction

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.

Read more

Advanced Python Strings

Introduction

In the previous guide, you learned the basics of handling strings in Python. In this guide, you will explore some of the more complex things that strings are capable of. There are tools built into Python, called string methods, that help you to handle strings and do some very powerful things. Through the use of string methods, you can masterfully manipulate text and use it to its fullest potential without writing a ton of code.

Navigating a String

Strings aren’t words. They aren’t sentences, phrases, and believe it or not, they aren’t even a collection of text. Strings are just a lists of characters. Those characters can be letter, numbers, symbols, spaces, and escape characters. Python sees strings by their parts(the characters) and uses those parts to manipulate strings. This is actually true of almost any programming language. So, that means that you can select individual characters out of a string. Try this:

phrase_string = "This phrase is a string!"
print(phrase_string[0])

Read more

Python Comments

Introduction

This guide isn’t about programming. In fact, there is no new code involved at all. However, it does have everything to do with making sure that the code that you write is understandable to both yourself and anyone else that might look at it down the line.

If you’ve looked at any open source projects, you’ve probably noticed notes placed in by the programmers. Those notes are just plain text. The programming language doesn’t compile or interpret them in any way. It just ignores them. It knows that those comments are for humans, not computers.

Read more

Python Lists

Introduction

Lists are a big deal. It really can’t be overemphasized what a big deal they are. Lists are not only used for iterating through data, but they are also a popular data storage and
categorization method used for handling data as a program is running. For anyone who’s programmed in another language, lists are often known as arrays.

Lists may be either very simple or very complex, but they all follow the same rules. Lists may contain different types of data, but be careful when working with it. If you create a list that mixes, say, strings and floats, be sure not to try to call a string method on a list item containing a float.

Remember when the string guides said that strings were just a list of characters. Well, now’s the time to be glad that you paid attention to strings. You can do many of the same things that you did
with strings with lists, and you can do them in the same way. So, some of this is going to feel like a repeat of the string guide, but don’t break focus. There are differences, and there are list
specific methods, so assuming that strings and lists are the exact same this will get you into trouble.

Creating a List

Creating a list is a bit different than the other variables that you’ve learned about so far. You can create a list with absolutely no values in it at all. This is useful for situations where you
don’t exactly know what will be added to the list because the data isn’t in the program yet. There will also be plenty of situations where you won’t be sure how many entries will be in a list, so
again, creating an empty one and adding data later is the right move.

Read more

Python Multidimensional Lists

Introduction

Now it’s time to take lists to a new dimension. No, you don’t need to learn how to code in “The Upside Down,” but there are additional degrees of complexity to lists. Lists are used to hold data,
but they are also used to categorize it. Values inside a list can be further broken down into other sets. That’s essentially what a multidimensional list is.

Two Dimensional Lists

What is a list that holds lists? That’s all a two dimensional list is. The list below consists of three lists. Each of the three lists has five elements. Don’t worry about numbers quite yet. Just
focus on the top level elements, the lists. You can access them the way you would any element in a normal list.

number_sets = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
print(number_sets[1])

Read more