Python Week-1
Python Week-1
Department of CSE
CRT
Python Programming
Week-1
Topics:
Control statements
o if
o if-else
o elif
o nested-if
o while loop
o for loop
o break
o continue
o pass
if statement:
syntax:
if test expression:
statement(s)
In Python, the body of the if statement is indicated by the indentation. Body starts
with an indentation and the first un indented line marks the end.
Example:
num = 3
if num > 0:
print(num, "is a positive number.")
print("This is always printed.")
if-else statement:
Syntax of if...else:
if test expression:
Body of if
else:
Body of else
Body of if executed only when test condition is True. If the condition is False, body of else is
executed. Indentation is used to separate the blocks.
Example:
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
if...elif...else statement:
Syntax:
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
• The elif is short form for else if. It allows us to check for multiple expressions.
• If the condition for if is False, it checks the condition of the next elif block and so on.
• If all the conditions are False, body of else is executed.
• Only one block among the several if...elif...else blocks is executed according to the
condition.
• The if block can have only one else block. But it can have multiple elif blocks.
• Example:
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Nested if statement:
• We can have a if...elif...else statement inside another if...elif...else statement. This is
called nesting in computer programming.
• Any number of these statements can be nested inside one another. Indentation is the
only way to figure out the level of nesting.
Loops:
Loops are often used when we have a piece of code that we need to repeat for (i) N
number of times or (ii) until a condition becomes true or false
• for loop
• while loop
• Nested loops
• break
• continue
for loop statement:
for, is often used when we have a piece of code which we want to repeat "n" number
of times.
Syntax:
for variable in range(start,end[,step]):
body of the for loop
Example:
for i in range(1,5):
print(i)
Output:
1
2
3
4
Range function iterates i value starting from 1st argument up to but not including 2nd
argument. So i value is iterated only up to 4, but not 5
Note: different forms of range function to be explained
while loop statement:
A while loop statement in Python programming language repeatedly executes a target
statement as long as a given condition is true.
Syntax:
while test_expression:
body
Example:
n = 10
i=1
while i <=n:
if(i%2!=0):
print(i,end=" ")
i=i+1
Output:
13579
Nested loops:
Python programming language allows using one loop inside another loop.
Syntax for nested for loop:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
Syntax for a nested while loop:
while expression:
while expression:
statement(s)
statement(s)
Example :
n=5
for row in range(1,n+1):
for col in range(1,row+1):
print(row,end=" ")
print()
Output:
1
22
333
4444
55555
break statement:
The break statement terminates the loop containing it. Control of the program flows to
the statement immediately after the body of the loop.
Example:
for val in "string":
if val == "i":
break
print(val,end=” “)
Output:
str
• If break statement is inside a nested loop (loop inside another loop), break will
terminate the innermost loop.
continue statement:
The continue statement is used to skip the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next iteration.
Example:
for val in "string":
if val == "i":
continue
print(val,end=” ”)
Output:
strng
pass statement:
• The pass statement is a null operation; nothing happens when it executes.
• The pass statement in Python is used when a statement is required syntactically but
you do not want any command or code to execute.
• Sometimes there may be situations so that we do not want to execute any statement
when a particular condition is true or false.
x=5
y=10
if(x==y):
else:
print("not equal")
Here we do not want to execute any statement when the if condition becomes true,
but when this code is executed, we get an error saying “expected and indented block”.
MCQs
testmoz.com/8676792
1. What kind of loop I can use to accept the number inputs from the user until the sum of
numbers given by the user is 100
A. while
B. for
Ans: A
7. What is the output of the following for loop and range() function for num in range(-2,-5,-1):
print(num, end=", ")
A. -2, -1, -3, -4
B. -2, -1, 0, 1, 2, 3,
C. -2, -1, 0
D. -2, -3, -4,
Ans: D
8. Given the nested if-else below, what will be the value x when the code executed successfully
x=0
a=5
b=5
if a > 0:
if b < 0:
x=x+5
elif a > 5:
x=x+4
else:
x=x+3
else:
x=x+2
print(x)
A. 0
B. 4
C. 3
D. 2
Ans: C
10. for i in range(-3), how many times this loop will run ?
A. 0
B. 1
C. Infinite
D. Error
Ans: A
11. In which of the following loop in python, we can check the condition?
A. for loop
B. while loop
C. do-while loop
D. while and do-while loop
Ans: B
12. 4 is 100 in binary and 11 is 1011. What is the output values of the following bitwise
operators?
a=4
b = 11
print(a | b)
print(a >> 2)
A. 15 and 1
B. 14 and 1
C. Error
Ans: A
15. print(10=10)
A. True
B. False
C. error
D. none of the above
Ans: C
i=2
while(i>0):
i=i-1
A. 2
B. 3
C. 1
D. 0
Ans: A
20. What keyword would you use to add an alternative condition to an if statement?
A. else if
B. elseif
C. elif
D. None of above
Ans: C
n=7
c=0
while(n):
if(n>5):
c=c+n-1
n=n-1
else:
break
print(n)
print(c)
A. 5 11
B. 6 5 2
C. 3
D. 5 2
Ans: A
23. Which of the following Python code will give different output from the others?
A. for i in range(0,5):
print(i)
B. for j in [0,1,2,3,4]:
print(j)
C. for k in [0,1,2,3,4,5]:
print(k)
D. for l in range(0,5,1):
print(l)
Ans: C
26. Which of the following sequences would be generated by the given line of code?
range (5, 0, -2)
A. 5 4 3 2 1 0 -1
B. 5 4 3 2 1 0
C. 5 3 1
D. None of above
Ans: C
27. When does the else statement written after loop executes?
A. When break statement is executed in the loop
B. When loop condition becomes false
C. Else statement is always executed
D. None of the above
Ans: B
30. Which of the following statement skips the remaining statements in the current iteration and
jumps to next iteration?
A. continue
B. break
C. jump
D. exit
Ans: A
31. To break the infinite loop , which keyword we use ?
A. continue
B. break
C. exit
D. none of the above
Ans: B
32. Which of the following is a null statement in Python?
A. break
B. continue
C. None
D. Pass
Ans: D
i=5
while(i>=1):
if i==3:
continue
print(i,end=" ")
i=i-1
A. 5 4 3 2 1
B. 5 4 2 1
C. Error
D. Infinitely iterate
Ans: D
B)
x=10
if(x==10):
else:
print("else block")
C)
x=10
if(x==10):
pass
else:
print("else block")
35. When you execute a break command inside the inner loop of a pair of nested loops ...
A. ...both the inner loop and the outer loop are interrupted.
B. ... the outer loop is interrupted and the inner loop is not affected.
C. ... the inner loop is interrupted and the outer loop is not affected.
D. ... neither the inner loop, nor the outer loop are interrupted.
Ans: C
Programs
1. Divisible by n(control)
Write a program to check whether given number is divisible by n or not(n is supplied as input
by the user)
Input Format
Consists of 2 integers: 1. Number to check whether it is divisible by n 2. n value
Constraints
.
Output Format
display "yes" or "no"
Sample Input 0
15
5
Sample Output 0
yes
Sample Input 1
15
4
Sample Output 1
No
2. student grade
Write a python program to display grade of student based on the conditions given below:
marks greater than 90, display A grade
marks greater than 80 and less than or equal to 90, display B grade
marks greater than 60 and less than or equal to 80, display C grade
marks greater than or equal to 40 and less than or equal to 60, display D grade
marks less than 40, display fail.
Input Format
marks entered by user.
Constraints
.
Output Format
Display grade of Student
Sample Input 0
72
Sample Output 0
C Grade
Sample Input 1
22
Sample Output 1
Fail
6. factors 4
Write a program to find the factors of a number entered by user excluding 1 and itself.
Input Format
number entered by user.
Constraints
.
Output Format
display all the factors
Sample Input 0
99
Sample Output 0
3 9 11 33
Sample Input 1
250
Sample Output 1
2 5 10 25 50 125
7. pattern 1(control)
Write a program to print the pattern shown in sample test cases
Input Format
integer value n
Constraints
.
Output Format
refer sample test cases
Sample Input 0
6
Sample Output 0
1
22
333
4444
55555
666666
Sample Input 1
2
Sample Output 1
1
22
9. Armstrong number 17
Write a python program to check whether a number entered by user is armstrong or not.
Input Format
number entered by user.
Constraints
Assume only 3 digit numbers are entered by user.
Output Format
display armstrong or not armstrong.
Sample Input 0
370
Sample Output 0
armstrong
Sample Input 1
156
Sample Output 1
not armstrong