Basics in Python Programming

Python Comments

Single-line comments are created simply by beginning a line with the hash (#) character, and they are automatically terminated by the end of line. #This would be a single line comment in Python

Comments that span multiple lines – used to explain things in more detail – are created by adding a delimiter (''') on each end of the comment.

''' This would be a multiline comment in Python that spans several lines and describes your code... '''

Block characteristics

Python is space sensitive. Python functions, ForLoop block, While bloch, If statements, etc. do not have explicit begin or end, nor braces that could mark where the block code begins and ends. The only delimiter is the indentation of the code. To specify the beginning of a block, indentation is increased. On the opposite, to specify the end of a block, indentation is reduced.

For example, to declare a function we will do:

def my_function(var1, var2):
    Four spaces indentation to start this block
    \# Statements within the same block of code need to be indented at the same level.
End of the function content : indentation is reduced 

The Range function

The range function is a powerful function for creating sequences of integers. It can take one, two, or three parameters.

Let's compute range(n) Where n indicates the upper limit (up to but not included) of generated integers (whole numbers), starting from zero:

Since Python 3.x The print statement has been replaced with a print() function. Refer to for more.

[0, 1, 2, 3, 4]

Let's compute range(m,n) Where m is the starting number of the sequence and n the upper limit (up to but not included):

print(range(1, 5))
[1, 2, 3, 4]

Let's compute range(m,n, s):

It is thus important to notice that:

print(range(2, 10, 2))
[2, 4, 6, 8]

The sequence can be decreasing with step=-1:

print(range(10, 0, -1))  
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

TODO: In python 2, range produced a list object and xrange returned an iterator... xrange was renamed to range in python 3.x. The original range function from python2 is deprecated in Python3.

For loop

The general Python syntax for a For loop is

for iterating_var in sequence: 

For loop examples

for letter in 'Python':     
   print('Current Letter : '+letter)
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        
   print ('Current fruit: '+ fruit)
Current fruit: banana
Current fruit: apple
Current fruit: mango

Using range() function, let's compute factorial of 10 :

n = 10
total = 1
for i in range(1, n+1):
    total *= i
print('10! = %i' %(total))
10! = 3628800

The python print function behaves as the bash printf command. We can refer to AWK Command to read more about printf for more details.

To compute the factorial of 10 can be done in a single step with the factorial method from the math package:

import math

If statement

The general Python syntax for a simple if statement is

if condition:
var = 100
if var == 100:
   print("condition 1: ")
print("That's it!")
condition 1: 
That's it!

The general Python if-else syntax is

if condition:
var = 100
if var == 200:
   print("condition 1: ")
   print("Condition was false: ")
print("That's it!")
Condition was false: 
That's it!

The most elaborate syntax for an if-elif-else statement is indicated in general below:

if condition1 :
elif condition2 :
elif condition3 :
elif condition4 :
var = 100
if var == 200:
   print("condition 1: ")
elif var == 150:
   print("condition 2: ")
elif var == 100:
   print("condition 3: ")
   print("Each condition was false: ")
condition 3: