F.Y.B.Sc Computer Science Python Practical
Practical no.1
Aim:Write a program to design and develop python program to implement various control statement using suitable examples
program 1: Write a program to check whether the entered number is divisible by 3(use if statement)
i=int(input("Enter any number"))
if(i%3==0):
print("%d is divisible by 3" %i)
Output:
Enter any number9
9 is divisible by 3
Program 2:Write a program to check whether the entered number is divisible by 3 or not(use if-else statement)
i=int(input("Enter any number: "))
if(i%3==0):
print("%d is divisible by 3" %i)
else:
print("%d is not divisible by 3" %i)
Output:
Enter any number: 5
5 is not divisible by 3
Output:
Enter any number: 12
12 is divisible by 3
Program 3:Write a program to check whether the entered number is even or odd(use if- else statement)
num = int (input ("Enter any number to test whether it is odd or even: "))
if (num % 2) == 0:
print ("The number is even")
else:
print ("The provided number is odd")
Output:
Enter any number to test whether it is odd or even: 89
The provided number is odd
Output:
Enter any number to test whether it is odd or even: 6
The number is even
Program 4: Write a python program to display 1 to 10 numbers.(use for loop)
for num in range(1, 11):
print(num)
Output:
1
2
3
4
5
6
7
8
9
10
Program 5: Write a python program to display each fruit from List(List traversing using for loop)
fruits = ["Apple", "Mango", "Banana", "Peach"]
for fruit in fruits:
print(fruit)
Output:
Apple
Mango
Banana
Peach
Program 6:Write a program to demonstrate nested for loop
# Running outer loop from 2 to 3
for i in range(2, 4):
# Printing inside the outer loop
# Running inner loop from 1 to 10
for j in range(1, 11):
# Printing inside the inner loop
print(i, "*", j, "=", i*j)
# Printing inside the outer loop
print()
Output:
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
Program 7: Write a python program to display your name 5 times(use while loop)
a=input("Enter your name: ")
b=1
while b<5:
print(a)
b=b+1
Output:
Enter your name: arjun
arjun
arjun
arjun
arjun
Program 8:Write a python program to use continue statement
#In Python, the continue keyword return control of the iteration to the beginning of the Python for loop or Python while loop. All remaining lines in the prevailing iteration of the loop are skipped by the continue keyword, which returns execution to the beginning of the next iteration of the loop.
# Python code to show example of continue statement
# looping from 10 to 20
for iterator in range(10, 21):
# If iterator is equals to 15, loop will continue to the next iteration
if iterator == 15:
continue
# otherwise printing the value of iterator
print( iterator )
Output:
10
11
12
13
14
16
17
18
19
20
Program 9:Write a python program to use break statement.
The break statement is used to terminate the loop immediately when it is encountered.
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2
Program 10:Write a python program to use pass statement.
In Python programming, the pass statement is a null statement which can be used as a placeholder for future code.
Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. In such cases, we can use the pass statement.
n = 10
# use pass inside if statement
if n >10:
pass
print('Hello')
Output:
Here, notice that we have used the pass statement inside the if statement.
However, nothing happens when the pass is executed. It results in no operation (NOP).