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 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

Python Tuples

Introduction

Tuples are immutable data sets made up of data of different types. While tuples are very similar to lists, they are different in those key ways.

Tuples cannot be changed once they are created. The data they hold can be passed to a different tuple, but the original tuple cannot be changed. This means that tuples don’t have methods for
manipulating them like lists do because they cannot be manipulated.

Like lists, tuples can also contain data of different types. Tuples can contain strings, integers, floats, booleans, and
even lists. Because tuples are immutable, they aren’t meant to be manipulated, so data types don’t matter nearly as much.

Read more

Python While Loops

Introduction

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
while(True):
	print("looping...")
	time.sleep(2)

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.

Read more

Python For Loops

Introduction

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.

Read more