It doesn't cost anything to download or use Python, or to include it in your application. Python can also be freely modified and re-distributed because while the language is copyrighted it's available under an open-source license [1].
In this section, you get an overview of essential Python programming concepts. Next, we will go into more detail about Python's data structures, functions, and other built-in tools. We will now discover a few key characteristics of Python.
hint: to maximise your learning, please open your jupyter notebook and type the code you see entered as your progress through the material. This will help train your programming muscle memory.
Python uses whitespace (tabs or spaces) to structure code instead of using braces as in many other languages.
Consider a program to print numbers from 1 to 10:
for x in range(1:10):
print(x)
Similarily, consider a program to only print even numbers from 1 to 10:
for x in range(0, 10):
if(x%2 == 0):
print(x)
A colon denotes the start of an indented code block after which all of the code must be indented by the same amount until the end of the block. Typically four spaces is the standard adopted by the vast majority of Python programmers, unless you have compelling reasons to do so otherwise, please follow the 4space rule.
observe what happens if you remove the spaces before print(x)
Any text preceded by the hash mark # is ignored by the Python inter preter. This is often used to add comments to code.
# range(0, 10) contains a object containing numbers from 0 to 10
for x in range(0, 10):
# use the % operator to find the modulo
if(x % 2 == 0):
print(x)
Please watch this video to learn more about good commenting practices.
An important characteristic of the Python language is that every number, string, data structure, function, class, module, and so on exists in the Python interpreter in its own container which is referred to as a Python object.
When assigning a variable (or name) in Python, you are creating a reference to the object on the righthand side of the equals sign. Consider a list of integers:
a = [1, 2, 3]
This creates a new Python object a and assigns the list of integers ‘[1, 2, 3]' to this object. In Pythonic terms, we are assigning the name a to the object ‘[1, 2, 3]'.
Note that we are not defining a specific data type for [1, 2 ,3]. Python automatically recognizes it as a list of integers.
a = "Hello World"
This creates a new Python object a and assigns the string "Hello World" to this object. In Pythonic terms, we are assigning the name a to the object "Hello World"'.
Please run the following set of statements and observe the output.
a = "Hello"
a = a + "World"
print (a == "Hello World")
b = 5
b = 5 + 4
print (b == 9)
The "+" operator essentially joins (or concatentates) objects of similar type and the "==" operator checks if two objects are equal. Note that in either case, it requires the two objects to be of the same type. Note that you can also extend it to list of objects.
b = [1, 2, 3]
b = b + [4]
print (b == [1,2,3,4])
What would happen if I run this piece of code?
b = [1, 2, 3]
b = b + ["four"]
print (b)
Loops are programs that iterate over a set of values to perform a certain task. In Python, there are two kinds of loops.
Here the program iterates over a set range of values. At the end of one loop iteration, the value of the iterator (x in the example) is automatically set to the next value.
for x in range(0, 10):
if(x % 2 == 0):
print(x)
Here we start by setting the iterator to a given value. At the start of each loop iteration, the value of the iterator is checked and as long as it meets the given condition, the loop is executed.
x = 0
while(x < 10):
if(x % 2 == 0):
print(x)
x = x + 1
A conditional statement is exactly as it seems. The program executes certain statements if a condition is met, else it skips these statements and proceeds.
# A program to check whether the range of numbers 0 to 10 are even or odd
for x in range(0, 10):
if(x % 2 == 0):
print('Even')
else:
print('Odd')
To understand more about loops, please watch the following short video.
A function is a programming technique used to combine a set of statements which perform specific operations on variables. Functions are an alternative method of cutting-and-pasting codes rather than duplicating lines of the same instruction or operation.
Imagine you are asked to add two numbers 4
and 5
. In python, you can perform this operation by simply typing..
print(4 + 5)
Now imagine I ask you to print the sum of two numbers, but I change the numbers every hour. Duplicating the print(x + y)
statement every hour can become quiet tiring, not to mention extemely boring. This is where functions come in handy.
def SumTwoNumbers(x, y):
return(x + y)
Please watch this brief video to review Python functions.
Function in Python accept variables known as function parameters. In our above example, SumTwoNumbers(x, y)
, x
and y
are example of function parameters. An unique feature of Python is that such function parameters can also have default values. To know how this works in practise, please watch the following video.
Learn Python 3
module in Code Academy which you can find here. You are expected to have completed this module by the end of week 8.We will recap today's concepts and discuss the answers at the Q&A, see you at 4pm!