[Part 4] Python3: Basics for Beginners – Type of Loops

In Part 3, we read about simple and advanced If/Else function. In this blog, we will read about usage of loops in python.

1. While Loop

Till now, our program was exiting moment it displays result after code execution. However, we may need our application to continuosly re-run python function without explicity exiting. This can be achieved via While Loop. Refer example below,

Code

def coffee(quant):
    if which_coffee == "black":
        print(f"{which_coffee} will be {5*quant} dollars")
    elif which_coffee == "latte":
        print(f"{which_coffee} will be {6*quant} dollars")
    else:
        print(f"Sorry we don't have it")

while True:
    which_coffee=input("What would like to have?\n")
    quantity=input("How many cups?\n")
    coffee(int(quantity))

So we instructed our code to re run instructions specified under while loop till they are true, like, which_coffee and quantity to be asked as input always and for every input, coffee function will call defined logic.

Output

What would like to have?
black
How many cups?
5
black will be 25 dollars
What would like to have?
latte
How many cups?
2
latte will be 12 dollars
What would like to have?

2. For Loop

Unlike while loop, where we specified our code to run until certian condition is met, For loop can be used where we want to run code “For” every value “In” a list (Data type list have values in format [10, 20, 30]

Code

def coffee_order(coffee_cups):
       if coffee_cups > 0:
          print(f"Your bill is {coffee_cups*5} dollars")
       elif coffee_cups == 0:
          print(f"ok, Next order please")
       else:
          print(f"We don't do that here")

while True:
    user_order=input("How many Coffee cups you like to have?\n")
    for coffee_types in user_order.split():
        coffee_order(int(coffee_types))

In above example, I called by function to process list of input values (seperated by space) specifying name of list in for loop and processed sequantially by function using split() builtin function.

Output

How many Coffee cups you like to have?
10 40
Your bill is 50 dollars
Your bill is 200 dollars
How many Coffee cups you like to have?
-1
We don't do that here
How many Coffee cups you like to have?
0
ok, Next order please
How many Coffee cups you like to have?
15 0
Your bill is 75 dollars
ok, Next order please

3. List and Set

We have seen how For loop is calling values in List and processing them sequantially in previous code. Another type of data type is Set which provides similar usage as List, but ignores repeatitive values.

Code

coffee_list = ["americano", "espresso", "latte", "mocha", "americano"]

print(type (coffee_list))

print (coffee_list)

coffee_set = {"americano", "espresso", "latte", "mocha", "americano"}

print(type (coffee_set))

print (coffee_set)

In above example, I created two data types, LIST using [] brackets and SET using {}, and displayed output via print function of python. Check difference amoung two outputs. (Repeatitive value is not printed in Set)

Output

<class 'list'>
['americano', 'espresso', 'latte', 'mocha', 'americano']
<class 'set'>
{'americano', 'espresso', 'mocha', 'latte'}

In next blog, we will continue our python learnings by covering more avanced topics.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s