Python Notes (KNC-402) UNIT-2

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

UNIT 2

Conditional Statement in Python

Decision Making

Decision-making is the anticipation of conditions occurring during the execution of a


program and specified actions taken according to the conditions.
Decision structures evaluate multiple expressions, which produce TRUE or FALSE as
the outcome. You need to determine which action to take and which statements to
execute if the outcome is TRUE or FALSE otherwise.
Python programming language assumes any non-zero and non-null values as TRUE,
and any zero or null values as FALSE value.
Python programming language provides the following types of decision-making
statements.

S.No. Statement & Description

if statements
1 An if statement consists of a boolean expression followed by one or
more statements.

if...else statements
2 An if statement can be followed by an optional else statement, which
executes when the boolean expression is FALSE.

nested if statements
3 You can use one if or else if statement inside another if or else if
statement(s).

1|P ag e Python Programming (KNC-402)


if Statement

if-else Statement

2|P ag e Python Programming (KNC-402)


Nested if Statements (if with in if)
If-elif-else Statement

Programs based on decision making----


1. Program to check whether a given number is even or odd.
Check even / odd using modulus operator:

1. #Even Odd Program using Modulus Operator


2. a=int(input("Please Enter a Number : "));
3. if(a%2==0):
4. print("This Number is Even")
5. else:
6. print("This Number is Odd")

Check even / odd using bitwise operator:

1. #Even Odd Program using Bitwise Operator


2. a=int(input("Please Enter a Number : "));
3. if(a&1==1):
4. print("This Number is Odd")
5. else:
6. print("This Number is Even")

3|P ag e Python Programming (KNC-402)


Check Even / Odd without using modulus or bitwise operator:

1. #Even Odd Program using Modulus Operator


2. number=int(input("Please Enter a Number : "));
3. x=int(number/2)*2;
4. if(x==number):
5. print("This Number is Even")
6. else:
7. print("This Number is Odd")

Program to find largest among two numbers.

1. num1=int(input("Enter your first number:"))


2. num2=int(input("Enter your second number: "))
3. if(num1>num2):
4. print(num1+"is greatest")
5. elif(num2>num1):
6. print(num2+ “is greatest")
7. else:
8. print(num1+ “and” num2+ “are equal")
9.

Program to find largest among three numbers.

1. num1,num2,num3=map(int,input("Enter three numbers:").split(" "))


2. if(num1>num2 and num1>num3):
3. print("{} is greatest".format(num1))
4. elif (num2>num3):
5. print("{} is greatest".format(num2))
6. else:
7. print("{} is greatest".format(num3))

Program to calculate the electricity bill as per below information-

Input Customer ID
Input Last Meter Reading(LMR)
Input Current Meter Reading(CMR)
Calculate the bill as per given unit consumption—
For first 150 units, charges are Rs. 3.0 per unit
For units >150 and <=300, charges are Rs. 4.5 per unit
For units>300 and <=500, charges are Rs. 6.0 per units
For Units >500, charges are Rs. 8.0 per unit.

4|P ag e Python Programming (KNC-402)


units = int(input(" Please enter Number of Units you Consumed : "))
if(units <= 150):
amount = units * 3.00
elif(units <= 300):
amount = (150*3.00+(units - 150) * 4.50)
elif(units <= 500):
amount = 150*3.00 + 150*4.50 + ((units - 500) * 6.00)
else:
amount = 150*3.00 + 150*4.50 +200*6.00+ ((units - 500) * 8.00)
print("\nElectricity Bill = %.2f" %amount)

Loops
A loop statement allows us to execute a statement or group of statements multiple times.

Python programming language provides the following types of loops to handle looping
requirements.

while Loop

5|P ag e Python Programming (KNC-402)


for Loop

For loop provides a mechanism to repeat a task until a particular condition is True. It is usually
known as a determinate or definite loop because the programmer knows exactly how many
times the loop will repeat. The for...in statement is a looping statement used in Python to iterate
over a sequence of objects.

For Loop and Range() Function

The range() function is a built-in function in Python that is used to iterate over a sequence of
numbers. The syntax of range() is range(beg, end, [step])

The range() produces a sequence of numbers starting with beg (inclusive) and ending with one
less than the number end. The step argument is option (that is why it is placed in brackets). By
default, every number in the range is incremented by 1 but we can specify a different increment
using step. It can be both negative and positive, but not zero.

If range() function is given a single argument, it produces an object with values from 0 to
argument-1. For example: range(10) is equal to writing range(0, 10).

• If range() is called with two arguments, it produces values from the first to the second. For
example, range(0,10).

• If range() has three arguments then the third argument specifies the interval of the sequence
produced. In this case, the third argument must be an integer. For example, range(1,20,3).

6|P ag e Python Programming (KNC-402)


Nested Loops

 Python allows its users to have nested loops, that is, loops that can be placed inside other
loops. Although this feature will work with any loop like while loop as well as for loop.
 A for loop can be used to control the number of times a particular set of statements will
be executed. Another outer loop could be used to control the number of times that a
whole loop is repeated.
 Loops should be properly indented to identify which statements are contained within
each for statement.

The Break Statement

The break statement is used to terminate the execution of the nearest enclosing loop in which it
appears. The break statement is widely used with for loop and while loop. When interpreter
encounters a break statement, the control passes to the statement that follows the loop in which the
break statement appears.

7|P ag e Python Programming (KNC-402)


The Continue Statement

Like the break statement, the continue statement can only appear in the body of a loop. When the
compiler encounters a continue statement then the rest of the statements in the loop are skipped and
the control is unconditionally transferred to the loop-continuation portion of the nearest enclosing
loop.

Programs based on loops----

Write a program to sum the digits of a number.


1. n=int(input("Enter a number:"))
2. tot=0
3. while(n>0):
4. dig=n%10
5. tot=tot+dig
6. n=n//10
7. print("The total sum of digits is:",tot)

Write a program to reverse a number and check whether it is palindrome or


not.
1. n=int(input("Enter number:"))
2. temp=n
3. rev=0
4. while(n>0):
5. dig=n%10
6. rev=rev*10+dig
7. n=n//10
8. if(temp==rev):
9. print("The number is a palindrome!")
10. else:
11. print("The number isn't a palindrome!")

Write a program to check whether a given number is armstrong or not.

1. num = int(input("Enter a number: "))


2.
3. sum = 0
4.
5. temp = num
6. while temp > 0:
7. digit = temp % 10
8|P ag e Python Programming (KNC-402)
8. sum += digit ** 3
9. temp //= 10
10. if num == sum:
11. print(num,"is an Armstrong number")
12. else:
13. print(num,"is not an Armstrong number")

Write a program to calculate factorial of a given number.


n=int(input("Enter number:"))
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)

Write a program to print Fibonacci series.

a=int(input("Enter the first number of the series "))


b=int(input("Enter the second number of the series "))
n=int(input("Enter the number of terms needed "))
print(a,b,end=" ")
while(n-2):
c=a+b
a=b
b=c
print(c,end=" ")
n=n-1

9|P ag e Python Programming (KNC-402)


Write a program to find prime numbers from 1 to 100.

for Number in range (1, 101):


count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break

if (count == 0 and Number != 1):


print(" %d" %Number, end = ' ')

10 | P a g e Python Programming (KNC-402)

You might also like