Unit-2 Notes Python
Unit-2 Notes Python
Unit -2
Content
• Conditional Statements:
• if statement
• if-else statement
• Nested-if statement
• elif statements
• Purpose and working of loops
• While loop, for loop
• else with loop statement
• Selecting an appropriate loop
• Nested Loops
• break
• continue and pass statement.
Why use conditional statements? (CO2)
if statement
if else statement
if elif statement
Nested if else
if Statement (CO2)
Boolean
False
expression
(Condition)
True
Statements
Statement-x
Important points about if Statement (CO2)
Boolean
expression
True False
(Condition)
Statement-A Statement-B
Statement-x
if elif Statement (CO2)
Boolean True
Expression-1 statement-A
False
Boolean True
statement-B
Expression-2
False
Boolean True
statement-C
Expression-2
False
statement-D
statement-X
if elif Statement Example (CO2)
Boolean True
Statement-A
expression-1
False
Boolean True
expression-1
Statement-B
False
Statement-C
Statement-X
Nested if else Statement Example (CO2)
• if -3 will evaluate to -
a) True
b) False
MCQ s
while Expression:
Statements
while flowchart (CO2)
Entry
Test False
expression
True
Statements
(while loop body)
Exit loop
Program to find the sum of first n natural numbers (CO2)
Output:
Enter a number: 4
The sum is: 10
for in Loop (CO2)
• Here, value is the variable that takes the value of the item
inside the sequence on each iteration.
• Loop continues until we reach the last item in the sequence.
• The body of for loop is separated from the rest of the code
using indentation.
“for in” loop flowchart (CO2)
for each item
in sequence
No
Statements
(while loop body)
Exit loop
Program to find the sum of all numbers stored in a list (CO2)
Output:
The sum of numbers is: 140
range() function (CO2)
Output:
0
25
50
75
Nested Loops (CO2)
Output:
1
22
333
4444
Program to print all prime numbers from a list (CO2)
Output: 2 5 11 31
break statement (CO2)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
num_sum = 0
count = 0
for x in numbers:
num_sum = num_sum + x
count = count + 1
if count == 5:
break
print("Sum of first ", count, "integers is: ", num_sum)
Output:
Sum of first 5 integers is: 15
continue statement (CO2)
for x in range(7):
if x == 3 or x == 6:
continue
print(x)
Output:
0
1
2
4
5
Python pass statement (CO2)
Output:
Number is: 5
Expression Evaluation (CO2)
a = 20
b = 10
c =a+b–5
print(c)
Output:
25
Float Representation in Python (CO2)
print(1.7e308)
# greater than 1.8 * 10^308 will
print 'inf'
print(1.82e308)
Float Representation in Python (CO2)
b = 3.5
d = b.as_integer_ratio()
print(d[0], "/", d[1]) Output:
7/2
Float Representation in Python (CO2)
print((-5.0).is_integer())
print((4.8).is_integer())
print(float.is_integer(275.0))
Output:
True
False
True
Float Representation in Python (CO2)
a = float.hex(35.0)
print(a)
Output:
0x1.1800000000000p+5
Daily Quiz
e.***** f. ***** g. h.
********** *********
**** **** * * *
*** *** * * *
** ** * *
* * **
* * ********** **
*
MCQ s
• Explain the purpose and working of loops. Discuss break and continue with
example. Write a python program to convert time from 12-hour format to
24-hour format. [AKTU 2019-20 (Odd) Marks-10]
• Write Python code to find the factorial of a number.
• Write Python program to convert uppercase letter to lowercase and vice
versaExplain the purpose and working of loops. Discuss Break and continue.
Write Python Programs to print following patterns.
1 *
010 ***
10101 *****
0101010 *******
Expected Questions for University Exam