Before you begin

You gotta know a few things about whats going on on this page. First, this guide is a quickstart so don’t expect a detailed explanation of anything less than criticaly important. Second, I write Python for web applications. So what I consider “the basics” may not be what you’re looking for (though if you’re a total beginner, you may get some use out of it). Third, notice the date. ‘murka. Aight, lets hop on our eagles and write some patriotic Python.

Getting Started

You should have some sort of Python environment installed. You can use the terminal and a text editor like Vim to write and execute Python code, or you could use an IDE like Idle, or PyCharm. I will focus primarily on running Python in a Linux based Terminal environment. If you’re using Idle or some other IDE, you can skip to the next section.

Also I use Vim. Sorry Emacs users.

Download & Install - Linux

Lets start by downloading, and installing the version of Python we are going to be using.

wget https://www.python.org/ftp/python/3.3.6/Python-3.3.6.tar.xz
tar xf Python-3.*
cd Python-3.*
./configure
make

If you’re running a version of Red Hat, or use yum, use

make altinstall

otherwise

make install

Once that is all finished, you should have python ready to go.

Download & Install - Windows & Mac

If you arent running a linux distribution, or just don’t want to hastle with the command line then you can install Idle for free from Python’s website.

www.python.org/downloads

Download and install Python 3.4.3, and open Idle once it’s finished.

Open a new Python file

For terminal users, cd to your working directory & open a new Python using

vim new_file.py

or if you’re using Idle,

<ctrl> + n

Run that code

If you’re using the terminal, close your text editor and

python new_file.py

or in Idle, simply press F5

Some Handy Stuff to Know

Comments

# either with a "#"
"""
    or as a block
    comment spanning
    multiple lines.
    You can use both
    single and double
    quotes for block
    comments
"""

Input / Output

You can output to the terminal with print()

print("Hello, world!")

Variable data types are implicit, so no type declaration is needed

my_variable = "Set it to a string"
another = 5
yet_another = False

If you want to get user input, use the input() function and assign it to a variable.

user_input = input("Say something!")
print(str(user_input))

Make sure to always sanitize your input and output to avoid errors.

output = my_variable + str(another)
print(output) # => "Set it to a string5"

str() is a Python builtin that will output a string representation of the argument. It can be overridden in a class method.

String Operations

There are also several handy string operations like

len(output)                 # => 19 - the length of the string
type(string)                # => string
words = output.split(" ")   # => "Set", "it", "to", "a", "string5"
output[0]                   # => "S"
words[0]                    # => "Set"

List Operations

words.append("called")      # => "Set", "it", "to", "a", "string5", "called"
words.pop()                 # => "Set", "it", "to", "a", "string5"

Control Flow

Python comes equipped with all the bells and whistles you need to make your program run. Thanks to Python’s focus on readability, control flow statements read exactly how they work.

if / else statements

a = 3
b = 5
if a < b:
    print(str(a) + " is less than " + str(b))
elif a > b:
    print(str(b) + " is less than " + str(a))
else:
    print("They are equal")

for loops

list = [1, 2, 3, 4, 5]
for entry in list:      # note that the iterator variable
    print(str(entry))   # declaration statement is implicit

and while loops

count = 0
full = 20
while count <= full:
    print(str(count))
    count++

Functions

function declaration in Python is done in the following way

def new_function(some_variable):
    """ This function is gonna do some stuff """
    result = some_variable.another_function()
    return result

since Python is interpreted, you need to declare functions before using them.

Function calls are done as expected.

some_variable = "stuff"
result = new_function(some_variable)

Classes

class declarations are done like

class AwesomeNewClass(SomeParentClass):
    """
        The purpose of this class is to extend the
        functionality of SomeParentClass by being more
        awesome.
    """
    def __init__():
        """ this is the constructor """

    def some_awesome_function():
        """ does all the awesome stuff """

NOTE: You should fill in the functions with at least 1 line of code otherwise it will result in a syntax error

That should be enough to get you started. If you feel like I’ve missed something, please email me and I’ll make changes as necessary. Again, this is a rough guide, and is not meant for people who have never programmed before. There are better, more comprehensive guides out there that would be a far better intro to programming for you. Thanks for reading! More Python coming up soon!