Job Saarnee

JOB SAARNEE

Top Most Platform for Job Updates O Level Results Answer keys AKTU MCQs

Long answer question of Python Programming from Unit 2

Unit 2 
Long answer question of Python Programming  


Q1 Explain all the Conditional statement in Python using small code example. . [ 2019-20]

and

Explain elif statement in Python.

Solution:

here are the different conditional statements in Python with code examples:

1. if statement:

The if statement is used to execute a block of code if a certain condition is met.

Example:

# check if a number is even or odd


number = 10


if number % 2 == 0:

    print(“The number is even”)

2. if-else statement:

The if-else statement is used to execute a block of code if a condition is met and another block of code if the condition is not met.

Example:

# check if a number is positive or negative

number = -5

if number >= 0:

    print(“The number is positive”)

else:

    print(“The number is negative”)


3. elif statement:

The elif statement allows you to check for multiple conditions and execute a block of code based on the first condition that is met.

Example:

# check if a number is positive, negative, or zero


number = 0


if number > 0:

    print(“The number is positive”)

elif number < 0:

    print(“The number is negative”)

else:

    print(“The number is zero”)


4. nested if statement:

The nested if statement allows you to check for a condition inside another condition.

Example:

# check if a number is positive and even

number = 4

if number > 0:

    if number % 2 == 0:

        print(“The number is positive and even”)

    else:

        print(“The number is positive but odd”)

else:

    print(“The number is not positive”)

These are the different conditional statements in Python.


Q2 Explain Expression Evaluation & Float Representation with example. Write a Python Program for How to check if a given number is Fibonacci number. [ 2019-20] [2021-22]

Solution:

1. Expression Evaluation:

In Python, expressions are made up of operators, operands, and function calls. Expression evaluation is the process of computing the value of an expression. Python uses an operator precedence rule to determine the order of evaluation for expressions with multiple operators.

Example:

# expression evaluation

x = 10

y = 5

z = 2

result = x + y / z

print(result)  # Output: 12.5


2. Float Representation:

In Python, floats are represented using the IEEE 754 standard. This standard uses a binary representation for floating-point numbers. A float is represented by a sign bit, an exponent, and a mantissa.

Example:

# float representation

num = 1.25

# convert float to binary

binary = format(struct.unpack(‘!I’, struct.pack(‘!f’, num))[0], ‘032b’)

print(binary)  # Output: 00111111101000000000000000000000

  • struct.pack(‘!f’, num) packs the floating-point number num into a binary string using the ‘!’ format specifier for network byte order and the ‘f’ type code for a single-precision float.
  • struct.unpack(‘!I’, …) unpacks the binary string from step 1 into a tuple containing a single unsigned integer using the ‘!’ format specifier for network byte order and the ‘I’ type code for a 4-byte unsigned integer.
  • [0] accesses the first element of the tuple from step 2.
  • format(…, ‘032b’) formats the unsigned integer from step 3 as a binary string with 32 bits, padded with leading zeros if necessary.

Python program to check if a given number is a Fibonacci number:

A Fibonacci series is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. To check if a given number is a Fibonacci number, we can use the following approach:

# check if a given number is a Fibonacci number

def is_fibonacci(n):

    a, b = 0, 1

    while b < n:

        a, b = b, a + b

    return b == n or n == 0

# test the function

print(is_fibonacci(8))   # Output: True

print(is_fibonacci(10))  # Output: False

In the above program, we first initialize two variables a and b to 0 and 1 respectively. We then use a while loop to compute the Fibonacci series until b is greater than or equal to the given number n. We return True if b is equal to n or if n is equal to 0, indicating that the number is a Fibonacci number. Otherwise, we return False.

Q3 Explain the purpose and working of loops. Discuss Break and continue with example. Write a Python program to convert time from 12 hour to 24-hour format. [ 2019-20]

Solution:

a. Loops in programming are used to execute a block of code repeatedly. They are used to automate repetitive tasks or to iterate through a collection of data.

There are two main types of loops in Python: for loops and while loops.

1. A for loop is used to iterate over a sequence (such as a list, tuple, or string) and execute a block of code for each item in the sequence. The syntax for a for loop is as follows:

for item in sequence:

    # code to execute for each item

2. A while loop is used to repeatedly execute a block of code as long as a certain condition is true. The syntax for a while loop is as follows:

while condition:

    # code to execute while condition is true


b. break and continue are two statements that are used to control the flow of a loop.

The break statement is used to exit a loop prematurely. When the break statement is encountered inside a loop, the loop immediately terminates and the program continues executing from the next statement after the loop.

Example:

# break example

numbers = [1, 2, 3, 4, 5]

for number in numbers:

    if number == 3:

        break

    print(number)

# Output: 1

#         2

In the above example, the loop terminates when the value 3 is encountered, and the program continues executing from the next statement after the loop.

The continue statement is used to skip the current iteration of a loop and continue with the next iteration. When the continue statement is encountered inside a loop, the current iteration is immediately stopped and the program continues with the next iteration.

Example:

# continue example

numbers = [1, 2, 3, 4, 5]

for number in numbers:

    if number == 3:

        continue

    print(number)

# Output: 1

#         2

#         4

#         5

In the above example, the value 3 is skipped during the iteration, and the program continues with the next iteration.


c. Python program to convert time from 12 hour to 24-hour format:

Here’s a simple Python program to convert time from 12-hour format to 24-hour format:


# Convert time from 12-hour to 24-hour format

time12 = input(“Enter time in 12-hour format (hh:mm:ss AM/PM): “)

# Extract the hour, minute, second, and meridian from the input string

hour, minute, second, meridian = time12[:-6].split(“:”) + time12[-6:].split()

# Convert the hour to 24-hour format

if meridian.upper() == “PM” and hour != “12”:

    hour = str(int(hour) + 12)

elif meridian.upper() == “AM” and hour == “12”:

    hour = “00”

# Output the time in 24-hour format

time24 = f”{hour}:{minute}:{second}”

print(“Time in 24-hour format:”, time24)


In this program, we prompt the user to enter the time in 12-hour format and extract the hour, minute, second, and meridian from the input string. We then convert the hour to 24-hour format using conditional statements to check whether the meridian is “PM” or “AM”. Finally, we output the time in 24-hour format using an f-string.


Note that this program assumes that the input time is in a valid 12-hour format and does not perform any error checking.

Q4 Write a python program to construct following pattern 


*

**

***

****

*****

****

***

**

* [2021-22]


Q5 What do you mean by if-else statement ? Explain with the help of example?

Q6 Write a program to find whether a number is even or odd?

Q7 Write a program to check the largest among the given three numbers?

Q8 Write a Python program to check if the input year is a leap year or not?

Q9 Write a Python program to display the Fibonacci sequence for n terms?

Q 10 Write a program to demonstrate while loop with else?


Q11 What will be the output after the following statements ?

x = 70

if x <= 30 or x >= 100:

     print(‘true’)

elif x <= 50 and x == 50:

     print(‘not true’)

elif x >= 150 or x <= 75:

     print(‘false’)

else:

     print(‘not false’)


Q12 What will be the output after the following statements ?

x = 25

if x > 10 and x < 15:

    print(‘true’)

elif x > 15 and x < 25:

    print(‘not true’)

elif x > 25 and x < 35:

    print(‘false’)

else:

    print(‘not false’)


You may Like to Read this 

PYTHON PROGRAMMING ALL STUDY MATERIAL || 99+ MOST IMPORTANT QUESTION WITH SOLUTION 

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart

You cannot copy content of this page