Hello Everyone,
Welcome to Day 28 of DevOps Learning, Today we explore Python example programs using for loops with solutions.
Example program 1:
Count the number which is divisible by 3 and 5, Range(1 - 100)
solution:
Here we used the empty variable "count", which stores the number of counts while the range of Iterations becomes True (range of number which is divisible by both 3 and 5)
Example program 2:
write the program to read 10 numbers from the keyboard and find their sum and average
solution:
As we have seen before about Python Lists[ ], here we used to store the values in the list (a)
List
Lists are used to store multiple items in a single variable.
and defined range(10) between (1, 11) and we get the input through Loop iteration with the range of(10)
we used input variable(b) with string casting function str() to display the number of iterations to the user.
- str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
and we used the append() method
append() will add a single item to the end of the existing list. The Python append() method only modifies the original list. It doesn’t return any value. The size of the list will increase by one.
append will store the values inside the List(a)
then we print the sum of the List(a) using sum() function
The sum()
function returns a number, the sum of all items in an iterate.
then we print the Average of the list using sum() and len() functions
Example program 3:
solution:
Congrats🎉, On Learning the Python For loop example programs.
Happy Learning :)