Class 11 CBSE - Python

Python is designed to understand the user’s responsibility, the protocol, and the ethical issues related to the field of computing.
Python in class 11 for cbse is very handy when it comes to practice on computers but for the beginers there might be some errors and problems such as exception. But it's always advisable to practice program on computer than writing it on paper because it cherishes your practical skills , knowledge and typing speed and you'll come to know new things and if you want to be a software developer someday then its a must call for you.

Conditional & Looping Constructs


Decision Making Statements




In Python, there are three forms of decision making statement.

  • if Statement

  • if else Statement

  • if elif else Statement

if Statement




The syntax of if statement in Python is:

if condition:
# body of if statement
                

The if statement evaluates condition.

(i) If condition is True, the code inside the body of if is executed.
(ii) If condition is False, the code inside the body of if is skipped.

Example :

if


Output :

No is positive.
Not in if block
                

if else Statement




The if else statement is used to execute a block of code among two alternatives. The syntax of if else statement is:

if condition:
# block of code if condition is True

else:
# block of code if condition is False
                

(i) If the condition is True
The code inside if is executed. The code inside else is skipped.

(ii) If the condition is False,
The code inside else is executed. The code inside if is skipped.

Example :

 if-else

Output :

Positive number
This statement is always executed
    

if elif else Statement




However, if we need to make a choice between more than two alternatives, then we use the if elif else statement. The syntax of the if elif else statement is:

if condition1:
      #block1

elif condition2:
      #block 2

else: 
      #block 3
                

Here,

(i) If condition1 is True, block1 is executed.
(ii) If condition1 is False, then condition2 is evaluated.
(iii) If condition2 is True, then block2 is executed.
(iv) If condition2 is False, then block3 is executed.

Example :

if elif else

Output :

Zero
This statement is always executed
    

Nested if Statement




We can also use an if statement inside of an if statement. This is known as a nested if statement. The syntax of nested if statement is:

# outer if statement
if condition1:
    # statement(s)

    # inner if statement
    if condition2: 
        # statement(s)
                

(i) We can add else and elif statements to the inner if statement as required.
(ii) We can also insert inner if statement inside the outer else or elif statements(if they exist)
(iii) We can use multiple layers of if statements.

Example :

 nested if

Output :

Number is positive
    

While loops




Python while loop is used to run a specific code until a certain condition is met. The syntax of while loop is:

while condition:
# body of while loop
                

Here,

(i) A while loop evaluates the condition
(ii) If the condition is True, the code inside the while loop is executed.
(iii) This process continues until the condition is False.
(iv) When condition is False, the loop stops.

for loop




In Python, the for loop is used to run a block of code for a certain number of times.

The syntax of the for loop is:

for variable in sequence-items:
# statement(s)
    

Here, val accesses each item of sequence on each iteration. Loop continues until we reach the last item in the sequence.

Example (loop over Python list) :

for

Output :

Java
Python
C++
JavaScript
     

In the above example, we have created a list called languages. Initially, the value of language is set to the first element of the list, i.e. Java, so the print statement inside the loop is executed. language is updated with the next element of the list and the print statement is executed again. This way the loop runs until the last element of list is accessed.

for loop with range()




A range is a series of values between two numeric intervals.

We use Python's built-in function range() to define a range of values. For example,

values = range(4)
                

Here, 4 inside range() defines a range containing values 0, 1, 2, 3.

In Python, we can use for loop to iterate over a range.

Example (using range function) :

for loop with range()

Output :

0
1
2
3
     


break Statement




The break statement terminates the current loop i.e., the loop in which it appears and resumes execution at the next statement immediately after the end of the loop.

break statement

Output :

Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good Bye loop terminates
     


continue Statement




The continue statement in Python returns the control to the beginning of the loop. The continue statement escape all the statements below it and moves the control back to the beginning of the loop.

continue statement

Output :

9
8
7
6
4
3
2
1
0
While loop is ended
     


pass Statement




The pass statement in Python is used when a statement is required grammatically but you do not want any command or code to execute.

pass statement

Output :

Current character : P
Current character : y
Current character : t
This is pass block
Current character : h
Current character : o
Current character : n
Good bye!
     


Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com