Introduction

Python is a high-level programming language that is well known and widely used in science and engineering fields. It is user-friendly and allows users to interact with a computer using simple English phrases instead of complex assembly or binary commands.

Loops

Here comes the dynamic part, python presents for-loops and while-loops, the main principle is as follows:

for-loops

For loops iterate over a given sequence, using the “range” functions is that the function returns a new list with numbers of that specified range. Note that the range function is zero based.

The range() function has 3 useful argument range(start, stop, step)

Prints out the numbers 0,1,2,3,4

1
2
for x in range(5):
    print(x)
Output:
1
2
3
4
5
0
1
2
3
4

Prints out 3,4,5

1
2
for x in range(3, 6):
    print(x)
Output:
1
2
3
3
4
5

Prints out 3,5,7

1
2
for x in range(3, 8, 2):
    print(x)
Output:
1
2
3
3
5
7

While - loops

The other primitive loop that is present in python is the while loop; it is the infinite loop, it starts with a boolean True whether as a condition rule or directly as True.

Prints out 0,1,2,3,4

1
2
3
4
count = 0
while count < 5:
    print(count)
    count += 1  # This is the same as count = count + 1
Output:
1
2
3
4
5
0
1
2
3
4

break and continue

break is used to exit a loop, whereas continue is used to skip the current block, and return to the statement.

Prints out 0,1,2,3,4

1
2
3
4
5
6
count = 0
while True:
    print(count)
    count += 1
    if count >= 5:
        break
Output:
1
2
3
4
5
0
1
2
3
4

Prints out only odd numbers - 1,3,5,7,9

1
2
3
4
5
for x in range(10):
    # Check if x is even
    if x % 2 == 0:
        continue
    print(x)
Output:
1
2
3
4
5
1
3
5
7
9

else clause

else clause can be used with loops, when the loop condition for the statement fails then code part in “else” is executed. If a break statement is executed inside the loop then the “else” part is skipped. Note that the “else” part is executed even if there is a continue statement.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Prints out 0,1,2,3,4 and then it prints "count value reached 5"

count=0
while(count<5):
    print(count)
    count +=1
else:
    print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
    if(i%5==0):
        break
    print(i)
else:
    print("this is not printed because for loop is terminated because of break but not due to fail in condition")
Output:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
0
1
2
3
4
count value reached 5
1
2
3
4

Conditionals clauses

The boolean logic is used to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated. As it was seen in the Boolean Operators how the assignment can be done.

Notice that variable assignment is done using a single equals operator =, whereas comparison between two variables is done using the double equals’ operator ==. The “not equals” operator is marked as !=.

The and and or boolean operators allow building complex boolean expressions.

The in operator could be used to check if a specified object exists within an iterable object container, such as a list.

Unlike the double equals operator ==, the is operator does not match the values of the variables, but the instances themselves.

Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
score = 85

if score == 100:
    grade = "A++"
elif score >= 95:
    grade = "A+"
elif score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print("Your grade is:", grade)
Output:
1
Your grade is: B