Wednesday, 2 November 2016

Loop and if in Python

Hi All,

See a program to print event numbers from 1 to 8

loop.py
---------------------------------------------------------------------------------------
print 'Displaying even numbers from 1 to 8'
for i in range(1,9):
 if i%2==0:   
  print i

---------------------------------------------------------------------------------------

Then type the command : python loop.py  


 gurst@dell:~$ python loop.py

NB: See the spaces in front of 'if statment' and 'print i'. 

 
Assignments

1. Factorial
2. Prime number checking
3. Sum of Series
4. Nested loop for a patter like this

*
* *
* * *
* * * *
* * * * * 

No comments:

Post a Comment