All Question paper with solution mean Bachelorexam.com

Unit 2 – python programming | CONDITIONALS AND LOOPS

Today’s topic is Important Question with Answer for Unit 2 – Python Programming | CONDITIONALS AND LOOPS. I hope this helps you prepare for your next Python programming test.

Dudes 🤔.. You want more useful details regarding this subject. Please keep in mind this as well.

Important Questions For Python Programming: 
*Unit-01     *Unit-02    
*Unit-03    *Unit-04 
*Unit-05    *Short-Q/Ans
*Question-Paper with solution 21-22 

Q1. Explain elif statements in Python.
OR
Explain all the conditional statement in Python using small code example?

Ans. Different types of conditional statement are:

1. If statement: 

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

2. With an if clause, a condition is provided; if the condition is true then the block of statement written in the if clause will be executed, otherwise not.

3. Syntax:

If (Boolean expression): Block of code #Set of statements to execute if the condition is true

2. If else statement:

1.An if statement can be followed by an optional else statement, which executes when the Boolean expression is False.

2. The else condition is used when we have to judge one statement on the

basis of other.

3. Syntax: If (Boolean expression): Block of code #Set of statements to execute – if condition is true

else:  Block of code #Set of statements to execute if condition is false

4. Working and execution:

  • The condition will be evaluated to a Boolean expression (true or false).
  • If the condition is true then the statements or program present inside the if block will be executed
  • If the condition is false then the statements or program present inside else block will be executed.

3.Nested-if statement:

  • Nested-if statements are nested inside other if statements. That is, a nested-if statement is the body of another if statement.
  • We use nested if statements when we need to check secondary conditions only if the fist condition executes as true.


4. Elif statement:

  1. Elif stands for else if in Python.
  2. We use elif statements when we need to check multiple conditions only if the given if condition executes as false.
  3. Working and execution:
    1. If the first if condition is true, the program will execute the body of the if statement. Otherwise, the program will go to the elif block (else if in Python) which basically checks for another if statement.
    2. Again, if the condition is true, the program will execute the body of the elif statement, and if the condition is found to be false, the program will go to the next else block and execute the body of the else block.
  4. Syntax

Q2. Explain expression evaluation and float representation with example. Write a Python program for how to check if a given number is Fibonacci number.

Ans.  Expression evaluation:

  • In Python actions are performed in two forms:
    • Expression evaluation,
    • Statement execution.
  • The key difference between these two forms is that expression evaluation returns a value whereas statement execution does not return any value. 
  • A Python program contains one or more statements. A statement contains zero or more expressions.
  • Python executes a statement by evaluating its expressions to values one by one.
  • Python evaluates an expression by evaluating the sub-expressions and substituting their values.
  • For example:
  • An expression is not always a mathematical expression in Python. A value by itself is a simple expression, and so is a variable.
  • In the given example, we assigned a value “Hello Python” to the variable program. Now, when we type only program, we get the output ‘Hello Python’. This is the term we typed when we assigned a value to the variable. When we use a print statement with program it gives the value of the variable i.e., the value after removing quotes.

Float representation: 

  1. Floating point representations vary from machine to machine.
  2. The float type in Python represents the floating-point number. 
  3. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts.
  4. For example: 97.98, 32.3+e18, -32.54e100 all are floating point numbers. 
  5. Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 × 10308
  6. Any number greater than this will be indicated by the string inf in Python.
  7. Floating-point numbers are represented in computer hardware as base 2 (binary) fractions.
  8. For example, the decimal fraction 0.125 has value 1/10+2/100+ 5/1000, and in the same way the binary fraction 0.001 has value 0/2 + 0/4 + 1/8. 
  9. For example: # Python code to demonstrate float values.

Program:


Q3. Explain the purpose and working of loops. Discuss break and continue with examples. Write a Python program to convert time from 12 hour to 24-hour format.

Ans.

Purpose and working of loops :

  • A loop is a programming structure that repeats a sequence of instructions until a specific condition is met.
  • A loop statement allows us to execute a statement or group of statements multiple times.
    • a. For
    • b. While
    • C.Nested
  • Python programming language provides following types of loops to handle looping requirements:
  • Purpose: 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 the number of times could be dictated by a certain condition being met.
  • Working: Consider the flow chart for a loop execution:
    • In the flow chart if the test condition is true, then the loop is executed, and if it is false then the execution breaks out of the loop.
    • After the loop is successfully executed the execution again starts from the loop entry and again checks for the test condition, and this keeps on repeating until the condition is false.

Break statement: 

  • The break keyword terminates the loop and transfers the control to the end of the loop.
  • While loops, for loops can also be prematurely terminated using the break statement.
  • The break statement exits from the loop and transfers the execution from the loop to the statement that is immediately following the loop. 
  • For example:

Continue statement: 

  • The continue statement causes execution to immediately continue at the start of the loop, it skips the execution of the remaining body part of the loop.
  • The continue keyword terminates the ongoing iteration and transfers the control to the top of the loop and the loop condition is evaluated again. If the condition is true, then the next iteration takes place. 
  • Just as with while loops, the continue statement can also be used in Python for loops
  • For Example:

Output

  • 2
  • 4
  • 6
  • 8

Program:


Q4. Differentiate between for and while loop.

Ans.

PropertiesForWhile
FormatInitialization, condition checking, iteration statements are written at the top of the loop.Only initialization and condition checking is done at top of the loop
UseThe ‘For’ loop is used only when we already knew the number of iterationsThe ‘while’ loop is used only when the number of iterations are ot exactly known.
ConditionIf the condition is not given in the ‘for’ loop then the loop iterates infinite times.If the condition is not given in the ‘while’ loop, it provides compilation error.
InitializationIn ‘for’ loop the initialization once done is never repeated.In the ‘while’ loop if initialization is done during condition checking, then initialization is done each time the loop iterates.

Q5. Write a python program to print the numbers for a user provided range.

Ans.


bachelor exam preparation all question paper with solution important questions with solution

Python Programming Important Links:

LabelLink
Subject SyllabusSyllabus
Short QuestionsShort-question
Important Unit-1Unit-1
Important Unit-2Unit-2
Important Unit-3Unit-3
Important Unit-4Unit-4
Important Unit-5Unit-5
Question paper – 2021-222021-22

AKTU Important Links | Btech Syllabus

Link NameLinks
Btech AKTU CircularsLinks
Btech AKTU SyllabusLinks
Btech AKTU Student DashboardStudent Dashboard
AKTU RESULT (One VIew)Student Result

Important Links-Btech (AKTU) | Python Programming syllabus

LabelLinks
Btech InformationInfo Link
Btech CSECSE-LINK
Quantum-PageLink
Python Programming SyllabusPython Programming Syllabus