Prerequisite for Data Structures and Algorithms in Python

Michael Otu - May 25 '22 - - Dev Community

Whether it is Data Structures or Algorithms first, it is still the same.

We would look briefly at:

  • Variables and expressions
  • Functions
  • Condition
  • Iteration

Variables and expressions

Variables are of the form, var_name = value. There is no semicolon.

These are some examples:

>> name = "daniel"
>> age = 34
>> height = 6.6
>> numbers = [1, 2, 3, 4, 5]
>>
Enter fullscreen mode Exit fullscreen mode

An expression will result in a value or "return" a value.

>> 2 + 3
>>
>> import datetime
>>
>> currentYear = datetime.datetime.now().year
>> age = 34
>> yob = currentYear() - age
>>
>> # some operators
>> # +,  -,  /, *, **, %
>> # >, <, ==, <=, >=,  !=
>>
Enter fullscreen mode Exit fullscreen mode

functions

This is how we would create a function in python:

def func_name([parameters]):
    # func_body

Enter fullscreen mode Exit fullscreen mode

A function that prints 3 to the screen:

def print_number():
    print(3)

print_number()
# 3

Enter fullscreen mode Exit fullscreen mode

A function that returns 3

def returnNumber():
    return 3

res = returnNumber()
print(res)
# 3

Enter fullscreen mode Exit fullscreen mode

A function with a parameter a. We add 2 to a and return the results:

def addTwo(a):
    b = a + 2
    return b

res = addTwo(3)
print(res)
# 5

Enter fullscreen mode Exit fullscreen mode

Decision structure

An if statement is of the form:

if condition:
    # if-body
Enter fullscreen mode Exit fullscreen mode

The body/block of the if statement is executed when the condition evaluates to True

age = 20
res = age > 18

if res:
    print(age)

# prints 20 because age, 20, is greater than 18

Enter fullscreen mode Exit fullscreen mode

We can add an else part to the if statement. This is the part of the code that is executed when the condition evaluates to False.

if condition:
    # if-body
else:
    # else-body

Enter fullscreen mode Exit fullscreen mode

This is an if-else statement:

age = 12
res = age > 18

if res:
    print(age)
else:
    print("sorry, age must be above 18")
# prints, sorry, age must be above 18, since age is less than 18

Enter fullscreen mode Exit fullscreen mode

We can use the and and or to compound logical expressions. and and or are known as logical operators.

age = int(input("Enter age: "))

if age > 18 and age % 2 == 0:
    print("Age is greater than 18 and it is even")
else:
    print("age must be greater than 18 and it is even")

Enter fullscreen mode Exit fullscreen mode

We can also nest the if-else statements

age = int(input("Enter age: "))

if age > 18:
    if age % 2 == 0:
        print("Age is greater than 18 and it is even")
   else:
        print("Age must be even")
else:
    print("age must be greater than 18")
Enter fullscreen mode Exit fullscreen mode

Iteration

This is known as looping. We for loop and while-loop in python

This is a for-loop that prints numbers from 0 to 5 (exclusive).

# for i in range(5):
# for i in range(0, 5):
for i in range(0, 5, 2):
    print(i)
Enter fullscreen mode Exit fullscreen mode

We can print the content of a list using a for-loop

numbers = [1, 2, 3, 4, 5]

for i in numbers:
    print(i)

Enter fullscreen mode Exit fullscreen mode

Loops are index-based and we can pass the size of the list as an argument to the range function to determine where the loop ends.

for i in range(len(numbers)):
    print(numbers[i])

Enter fullscreen mode Exit fullscreen mode

We can also nest for loops:

numbers = [1, 2, 3, 4, 5]

for i in numbers:
    for j in numbers:
        print(i * j)

Enter fullscreen mode Exit fullscreen mode

This is how we write for loops in python:

while condition:
    # while-body

Enter fullscreen mode Exit fullscreen mode

Let print from 0 to 5 inclusive

start = 0
end = 5
step = 1

# this is the same as the above
# start, end, step = 0, 5, 1

while start <= end:
    print(start)
    start = start + step
    # start += step

Enter fullscreen mode Exit fullscreen mode

We can use a while loop to loop through a list

numbers = [1, 2, 3, 4, 5]
i = 0

while i < len(numbers):
    print(numbers[i])
    # i+= 1
    i = i + 1

Enter fullscreen mode Exit fullscreen mode

We can nest while loops too

i = 0

while i < 5:
    j = 0

    while j < i+1:
        print(i*j)
        j += 1

    i += 1

Enter fullscreen mode Exit fullscreen mode

So, what is next?

So what is next? A series on Algorithms and Data Structures in Python. It is more or less Introduction to data structures and Algorithm 1. We would be using Python. This means we can use another language. Stay tuned as they say. Seriously stay tuned.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .