Professional Documents
Culture Documents
Example 1
1. num = int(input("enter the number?"))
2. if num%2 == 0:
3. print("Number is even")
Output:
enter the number?10
Number is even
Example 2 : Program to print the largest of the
three numbers. The syntax of the if-else statement is given below.
1. a = int(input("Enter a? ")); 1. if condition:
2. b = int(input("Enter b? ")); 2. #block of statements
3. c = int(input("Enter c? ")); 3. else:
4. if a>b and a>c: 4. #another block of statements (else-block)
5. print("a is largest"); Example 1 : Program to check whether a person is
6. if b>a and b>c: eligible to vote or not.
7. print("b is largest"); 1. age = int (input("Enter your age? "))
8. if c>a and c>b: 2. if age>=18:
9. print("c is largest"); 3. print("You are eligible to vote !!");
4. else:
Output: 5. print("Sorry! you have to wait !!");
Enter a? 100 Output:
Enter b? 120 Enter your age? 90
Example 1
1. n = int(input("Enter the number of rows you want to pri
nt?"))
2. i,j=0,0
3. for i in range(0,n):
4. print()
5. for j in range(0,i+1):
6. print("*",end="")
Output:
Enter the number of rows you want to print?5
*
**
***
Example ****
1. i=1
*****
2. n=int(input("Enter the number up to which you want to
print the natural numbers?"))
3. for i in range(0,10): Using else statement with for loop
4. print(i,end = ' ')
Output: Unlike other languages like C, C++, or Java, python
0123456789 allows us to use the else statement with the for loop
Python for loop example : printing the table of the which can be executed only when all the iterations are
given number exhausted. Here, we must notice that if the loop
1. i=1; contains any of the break statement then the else
2. num = int(input("Enter a number:")); statement will not be executed.
3. for i in range(1,11): Example 1
4. print("%d X %d = %d"%(num,i,num*i)); 1. for i in range(0,5):
Output: 2. print(i)
Enter a number:10 3. else:print("for loop completely exhausted, since there i
10 X 1 = 10 s no break.");
10 X 2 = 20 Output:
10 X 3 = 30 0
10 X 4 = 40 1
10 X 5 = 50 2
10 X 6 = 60 3
10 X 7 = 70 4
10 X 8 = 80 for loop completely exhausted, since there is no break.
10 X 9 = 90
10 X 10 = 100 Example 2
1. for i in range(0,5):
[By- Prof. Narendra Kumar] Page 4
Unit 2
2. print(i)
3. break;
4. else:
5. print("for loop is exhausted");
6. print("The loop is broken due to break statement...cam
e out of loop")
Output:
0
Example 1 2
1. while (1):
2. print("Hi! we are inside the infinite while loop"); Nested While Loop
Output:
Hi! we are inside the infinite while loop Syntax
(infinite times) for iterating_var in sequence:
Example 2 for iterating_var in sequence:
1. var = 1 statements(s)
2. while var != 2: statements(s)
3. i = int(input("Enter the number?"))
4. print ("Entered value is %d"%(i)) Example : Find the prime numbers from 2 to 15
Output:
Enter the number?102 i=2
Entered value is 102 while(i < 100):
Enter the number?102 j=2
Entered value is 102 while(j <= (i/j)):
Enter the number?103 if not(i%j): break
Entered value is 103 j=j+1
Enter the number?103 if (j > i/j) : print i, " is prime"
(infinite loop) i=i+1
Example 2
1. i=1; #initializing a local variable
2. #starting a loop from 1 to 10