Unit 2 Python
Unit 2 Python
Conditional statement
AKTU PYQs
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
conditional statement
A conditional statement as the name suggests itself, is used to handle conditions in your program. These
statements guide the program while making decisions based on the conditions encountered by the
program., we will explore three conditional statements in Python: if statement, if-else statements, if-elif-
else ladder.
Indentation in Python
For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for
the block level code. In Python, indentation is used to declare a block. If two statements are at the
same indentation level, then they are the part of the same block.
Generally, four spaces are given to indent the statements which are a typical amount of indentation in
python
Indentation is the most used part of the python language since it declares the block of code. All the
statements of one block are intended at the same level indentation. We will see how the actual
indentation takes place in decision making and other stuff in python.
The if statement
The if statement is used to test a particular condition and if the condition is true, it executes a block of
code known as if-block. The condition of if statement can be any valid logical expression which can be
either evaluated to true or false.
Syntax
if expression:
statement
The if statement(flowchart)
Example
output
enter the number:5
hello
hi
how are you?
bye
Write a program to find out the largest number among three
output
Enter a: 6
Enter b: 7
Enter c: 5
7 is the largest number
If-else statement
The if-else statement provides an else block combined with the if statement which is executed in the false
case of the condition.
if the condition is true, then the if-block is executed. Otherwise, the else-block is executed.
Syntax
if condition:
#block of statements
else:
#another block of statements (else-block)
If-else statement
Write a program to check whether you are eligible to vote or not
Control statement
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
The elif statement
The elif statement enables us to check multiple conditions and execute the specific block of statements
depending upon the true condition among them. We can have any number of elif statements in our
program depending upon our need. However, using elif is optional.
The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.
The syntax of the elif statement is given below.
The elif statement(syntax)
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
The elif statement(flowchart)
Example1
output
Enter the number?6
The given number is not equal to 10, 50 or 100
Example2
output
Enter the marks? 70
You scored grade B + ...
Write a program to Convert month name to a number of days
print("List of months: January, February, March, April, May, June, July, August, September, October,
November, December")
month_name = input("Input the name of Month: ")
if month_name == "February":
print("No. of days: 28/29 days")
elif month_name in ("April", "June", "September", "November"):
print("No. of days: 30 days")
elif month_name in ("January", "March", "May", "July", "August", "October", "December"):
print("No. of days: 31 day")
else:
print("Wrong month name")
Write a program to Convert month name to a number of days
output
List of months: January, February, March, April, May, June, July, August, September, October, November,
December
Input the name of Month: April
No. of days: 30 days
PYTHON PROGRAMMING
AKTU PYQs
Write approgram to input a character and check whether a character is vowel and consonants
Write approgram to input a character and check whether a character is vowel and consonants
output
Write approgram to check whether a character is vowel and consonants or digit and special
symbol
Write approgram to check whether a character is vowel and consonants or digit and special
symbol
output
Write approgram to check whether a character is alphabet (uppercase and lower case and non-
alphabet symbol
Write approgram to check whether a character is alphabet ,non-alphabet and non-alphabet
symbol
p
Write a program to enter the angle and check which type of triangle is
p
Write a program to enter the angle and check which type of triangle is
p
Write a program to enter the side and check which type of triangle is
p
Write a program to enter the side and check which type of triangle is
p
Write a program to find out the largest number among three
p
Write a program to find out the largest number among three
PYTHON PROGRAMMING
Leap year
Largest number among three using
nested –if By PRAGYA RAJVANSHI
Quadratic equation B.Tech, M.Tech( C.S.E)
output
Write approgram to check whether year is leap year or not
Write approgram to check whether year is leap year or not
output
Writep a program to find out the roots of quadratic equation
Writep a program to find out the roots of quadratic equation
p
write a program to find the root of quadratic equation
Writep a program to make calculator using elif statement
Writep a program to make calculator using elif statement
Writep a program to make calculator using elif statement
PYTHON PROGRAMMING
For developers coming from languages like C/C++ or Java know that there was a conditional statement
known as Switch Case. This Match-Case is the Switch Case of Python which was introduced in Python
3.10. Here we have to first pass a parameter then try to check with which case the parameter is getting
satisfied. If we find a match we will do something and if there is no match at all we will do something
else
p
MATCH CASE
parameter = "Geeksforgeeks"
match parameter:
case first :
do_something(first)
case second :
do_something(second)
p
MATCH CASE
case third :
do_something(third)
.............
............
case n :
do_something(n)
case _ :
nothing_matched_function()
p
Write to print the day in a week using match case
p
Write to print the day in a week using match case
p
Write a program to print the number of days in a week
p
Write a program to check whether is a vowel or consonants
p
Write a program to check whether is a vowel or consonants
p
Write a program to check whether is a vowel or consonants
p
Write to print the number of month in a year
p
Write to print the number of month in a year
p
Write to print the number of month in a year
p
Write a program to make calculator using match case
p
Write a program to make calculator using match case
p
Write a program to make calculator using match case
PYTHON PROGRAMMING
While loop
Loops program
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
p LOOPS
A loop is a programming structure that repeats a sequence of instructions until a specific condition
is satisfied
A loop statement allows us to execute a statement or group of statement multiple times
Python programming language provides following types of loops to handle looping requirement:
While( entry controlled loop)
For
nested
p while loop
In python, a while loop is used to execute a block of statements repeatedly until a given condition
is satisfied. And when the condition becomes false, the line immediately after the loop in the
program is executed.
Syntax
While expression:
statement
All the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its
method of grouping statements.
p while loop(flowchart)
p while Loop
writep a program to print first n natural number
writep a program to print the sum of first n natural number
writep a program to reverse a n digit number
writep a program to count the number of digit in a n digit number
writep a program to count the number of digit in a n digit number
writep a program to check whether a number is palindrome or not
writep a program to check whether a number is Armstrong or not
PYTHON PROGRAMMING
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
writep a program to print first n natural number
p
p
writep a program to print the sum of first n natural number
p
p
writep a program to reverse a n digit number
writep a program to reverse a n digit number
writep a program to reverse a n digit number
writep a program to check whether a number is palindrome or not
p
p
writep a program to check whether a number is Armstrong or not
p
p
writep a program to count the number of digit in a n digit number
p
p
PYTHON PROGRAMMING
For loop
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
p For loop
Python For loop is used for sequential traversal i.e. it is used for iterating over an iterable like String, Tuple,
List, Set, or Dictionary
for var in iterable:
# statements
Here the iterable is a collection of objects like lists, and tuples. The indented statements inside the for loops
are executed once for each item in an iterable. The variable var takes the value of the next item of the
iterable each time through the loop.
p For loop
p For loop with list
p For loop with dictionary
p For loop with dictionary
p For loop with dictionary
p For loop with string
p Python For Loop with a step size
This code uses a for loop in conjunction with the range() function to generate a sequence of numbers
starting from 0, up to (but not including) 10, and with a step size of 2. For each number in the sequence, the
loop prints its value using the print() function. The output will show the numbers 0, 2, 4, 6, and 8.
p Range()
The Python range() function returns a sequence of numbers, in a given range. The most common use of
it is to iterate sequences on a sequence of numbers using Python loop
p Syntax of Python range() function
In simple terms, range() allows the user to generate a series of numbers within a given range. Depending
on how many arguments the user is passing to the function, the user can decide where that series of
numbers will begin and end, as well as how big the difference will be between one number and the
next. Python range() function takes can be initialized in 3 ways.
range (stop) takes one argument.
range (start, stop) takes two arguments.
range (start, stop, step) takes three arguments.
p Python range (stop)
When the user call range() with one argument, the user will get a series of numbers that starts at 0 and
includes every whole number up to, but not including, the number that the user has provided as the
stop.
p Python range (start, stop)
When the user call range() with two arguments, the user gets to decide not only where the series of
numbers stops but also where it starts, so the user doesn’t have to start at 0 all the time. Users can use
range() to generate a series of numbers from X to Y using range(X, Y).
p Python range (start, stop, step)
p Incrementing the Range using a Positive Step
p Python range() using Negative Step
p Python range() with Float Values
PYTHON PROGRAMMING
By PRAGYA RAJVANSHI
B.Tech, M.Tech( C.S.E)
Write approgram to print the pattern
Write approgram to print the pattern
Write approgram to print the pattern
Write approgram to print the pattern
Write approgram to print the pattern
Write approgram to print the pattern
Write approgram to the pattern
PYTHON PROGRAMMING
the break statement is used to terminate the loop immediately when it is encountered.
The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the
next iteration.
The syntax of the continue statement is:
Continue;
p continue statement
p continue statement
Write a pprogram to check whether a number is prime number or not
PYTHON PROGRAMMING
Pass statement
Star pattern
By PRAGYA RAJVANSHI
AKTU PYQS’
B.Tech, M.Tech( C.S.E)
p PASS STATEMENT
Python pass statement is a null statement. But the difference between pass and comment is that comment is
ignored by the interpreter whereas pass is not ignored.
SYNTAX
pass
What is pass statement in Python?
When the user does not know what code to write, So user simply places a pass at that line. Sometimes, the pass is
used when the user doesn’t want any code to execute. So users can simply place a pass where empty code is not
allowed, like in loops, function definitions, class definitions, or in if statements. So using a pass statement user
avoids this error.
p PASS STATEMENT
The purpose of loops is to repeat the same or similar code a number of times . This number of times could be
specified to a certain number or number of times to be dictated by certain condition being met
writepa program to print the prime number within an interval
p difference between
For loop is typically used for iterating over a fixed While loop is used for more complex control flow
sequence of items situations.
p difference between
A tuple is also a sequence of values just like a list. It is immutable and enclosed in parentheses instead of square
brackets
p for loop with list
p for loop with string
p for loop with else
you can also use else keyword in python for loop. This is useful when you want to execute some code
when the loop is finished.
The else block is executed when the loop is finished.
p AKTU QUESTIONS
Explain all the conditional statement in python using small code example
Q1 AKTU 2019-20
Q2 Write a program to check if the input year is a leap year or not AKTU 2021-22
AKTU 2021-22
Q3 Write a program to print the Fibonacci series in python
AKTU 2019-20
Explain the following loops with flowdaigram, synatz and with suitable
Q5 AKTU 2022-23
example
p AKTU QUESTIONS
Link in Description