Learn Python in 60 minutes.

Before you even begin reading I want you to understand that this is not a start a Python programming career in an hour guide.  This is meant to get you rolling with Python so that you can start exploring, learning, and building with Python.  For a more time intensive, and in-depth education with Python I suggest www.diveintopython3.net and www.diveintopython.net.

I will not go over installing Python, but it is covered pretty well here.  I will be using Python 3.6.0, but this all applies to any current release of Python.  When I come across a keyword I feel is important, I will mark it in case you ever need to come back to this page.  In between each part, I suggest that you experiment with the information a little bit.  Try to break it, or improve it, or otherwise modify it to your liking so that you know you understand what is going on.  I know the title is learn Python in 60 minutes but there is no reason to rush through.

As usual, if you have any questions please feel free to jump on IRC or the forum.  The IRC web chat is at https://webchat.hackr.pl and the forum https://forum.hackr.pl.  You can also ask on Facebook or Twitter as well.

Without further ado, let’s get started learning Python.

Python’s Syntax

Python organizes code using indentation to identify blocks and newlines as line terminators.  You will never forget that semicolon in Python because you don’t need one.  Also, it’s important to know that anything in Python that is going to open a block will end with a “:”.  The indentation can be either tabs or spaces, but using both will throw an error in some versions of Python.  There is no set number of spaces to use, as long as it is consistent.  

Variables

Variables in Python are strongly typed, implicitly typed, and case sensitive.  Or in other words; types are important (int vs char vs float), you don’t have to declare variables (you just use them and they are set), and the variables ABC and abc are completely different.

 

Python’s Data Types

Python has 3 common data types.  They are; listsdictionaries, and tuples.

Now that we have packed up a few variables with some different data types, we should learn how we can retrieve that information later in our code.  Python is awesome about this, you can pretty much grab data however you want.

If you are anything like me, when you look at these examples you see that there is a clear reason for dictionaries, but what is the difference between lists and tuples?  Lists are dynamically sized and tuples have fixed sizes.  You will see in the next block of demo code that you can add and remove from lists, but once a tuple is set it is set for good.

On our lists we can use the .append() and .extend() methods add to our lists.  This won’t work on our tuples.  These methods are pretty similar but the difference isn’t negligible.  

Flow Control

This is where we get into the heart of any programming language.  Flow control separates us from a linear flow of instructions to a program that can make decisions.  The available flow control statements are if, for, and while.  

I should also mention the comparison operators that Python uses.  They are the same common ones you would expect to find but here they are:

Equal ==
Not Equal !=
Less Than <
Greater Than >
Greater than or equal to >=
Less than or equal to <=

Functions

Like any other programming language, we need to be able to create our own functions so that we can reuse code, and reduce the scope of variables.  Functions are easily defined using the def statement.

Creating Classes

Everything in Python is a class.  The lists, tuples, and dictionaries we have been working with: classes.  The strings that we use: classes.  Even the script itself is a class.  What we haven’t talked about though is how we can create our own classes with custom methods and attributes.

External Libraries

Python is a powerful language, but the real power of Python is in the vast array of libraries that can be imported into your scripts to add additional functionality.

 In Conclusion

If you are new to programming in general, this can seem like a lot.  Just understand that when you start branching out to other languages you will start to see many similarities, and the logic and problem solving behind programming doesn’t change, and that’s the real fun part.

If you are an experienced programmer, well, now you know Python.  Go start reading documentation and importing libraries to do awesome stuff.  Python is a really fun language, and you can whack out cool little scripts really freaking fast.

Good luck to everyone, thank you for reading!

Daniel is a freelance web developer and IT consultant with a passion for security and privacy. Although he isn't much of a writer, he enjoys writing blog posts that help out others in the community.

Leave a Reply

Your email address will not be published. Required fields are marked *