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... '''
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 is a powerful function for creating sequences of integers. It can take one, two, or three parameters.
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 https://docs.python.org/3/whatsnew/3.0.html for more.
[0, 1, 2, 3, 4]
range(m,n) Where m is the starting number of the sequence and n the upper limit (up to but not included):
[1, 2, 3, 4]
It is thus important to notice that:
range (1, n+1) == [1, 2, 3, 4,..., n]
range (n) == [0, 1, 2,..., n-1]
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. http://pythoncentral.io/pythons-range-function-explained/
The general Python syntax for a For loop is
for iterating_var in sequence: indentedStatementBlock
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
printfcommand. 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 print(math.factorial(10))
The general Python syntax for a simple if statement is
if condition: indentedStatementBlock
var = 100 if var == 100: print("condition 1: ") print(var) print("That's it!")
condition 1: 100 That's it!
The general Python if-else syntax is
if condition: indentedStatementBlockForTrueCondition else: indentedStatementBlockForFalseCondition
var = 100 if var == 200: print("condition 1: ") print(var) else: print("Condition was false: ") print(var) print("That's it!")
Condition was false: 100 That's it!
The most elaborate syntax for an if-elif-else statement is indicated in general below:
if condition1 : indentedStatementBlockForTrueCondition1 elif condition2 : indentedStatementBlockForFirstTrueCondition2 elif condition3 : indentedStatementBlockForFirstTrueCondition3 elif condition4 : indentedStatementBlockForFirstTrueCondition4 else: indentedStatementBlockForEachConditionFalse
var = 100 if var == 200: print("condition 1: ") print(var) elif var == 150: print("condition 2: ") print(var) elif var == 100: print("condition 3: ") print(var) else: print("Each condition was false: ") print(var)
condition 3: 100