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

Python Introduction and Installation Guide

Introduction

Python is a dynamically typed, interpreted, general purpose programming language. It’s useful for everything from system scripting, to web applications, to full graphical desktop programs. Because of that, it’s no surprise that demand for Python programming skills is only increasing, and top companies like Google, Mozilla, Instagram(Facebook), and Reddit rely on it as part of their core technology stack. Not only that, but Python is a favorite in both academic and scientific circles and is gaining ground in the financial sector. Top universities are even using it to teach programming in their computer science programs.

With all of that said, you’d probably be thinking that Python is something super difficult to learn and only accessible to the elite in the technology field, but you couldn’t be more wrong. Python is easy. Python is really easy. In fact, Python is one of the first languages used to teach children to program. Python was designed to be very clear and simple to understand. It reads like plain English, and its syntax makes use of spaces rather than brackets and semicolons, so it always looks clean and uncluttered. It’s very difficult, if not impossible, to wright messy Python. This helps out new programmers and programmers new to Python big time because you can always tell what you’re looking at, or at least, get a decent sense of what it does. This way, you can look at code examples from established open source projects to get an idea of what professional grade Python looks like and how it’s used.

Python and Linux work incredibly well together. It wasn’t all that long ago that Python supplanted Perl as the de facto scripting and “glue” language on Linux systems. This means that many scripts and utilities that ship with modern Linux systems are written in Python. As a result, most Linux distributions have Python installed by default, but there is a bit of a catch. There are two current versions of Python. Python 2.7.X and Python 3.X.X are both current. Syntactically, they are very similar, but Python 3 has some features that Python 2 doesn’t. That means that they are not entirely compatible and many distributions package them separately. So, your system may have Python 2, but not Python 3 or vice versa. This guide and the others in the series are going to cover Python 3. It is the future of Python, and it’s not so bad to go back to Python 2 after you’ve worked with Python 3.

Read more

Python Dictionaries

Introduction

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.

Read more

Python Files and the Interpreter

Introduction

Python is an interpreted language, meaning that it is compiled every time that it is run. There are a number of pros and cons when talking about an interpreted language like this.

First, on a positive note, they tend to be easier to debug. They fail immediately when they are run, and tell you what went wrong, which is nice compared to compiled languages like C/C++, which can compile just fine, but fail silently when run.

Interpreted languages are also very portable. All you have to do is install the interpreter on a system, and most code written in that language can run fine, regardless of the operating system. There are some exceptions when dealing with operating system specific code and libraries, but if you’ve planned for portability, you can work around those situations.

Read more

Python Advanced Dictionaries

Introduction

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.

Read more

Experimenting With Numbers and Text In Python

Introduction

You probably want to jump in and start coding right away. That’s a great attitude to have, but it’s much better to experiment with the language and your programming environment first. If you’ve never programmed or never worked with an interpreted language like Python before, it’s important to get a feel for the way Python works and start to develop a workflow. One great aspect of Python being interpreted is the ability to write a couple of quick lines of code and test them out in real time. There really isn’t much setup beyond what you’ve already done.

Playing With Numbers

Without knowing anything about the language, you can use Python like a basic calculator. Open up either your .py file or the interpreter. Type in a basic math problem and run it.

>>> 10+25
35

Read more