Experiment - 5
Experiment - 5
Problem Statement:
1) Write a Python program to get the Fibonacci series between 0 to 50 2) Write a Python program to sum
of two given integers. However, if the sum is between 15 to 20 it will return 20.
3) Write a Python program that accepts a string and calculates the number of digits and letters.
Outcomes: Students will be able to learn to create basic python sketch Prerequisite:
Hardware requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB
Software requirements:
Operating systems: Windows* 7 or later, macOS, and Linux.
Python versions: 2.7.X, 3.6.X. , Jupyter Notebook
Theory:
Python Loops
The flow of the programs written in any programming language is sequential by default. Sometimes we may
need to alter the flow of the program. The execution of a specific code may need to be repeated several
numbers of times.
For this purpose, programming languages provide various types of loops which are capable of repeating some
specific code several numbers of times. Consider the following diagram to understand the working of a loop
statement.
The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so
that instead of writing the same code again and again, we can repeat the same code for a finite number of times.
For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times,
we can print inside a loop which runs up to 10 iterations.
Advantages of loops
There are the following advantages of loops in Python.
Instructions: N.A.
2) Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it
will return 20.
3) Write a Python program that accepts a string and calculates the number of digits and letters.
Ans : Python For loop is used for sequential traversal i.e. it is used for iterating
over an iterable like String, Tuple, List, Set or Dictionary.
Syntax:
Ans : Python While Loop is used to execute a block of statements repeatedly until a
the given condition is satisfied. And when the condition becomes false, the line
immediately after the loop in the program is executed.
Syntax:
while expression:
statement(s)
Ans : Python doesn't have a do-while loop. But we can create a program like this. The
do while loop is used to check condition after executing the statement. It is
like a while loop but it is executed at least once.
General Do While Loop Syntax :
do
{
//statement
} while (condition);
Ans : The control structure is known as a post-test loop. This means that the code
must always be executed first and then the expression or test condition is
evaluated. If it is true, the code executes the body of the loop again.