# Calculate the remainder of a divisional operation
x = 56
y = 3
z = x % y         # Modulo operation

if z > 0:
    print ("The remainder of %d divided by %d is %d" % (x,y,z))
else:
    print "There's no remainder"

"""
Explanation: <span style="color: #B40486">BASIC PYTHON FOR RESEARCHERS</span>
by Megat Harun Al Rashid bin Megat Ahmad
last updated: April 14, 2016

<span style="color: #29088A">3. Conditional Expressions</span>
$Python$ conditional expressions include the <span style="color: #0000FF">$if/elif/else$</span> statement. In addition the <span style="color: #0000FF">$for$</span> and <span style="color: #0000FF">$while$</span> statements can be used in conditional looping. $Python$ also has the <span style="color: #0000FF">$enumerate$&#40; &#41;</span> function for conditional looping.

3.1 The <span style="color: #0000FF">if/else</span> condition
The <span style="color: #0000FF">$if/else$</span> conditional expression allows statement to be executed if a condition is fulfilled:
End of explanation
"""


# Compare the values of two integers

int1 = 45
int2 = 55

if int1 > int2:
    print "%d is larger than %d" % (int1,int2)
elif int1 == int2:
    print "%d is equal to %d" % (int1,int2)
else:
    print "%d is less than %d" % (int1,int2)

"""
Explanation: The $z = x$ &#37; $y$ operation means that the remainder of $x$ when divided with $y$ will be assigned to $z$. The <span style="color: #0000FF">$if/else$</span> conditional expression above resulted in the printing of the variable $z$ if its value is positive, i.e. if $x$ when divided with $y$ has a remainder. If this condition is not fulfilled, then the text "There's no remainder" will be printed.
Statements inside conditional expression must be indented with space (not tab). The indentation must be consistent throughout the condition.

3.2 The <span style="color: #0000FF">if/elif/else</span> condition
The <span style="color: #0000FF">$if/elif/else$</span> conditional expression allows multiple conditions to be applied.
End of explanation
"""


x = 34
y = 12
z = 67

if x > y:
    if y > z:
        print 'Maximum integer is %d' % x
        print 'Minimum integer is %d' % z
    elif z > x:
        print 'Maximum integer is %d' % z
        print 'Minimum integer is %d' % y
    else:
        print 'Maximum integer is %d' % x
        print 'Minimum integer is %d' % y
        
else:  # y > x
    if x > z:
        print 'Maximum integer is %d' % y
        print 'Minimum integer is %d' % z
    elif z > y:
        print 'Maximum integer is %d' % z
        print 'Minimum integer is %d' % x
    else:
        print 'Maximum integer is %d' % y
        print 'Minimum integer is %d' % x



"""
Explanation: In the example above, the first condition always use the <span style="color: #0000FF">$if$</span> condition expression (i.e. $int1$ > $int2$). Only if this is not fulfilled will the second condition be evaluated i.e. the <span style="color: #0000FF">$elif$</span> condition expression. If this condition is also not fulfilled, then the <span style="color: #0000FF">$else$</span> condition statement will be executed.
In multiple conditional expressions, all the conditions will be evaluated in sequence. When one of the condition is fulfilled, the sequential evaluation will stop and the statement for that conditions will be executed.
Some of the conditional operators that can be used in a conditional expression:
|Condition|Function|
|---|---|
|>|more than|
|<|less than|
|>=|equal or more than|
|<=|equal or less than|
|==|equal to|
|!=|not equal to|
|and|more than one conditional operations are true|
|or|either one conditional operations is true|
In general, the multiple conditional expressions format is:  
if (condition/s 1):

statement 1.1
statement 1.2
......

elif (condition/s 2):

statement 2.1
......  

elif (condition/s 3):  

statement 3.1
......  

......
......
......  
else:  

statement
......

The statement in each conditional expression can also be a conditional expression.
<span style="color: #F5DA81; background-color: #610B4B">Example 3.1</span>: Determine the maximum and minimum of three different integers: 34,12,67.
End of explanation
"""


x = 34
y = 12
z = 67

if x > y > z:
    print 'Maximum integer is %d' % x
    print 'Minimum integer is %d' % z

elif x > z > y:
    print 'Maximum integer is %d' % x
    print 'Minimum integer is %d' % y
    
elif y > x > z:
    print 'Maximum integer is %d' % y
    print 'Minimum integer is %d' % z

elif y > z > x:
    print 'Maximum integer is %d' % y
    print 'Minimum integer is %d' % x

elif z > x > y:
    print 'Maximum integer is %d' % z
    print 'Minimum integer is %d' % y

else:
    print 'Maximum integer is %d' % z
    print 'Minimum integer is %d' % x

"""
Explanation: <span style="color: #F5DA81; background-color: #610B4B">Example 3.2</span>: Use only one type of conditional operator for Exercise 3.1.
End of explanation
"""


x = 78
y = 78
z = 99

if x >= y >= z:
    print 'Maximum integer is %d' % x
    print 'Minimum integer is %d' % z

elif x >= z >= y:
    print 'Maximum integer is %d' % x
    print 'Minimum integer is %d' % y
    
elif y >= x >= z:
    print 'Maximum integer is %d' % y
    print 'Minimum integer is %d' % z

elif y >= z >= x:
    print 'Maximum integer is %d' % y
    print 'Minimum integer is %d' % x

elif z >= x >= y:
    print 'Maximum integer is %d' % z
    print 'Minimum integer is %d' % y

else:
    print 'Maximum integer is %d' % z
    print 'Minimum integer is %d' % x

"""
Explanation: <span style="color: #F5DA81; background-color: #610B4B">Exercise 3.1</span>: What if two or all integers have the same value. Try this and run the codes that solve Examples 3.1 and 3.2.
Codes in Example 3.1 seems more robust but 3.2 can be made more robust adding '&gt;=' instead of '&gt;'.
End of explanation
"""


for i in range(0,5,1):
    print i

"""
Explanation: 3.3 The <span style="color: #0000FF">for</span> and <span style="color: #0000FF">while</span> conditions
The <span style="color: #0000FF">$for$</span> and <span style="color: #0000FF">$while$</span> functions can be used to do repetitive action. The indentation with space (not tab) for statements inside the loop is also applied and consistent throughout the condition.
End of explanation
"""


for i in range(4,17,3):
    print i*2

"""
Explanation: Here the variable $i$ will be assigned the value of $0$ and cyclically incremented $5$ times by adding the integer $1$ to it each time. Only integer values are accepted in the parenthesis of the range statement. The first integer is the intial value of the $i$ variable, the second integer indicates (not-inclusive) the limiting value of the $i$ variable and the third integer represent the integer added to the variable $i$ for each cycles.
End of explanation
"""


for i in range(1,6,1):
    for j in range(6,11,1):
        print '%d x %d = %d' % (i,j,i*j)

"""
Explanation: The conditional looping can be nested as examplified below:
End of explanation
"""


for name in 'Numpy':
    print name

"""
Explanation: It is also possible to loop into the elements of a string (i.e. a $list$).
End of explanation
"""


z = 0
while z < 27:
    print z
    z = z + 6

"""
Explanation: The <span style="color: #0000FF">$while$</span> function works similarly like <span style="color: #0000FF">$for$</span> but initialization of the variable is performed before the <span style="color: #0000FF">$while$</span> statement and incrementing process is carried out as part of the loop argument.
End of explanation
"""


for i,j in enumerate('Numpy'):
    print i, '\t', j

for item in enumerate('Numpy'):
    print item

"""
Explanation: 3.3 The <span style="color: #0000FF">$enumerate$&#40; &#41;</span> function
The <span style="color: #0000FF">$enumerate$&#40; &#41;</span> function will make the <span style="color: #0000FF">$for$</span> looping condition looking more comprehensible. The argument for this function is a $list$.
End of explanation
"""


for item in enumerate('Numpy',5):
    print item

"""
Explanation: The <span style="color: #0000FF">$enumerate$&#40; &#41;</span> function allows the extraction of both the default position and its element of a $list$. In the first example, the two variables $i$ and $j$ will be assigned the $list$ default positional number and its element, respectively. In the second example, the variable $item$ will be assigned a tuple that consists the pair of default positional number and its element of the $list$.
The default positional number can be initiated to a different number. This can be done by passing the initial number as another argument in the <span style="color: #0000FF">$enumerate$&#40; &#41;</span> function.
End of explanation
"""


