Skip to main content

ads1

Class 11th Chapter - 5 Flow of Control

 Class 11th Chapter - 5  Flow of Control


TERM 
Block :- A group of consecutive statements having some indentation level
Body :- The block of statements in a compound statement that follows the header.
Empty statement :- A statement that appears in the code but does nothing 
Infinite Loop :- A loop that never ends. Endless loop:
Iteration Statement :- Statement that allows a set of instructions to be performed repeatedly. 
Looping Statement :- Iteration statement. Also called a loop.
Nesting :- Some program-construct within another of same type of construct 
Nested Loop :- A loop that contains another loop inside its body.
Suite :- Block

Answer the Following Question.

1. What is a statement ? What is the significance of an empty statement ? 

SOLUTION. A statement is an instruction given to the computer to perform any kind of action. An empty statement is useful in situations where the code requires a statement but logic does not. To fill these two requirements simultaneously, empty statement is used.Python offers pass statement as an empty statement.

2. Use the Python rangel) function to create the following list: [17, 3,-1, -5].
SOLUTION. range (7, -6, -4)

3.What is the purpose of range() function ? Given one example.

SOLUTION. The range() function generates an iterable sequence using the arguments start, stop and stop of range() function. It is very handy and useful for the "for loops, which require an iterable sequence for looping.

4. Write a note on Decision making in Python?
Solution :- 
Python If-else statements
Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.

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.

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.



The syntax of the if-statement is given below.

if expression:  
    statement  

Example 1
num = int(input("enter the number?"))  
if num%2 == 0:  
    print("Number is even")  

The 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.


The syntax of the if-else statement is given below.
if condition:  
    #block of statements   
else:   
    #another block of statements (else-block)   
Example 1 : Program to check whether a person is eligible to vote or not.
age = int (input("Enter your age? "))  
if age>=18:  
    print("You are eligible to vote !!") 
else:  
    print("Sorry! you have to wait !!")

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.
if expression 1:   
    # block of statements   
  
elif expression 2:   
    # block of statements   
  
elif expression 3:   
    # block of statements   
  
else:   
    # block of statements  
Example 1
number = int(input("Enter the number?"))  
if number==10:  
    print("number is equals to 10")  
elif number==50:  
    print("number is equal to 50");  
elif number==100:  
    print("number is equal to 100");  
else:  
    print("number is not equal to 10, 50 or 100");  

5. What is range()funtion in Python?
Solution :- The range() function

The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below.

Syntax:

range(start,stop,step size)  
The start represents the beginning of the iteration.
The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
Example-1: Program to print numbers in sequence.

for i in range(10):  
    print(i,end = ' ')  

6.Write a note on Python Loops?
Solution :- The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.

For this purpose, The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the following diagram to understand the working of a loop statement.

Python for loop
The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.

The syntax of for loop in python is given below.

for iterating_var in sequence:    
    statement(s)    
The for loop flowchart
For loop Using Sequence
Example-1: Iterating string using for loop

str = "Python"  
for i in str:  
    print(i)

Python While loop
The Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop.

It can be viewed as a repeating if statement. When we don't know the number of iterations then the while loop is most effective to use.

The syntax is given below.

while expression:    
    statements    
Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0.

Loop Control Statements
We can change the normal sequence of while loop's execution using the loop control statement. When the while loop's execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop.

1. Continue Statement - When the continue statement is encountered, the control transfer to the beginning of the loop. Let's understand the following example.

Example:

# prints all letters except 'a' and 't'   
i = 0  
str1 = 'javatpoint'  
  
while i < len(str1):   
    if str1[i] == 'a' or str1[i] == 't':   
        i += 1  
        continue  
    print('Current Letter :', a[i])   
    i += 1  

ads2

Popular posts from this blog

11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following:

    11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 : HRA = 25%, DA = 90% Basic Salary > 20000 : HRA = 30%, DA = 95% Static Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     if(basic <=10000)     {         da = basic * 0.8;         hra = basic *0.2;     }              else if(basic <=20000)     {         da = basic * 0.9;         hra = basic *0.25;     }     else     {         da = basic * 0.95;         hra = basic * 0.3;     }     gross = basic + da + hra;     System.out.println("The Gross Salary is :-"+gross);     } } Output :-  Dynamic Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     Scanner in = new Scanner(System.in);     System.out.println("Enter the Basic Salary

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise or class wise.

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise  or class wise. Solution :-   # x-axis is shows the subject and y -axis # shows the markers in each subject # import pandas and matplotlib  import pandas as pd  import matplotlib.pyplot as plt # Simple Line Chart with setting of Label of X and Y axis, # title for chart line and color of line  subject = ['Physic','Chemistry','Mathematics', 'Biology','Computer'] marks =[80,75,70,78,82] # To draw line in red colour plt.plot(subject,marks,'r',marker ='*')     # To Write Title of the Line Chart plt.title('Marks Scored') # To Put Label At Y Axis plt.xlabel('SUBJECT')           # To Put Label At X Axis plt.ylabel('MARKS')             plt.show() Output :- 

24.Create a Data Frame quarterly sales where each row contains the item category, item name, and expenditure. Group the rows by the category and print the total expenditure per category.

24.Create a Data Frame quarterly sales where each row contains the item category, item name, and expenditure. Group the rows by the category and print the total expenditure per category. import pandas as pd  # initialize list of lists data = [['CAR','Maruti',1000000],['AC','Hitachi',55000],['AIRCOLLER','Bajaj',12000], ['WASHING MACHINE','LG',15000],['CAR','Ford',7000000],['AC','SAMSUNG',45000],['AIRCOLLER','Symphony',20000],['WASHING MACHINE','Wirlpool',25000]] Col=['itemcat','itemname','expenditure'] # Create the pandas DataFrame qrtsales = pd.DataFrame(data,columns=Col) # print dataframe.  print (qrtsales) qs=qrtsales.groupby('itemcat')  print('Result after Filtering Dataframe')  print(qs['itemcat','expenditure'].sum()) Output :-