Billy Okeyo

Posted on

# Introduction to Python Programming Basics(Part 2)

Hey there, this is a Continuation from the part 1 which i did earlier.
We'll get into conditionals, loops and modules.

## Conditionals

If/else statements are used to decide if a certain condition is met based on being true or false

``````i = 22 j = 10
if i > j:
print(i)
else:
print(j)
``````

In the above example it's evident i is greater than j, thus the if statement will be outputted. It will output:

``````22
``````

Now note, i won't be outputted but the value assigned to i is what will be outputted that's why in the print statement we don't have any quotes sorrounding i. Now let's get in a more detailed example.

``````a = 2 b = 5
if a > b:
print(a " is greater than " b)
elif a == b:
print(a " is equal to " b)
else:
print(a " is less than " b)
``````

Can you try to guess what will be outputted in the above example
........
Still thinking, well the last statement will be outputted, simply because a is less than b, so we will get

``````2 is less than 5
``````

As in the example above we are calling the variable a and b and not strings a and b so the value assigned to the variable is what will be outputted then we have some statements in quotes i.e ' is greater than ', ' is equal to ' and ' is less than ', this will be considered as strings since we have sorrounded them with quotes. From the example above, we also note that there is some space between the quites and the strings, this is because python doesn't assign whitespaces by it self, let's look at the example below:

``````name = "John Doe"
print("My name is:" + name) print("My name is: " + name) print("My name is:",name)
``````

From the above example it is evident the statements are the same but once it is run, you'll notice the difference. The output will be:

``````My name is:John Doe
My name is: John Doe
My name is: John Doe
``````

You'll notice the first output doesn't have any whitespace because we didn't have a whitespace between the colon and the closing quote. The second output displays correctly because we introduced a white space same to the third output. You might be asking yourself what's the + sign and comma doing there, well let's talk abit about that. In python we can concatenate strings with strings and of course with other data types as well. Now the + can be used to concatenate strings with strings but not strings with integers while , can only used with strings and strings, strings and integers etc. Hope we are now clear on that.

## Loops

Loops are used to execute a continuous statement over and over again until a certain condition is met.

#### For Loop

Let's look at the example below

``````grades = [20, 40, 50, 30, 70]

students = ["John","Mary","Doe","Jane"]
for student in students:
print("Name is: " + student)
``````

In the above example, we are creating a loop that iterates through list of grades and students and prints out each one of them, the output woll be as follows;

``````Individual grade is: 20
Name is: John
Name is: Mary
Name is: Smith
Name is; Jane
``````

We can use break and continue to do specific things in a loop.
Break will be used to stop the current loop while continue will be used to jump a specific value and continue to the next value. Lets look at an example;

``````grades = [20, 40, 50, 30, 70]
break
``````

This will have the output below. The for loop will run and upon finding a grade that is equal to 40 the loop will stop;

``````Individual grade is: 20
``````

Now let's look at how continue is implemented;

``````grades = [20, 40, 50, 30, 70]
continue
``````

Now in the above example, the loop will run and once it gets a grade which is equal to 50 it skips that and continues to the next value, thus we will have an ouput as shown below;

``````Individual grade is: 20
``````

#### While Loops

While loops are used to execute a statement if a certain condition is true
Lets have an example;

``````number = 0
while number <= 5:
print("Number:" , number)
number += 1
``````

In the above example we want the while loop to execute until number is less than or equal to 5, and when 5 is reached the condition becomes false and the loop is stopped.
Number += 1 is a short form of saying number = number + 1 so the number keeps adding by one. So we'll get the following output

``````Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
``````

## Modules

A module is a file containing certain functions that you can use in your application. Some refer to them as libraries. We have modules which are user defined, others come pre-loaded already with python and others are downloadable through the pip manager("I'll cover this later on")
To use a module or a library we have to import it into our workarea or project and we use the keyword import

``````import datetime
from datetime import date

today = date.today()
print(today)
``````

In the above example it will output the current date.
So we have imported datetime which is a core module in python then from the datetime module we have imported date because we only want to use the date function from the datetime module.
That's all for now.
See you soon as i will be resuming from where i have left. Till then stay safe and don't forget to comment and say what part you want me to tackle next....