And now for something completely different…

Syntax

Like any language, computer or human, Python has some ground rules for being spoken. Welcome to Python Club. The first rule of Python Club is:

You must correctly indent in Python Club.

Here is the second rule of Python Club:

You MUST correctly indent in Python Club.

In other words, spaces and indentation are very important in Python.

Whenever you see a “:” you should start a new line and then indent four spaces from the previous line because these are a nested statement.

What the colon?

More like what the code blockin’ Essentially, the colon is used to denote a code block, which will be discussed soon.

These rules make Python a good programming language for beginners because it teaches disciplined formatting.

Print

Check outputs by using the in-built “print” function, for example print "hello python club" will print out your welcome to the club.

Congratulations welcome to Python Club

 

Assigning Variables

To speak effectively in Python you have to use variables, which are defined with an = sign. Unlike some computer languages, variables are typed (treated as a number or text) depending on how they are defined. This makes Python easily readable.

For example:

Creates a variable called “awesome” that is a string of characters called “some text”, while:

Creates a variable with a value of 10.

So awesome + awesome = 20 neat, right?

You can also define lists, arrays, and tuples. These are simple collection of items stored in brackets, like so:

Importance of Importing

Use “import” to bring in modules, also known as external tools and functions.

For example import datetime would bring in date and time functions, like datetime.datetime.now() will tell you the exact time.

Import parts of a module

To use certain parts of another module and you can also do

from datetime import datetime which would take the “datetime” function, but not the whole “datetime” module.

If done correctly, the following code should work:

datetime.now()

There we are using “datetime” as if we created the function ourselves, but in fact we borrowed it from the (badly named) datetime module.

Conditionals, loops, and exceptions! Oh my!

What does a “If-Statement”, a “for-loop”, and “try, except” have in common? They all exist inside a code block, because they are nested.

An if-statement uses an operator to check/compare various variables and statements:

A for-loop is designed to loop through an array items and do something with them, which can also be combined with a conditional:

Finally, a try/except is a code block that will try to run, but return an error when it does not. For example:

Organization

Much like an essay, Python code flows from top to bottom.

For example: If you ran the datetime.now() before importing it will not run at all.

While coders may have different styles of arranging, what is most important is readability.  As such, the Python developers have created official coding guidelines for Python at the PEP Style Guide for Python:

https://www.python.org/dev/peps/pep-0008/#id15

Now that we have some fundamentals, lets move on to some real examples of Python uses.