a = 5 b = 1 while b <= 5: print ("%d * %d = %d" %(a, b, a*b)) b+=1 ----------Output--------- 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
a = 1 while a <= 3: b = int (input ("enter a no: ")) if b == 0: print ("exiting loop with break command, 'else' is not executed") break a+=1 else: print ("loop exited without executing break command") ''' In the above program, if your input is 0, while loop will "break" and the code inside "else" clause will not get executed. If you are using other numbers as inputs, while loop will complete its full cycle and the code inside "else" clause will be considered '''
a = 1 while a < 10: print (a, end=',') a+=1 ----------Output--------- 1,2,3,4,5,6,7,8,9, a = 1 while a < 10: print (a, end=' ') a+=1 ----------Output--------- 1 2 3 4 5 6 7 8 9
for a in range(10): print (a, end=" ") ----------Output--------- 0 1 2 3 4 5 6 7 8 9 a = 5 for b in range(1, 5): print ("%d * %d = %d" %(a, b, a*b)) ----------Output--------- 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 a = [10,20,30,40,50] for b in a: print (b+5, end=" ") ----------Output--------- 15 25 35 45 55
1. Write a python program to print the square of all numbers from 0 to 10
2. Write a python program to find the sum of all even numbers from 0 to 10
3. Write a python program to read three numbers (a,b,c) and check how many numbers between ‘a’ and ‘b’ are divisible by ‘c’
4. Write a python program to get the following output
1-----99
2-----98
3-----97
. .
. .
. .
98-----2
99-----1
5. Write a python program to read a number and print the binary of that number (hint: if ‘a’ is a string , a[::-1] will be reverse of that string)
6. Write a python program to read four numbers (representing the four octets of an IP) and print the next five IP address
Eg:
Input:
192 168 255 252
----------Output---------
192 168 255 253
192 168 255 254
192 168 255 255
192 169 0 0
192 169 0 1
7. Write a python program to print the factorial of a given number
8. Write a python program to print the first 10 numbers Fibonacci series
9. Write a python program to read a number and print a right triangle using "*"
Eg :
Input : 5
----------Output---------
*
* *
* * *
* * * *
* * * * *
10. Write a python program to check given number is prime or not
11. Write a python program to print all prime numbers between 0 to 100 , and print how many prime numbers are there.
12. a, b, c = 0, 0, 0 . Write a python program to print all permutations using those three variables
Output : 000 , 001 ,002, 003, 004, 0005 ,006, 007, 008, 009, 010, 011 …… 999
Next chapter: Python training : Strings and String Methods
Previous chapter: Python training : Variables, Data types and If else control statements