Python sample fizzbuzz program

Things we can learn while writing the Fizzbuzz program.

Iterator vs Iterate

>> Iterator is like a container

>> Iterate is like a single item in a container

>> python intendation should be maintained. 

>> range starts with 0 and end with length. ex: range(0,5) returns 0,1,2,3,4

>> modulo operator 5%2 returns 1, which returns only reminder.

>> def func(): >> after a function if the : is present which represents the code block is followed in next line.

>> if else statement checks for condition and move to next set of block condition, based on True or False.

>> and command in python will be in readable format and check for two conditions are getting satisfied or not.

>> we have only while loop in python, we don't have do while loop like other programming languages in python.

>> Any element divided by 0 is 0, so keep remind on this.


Fizzbuzz program in python

''' 

num=16

for itr in range(0, num+1):

    // print(itr)

    if(itr % 3) ==0 and (itr % 3) ==0:

        print("fizzbuzz")

    elif(itr % 3)==0:

        print("fizz")

    elif (itr % 5)==0: 

        print("Buzz")

    else: 

        print(itr)

'''

In the above program, we have used the for loop and solved the fizzbuzz program, so now we will try with while loop

while loop is checks condition and loop for infinite times. so while using this loop option please make sure to break the code.

'''

num=16

index=0

while index <= num:

    if(itr % 3) ==0 and (itr % 3) ==0:

        print("fizzbuzz")

    elif(itr % 3)==0:

        print("fizz")

    elif (itr % 5)==0: 

        print("Buzz")

    else: 

        print(itr)

    index++

'''


Comments