Python Week-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

RAGHU ENGINEERING COLLEGE (A)

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

2. What will be the output of the following Python code?


i=1
while True:
if i%3 == 0:
break
print(i)
i+=1
A. 1 2
B. 1 2 3
C. error
D. none of above
Ans: A

3. What is the output of the following if statement


a, b = 12, 5
if a + b:
print('True')
else:
print('False')
A. True
B. False
Ans: A

4. What is the output of the following range() function


for num in range(2,-5,-1):
print(num, end=", ")
A. 2, 1, 0
B. 2, 1, 0, -1, -2, -3, -4, -5
C. 2, 1, 0, -1, -2, -3, -4
Ans: C

5. if -3 will evaluate to true


A. Yes
B. No
Ans: A

6. What is the output of the following loop


for l in 'Jhon':
if l == 'o':
pass
print(l, end=", ")
A. J, h, n,
B. J, h, o, n,
C. Error
Ans: B

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

9. What is the value of x


x=0
while (x < 100):
x+=2
print(x)
A. 101
B. 100
C. 99
D. none of above
Ans: B

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

13. While(0), how many times a loop run ?


A. 0
B. 1
C. Infinite
D. Error
Ans: A

14. while(1): print(2), How many times a loops run ?


A. 0
B. 1
C. Infinite
D. Error
Ans: C

15. print(10=10)
A. True
B. False
C. error
D. none of the above
Ans: C

16. In a Python program, a control statement,


A. Defines program-specific data structures
B. Directs the order of execution of the statements in the program
C. Manages the input and output of control characters
D. Dictates what happens before the program starts and after it terminates
Ans: B

17. Which of the following is True regarding loops in Python?


A. Loops should be ended with keyword "end".
B. No loop can be used to iterate through the elements of strings.
C. Keyword "break" can be used to bring control out of the current loop.
D. Keyword "continue" is used to continue with the remaining statements inside the loop.
Ans: C

18. How many times will the loop run?

i=2
while(i>0):
i=i-1
A. 2
B. 3
C. 1
D. 0
Ans: A

19. Which one of the following is a valid Python if statement :


A. if a>=2 :
B. if(a >= 2):
C. Only A
D. Both are valid
Ans: D

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

21. Which statement will check if a is equal to b?


A. if a=b:
B. if a==b:
C. if a===b:
D. if a==b
Ans: B

22. What will be the output of given Python code?

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

24. What will be the output of the following Python code?


for i in range(0,2,-1):
print("Hello")
A. Hello
B. Hello Hello
C. No Output
D. Error
Ans: C
25. Which of the following is a valid for loop in Python?
A. for(i=0; i < n; i++)
B. for i in range(0,5):
C. for i in range(0,5)
D. for i in range(5)
Ans: B

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

28. What is the output of the following program?


i=1
while True:
if i%3 == 0:
break
print(i,end=" ")
i+= 1
A. 1 2
B. 1
C. 1 2 3
D. No output
Ans: A

29. To break the infinite loop , which keyword we use ?


A. continue
B. break
C. exit
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

33. What is the output of below code,

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

34. Which of the following is valid code in Python?


A)
x=10
if(x==10):
else:
print("else block")

B)
x=10
if(x==10):

else:
print("else block")

C)
x=10
if(x==10):
pass
else:
print("else block")

D) All are valid


Ans: C

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

3. odd number greater than 1000(control)


Write a program to check whether the given number is an odd number which is greater than
1000
Input Format
consists only one integer
Constraints
.
Output Format
print either "yes" or "no"
Sample Input 0
1511
Sample Output 0
yes
Sample Input 1
101
Sample Output 1
no

4. odd numbers within range(control)


Write a program to display all the odd numbers from 1 to n seperated by a space.
Input Format
integer value n
Constraints
do not use 'if'
Output Format
odd numbers from 1 to n
Sample Input 0
7
Sample Output 0
1357

5. Palindrome number using while loop (control)


Program to check whether given number is palindrome or not
Input Format
Integer as an input
Constraints
Input > 0
Output Format
Yes (or) No
Sample Input 0
121
Sample Output 0
Yes
Sample Input 1
234
Sample Output 1
No

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

8. Number Series 1(numbers)


Find the nth number in the sequence:
1, 2, 4, 7, 11, 16,....
Input Format
n value entered by user
Constraints
Assume n is positive and less than 20.
Output Format
print nth number of the sequence
Sample Input 0
3
Sample Output 0
4
Sample Input 1
9
Sample Output 1
37

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

10. pattern 6(control)


Write a program to print the pattern based on n value as shown in sample input and output
Input Format
integer value n
Constraints
.
Output Format
refer sample input and output
Sample Input 0
5
Sample Output 0
56789
4567
345
23
1
Sample Input 1
3
Sample Output 1
345
23
1

You might also like