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.
[TBS_ALERT color=”info” heading=”What the colon?”]More like what the code blockin’ Essentially, the colon is used to denote a code block, which will be discussed soon.[/TBS_ALERT]
These rules make Python a good programming language for beginners because it teaches disciplined formatting.
Check outputs by using the in-built “print” function, for example print “hello python club” will print out your welcome to the club.
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.
awesome = "some text"
Creates a variable called “awesome” that is a string of characters called “some text”, while:
awesome = 10
Creates a variable with a value of 10.
So awesome + awesome = 20 neat, right?
Variables can also be defined as a list. A list is a collection of items stored in brackets, like so:
UCLA = ['University','of','California','Los Angeles']
You can access objects in a list in a couple ways, one is directly calling it’s place in the index. In Python, any index starts at 0, so UCLA would return “University”.
One common use is to slice objects using a colon, the location of the colon indicates which direction to grab the data from. If I wanted everything after California I would use UCLA[:2] .
Defining a function
Recall the formula for a line (y = mx + b), instead of assigning a variable once, you can define a function to calculate results. The syntax is as follows:
def my_addition_function(parameter1,parameter2): parameter1+parameter2 my_addition_function(10,10)
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:
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 = 5 b = 10 if a > b: print a + " is bigger than " + b
A for-loop is designed to loop through an array items and do something with them, which can also be combined with a conditional:
sample_array = [1,2,3,4,5,6] for number in sample_array: if number > 5: print str(number) + " is greater than 5
You can also set a range to loop through, rather than a per-defined list of numbers:
for number in range(0,10): print number
Finally, a try/except is a code block that will try to run, but return an error when it does not. For example:
try: normal_operation = (10/10) broke_operation = (10 / 0) except: print "there was a mistake somewhere"
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:
Now that we have some fundamentals, we can now move on to some real examples of Python uses.