Python for beginners part III

Saroj Humagain
4 min readDec 2, 2018

In the previous blog, we discussed on the topic of “Data types in Python”.

Flow control in Python

Flow control controls the workflow of your program. Following are the available flow controls in Python:

1. If-else: The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.

Syntax:
if(condition):
statement1..
else:
statement2.

For example

print("Enter the first number:")
num1 = input()
print("Enter the second number:")
num2 = input()
if (num1 > num2):
print("%s is greater then %s"%(num1,num2))
else:
print("%s is greater then %s"%(num2,num1))
output
Enter the first number: 15 (if)
Enter the second number: 3
15 is greater then 3

2. Nested if else: If we have multiple conditions to check then we use multiple if-else together. In Python, elif keyword is used which in other programming language means else if.

Syntax:
if(condition1):
statement1..
elif(condition2):
statement2..
else:
statement3

For example, lets redo the above program

print("Enter the first number:")
num1 = input()
print("Enter the second number:")
num2 = input()
if (num1 > num2):
print("%s is greater then %s"%(num1,num2))
elif(num1 < num2):
print("%s is greater then %s"%(num2,num1))
else:
print("They are the same number")
output
Enter the first number: 15 (if)
Enter the second number: 15
They are the same number

3. For: For statement is basically a loop and is used for iterating over a sequence it can be either a list, a tuple, a dictionary, a set, or a string.

Syntax:
for interating_var in sequence:
execute statement

For example

nepAlph = ["ka", "kha", "ga", "gha"]
for x in nepAlph:
print(x)
output
ka
kha
ga
gha

And we use range() function to define the sequence.
For example

for x in range(5):
print(x)
output

0
1
2
3
4
# The starting value of range is always 0 until you override it, you must give the stopping value and the value increases by 1 until you enter manually.
for x in range(2,13,2):
print(x)
output
4
6
8
10
12
#In above example,
range(4,13,2), 4 is the staring value, 13 is a end value and is incremented by 2.
Note: we can also go from higher starting point to lower, just change the starting value, end value and give the negative counter.

4. While: While loop can execute the statement as long as the condition is true. The loop breaks when the condition is false.

Syntax:
while(condition is true):
statement1

For example

a = 0
while a <= 5:
print(a)
a+=1
output
0
1
2
3
4

5. Break: Break statement is used to stop the loop even the condition is true.
For example

a = 0
while a <= 5:
if(a == 2):
break
print(a)
output
0
1
# Here, the while loop breaks when the value of a reaches to 2.

6. Continue: Unlike break, continue is used when we want to just stop the current iteration and continue with the next one.

a = 0
while a <= 5:
a+=1
if(a == 2):
continue
print(a)
output
1
3
4
# Here, the iteration stops when the value of a is 2 and continues form 3 again.

Function in Python

Function is a block of organized and reusable sets of instructions that are used to perform some related action. Function is important because
1. Function enables us to reuse the same code which minimizes redundancy. Suppose we have 10 line of code and in our man program they are going to appear 6 times that means, we have to write 60 line of redundant line of codes. We can resolve this issue using the function.

2. Procedural decomposition makes things organized. It is a strategy for organizing a program in a number of parts and it usually implies a specific way to organize a text and reduces program complexity.

There are two types of functions:

1. User-defined function: User-defined functions are the one which is created by you as per the requirement of the program. There are several rules to define a function:
i. Function block begins with the keyword def followed by the function name and parameters.
ii. Those parameters consist of an input parameter or argument.
iii. The code block within every function is intended and should start with the colon(:)
iv. return[expression] is used to execute the expression and return to the function. A return statement with no arguments is the same as return None.

syntax
def function_name():
statement1..
statement2...
..........

For example

def add(num1,num2):
sum = num1 + num2
print("The sum of %s and %s is %s."%(num1,num2,sum))
add(12,13) #calling the function
output
The sum of 12 and 13 is 25.

2. Built-in function: Built-in functions are those which are predefined and we don’t need to create them we can just call them when we need them. Some of the built-in functions are as follows:

abs() : return absolute value
all(): return True if all items in an iterable object are true
any(): return True if any items in an iterable object are true
bin(): return the binary value of a number
bool(): return the boolean value of the specified object
round(): return the round off value

I broke down the blog into three parts if you are thinking of starting from the beginning. Here is the link to the first and second blog.

--

--

Saroj Humagain

I basically write on data science, ML and AI and sometimes random things.