Chapter 4 Class 8th – Loops in Python

Python History

  • Python was created by Guido van Rossum in 1989 at CWI (Centrum Wiskunde & Informatica) in Netherlands.

  • The first version of Python, Python 1.0, was released in 1991.

  • Guido wanted a language that was simple, easy to read, and powerful.

  • The name “Python” came from Guido’s favorite TV show Monty Python’s Flying Circus, not from the snake.

  • Later, improved versions were released:

    • Python 2.0 in 2000

    • Python 3.0 in 2008

  • Python 3 became the main version used worldwide because it is more modern and supports better features.


In short:
Python was created by Guido van Rossum in 1989 to make programming simple and readable for everyone.

 Python:

Python is a simple and powerful programming language used to give instructions to the computer.


Python Program:
A Python program is a set of instructions written in Python to perform a specific task.


String:
A string is a collection of characters written inside single (' ') or double (" ") quotes. Strings are immutable.


List:
A list is a collection of items written inside square brackets [ ]. Lists are mutable.


Tuple:
A tuple is a collection of items written inside round brackets ( ). Tuples are immutable.


Mutable:
Mutable means something that can be changed after creation.


Immutable:
Immutable means something that cannot be changed after creation.

Chapter 8 – Loops in Python

Recalling Conditional Statements

In Python, a condition (शर्त) is a statement that can be either true (सही) or false (गलत).
Conditional statements help to check a condition and run only that part of the program which meets the condition.

Types of Conditional Statements

StatementPurpose (उद्देश्य)
ifTests a single condition and runs the code only if the condition is true.
if…elseRuns one block of code if the condition is true, and another block if it is false.
if…elif…elseChecks many conditions one by one and runs the correct block when a condition is true.

Example Program – Checking Grades

marks = int(input("Enter your percentage marks: ")) if marks >= 85 and marks <= 100: print("You scored grade A+") elif marks >= 60 and marks < 85: print("You scored grade A") elif marks >= 40 and marks < 60: print("You scored grade B") else: print("Sorry, you are fail")

Explanation (व्याख्या):

  • The program asks the user to enter marks.

  • It checks the range of marks using if, elif, and else conditions.

  • Depending on the marks, it prints the correct grade.


What are Loops

Sometimes in programming, we need to repeat (दोहराना) a set of statements many times.
For this, we use loops.
A loop is an arrangement that repeats some steps until a given condition becomes false (गलत).

Example

If your teacher says “Write ‘I will study daily’ 10 times,”
instead of writing it 10 times manually, a loop can do it automatically.


Concept of a Counter

A counter (गिनने वाला variable) keeps track of how many times something happens in a loop.

Syntax:

CT = CT + 1

or

CT = CT - 1

Example

counter = 1 while counter <= 5: print("Hello") counter = counter + 1

Output:

Hello Hello Hello Hello Hello

Explanation:
The variable counter starts from 1 and increases by 1 after every print until it reaches 5.


Iteration or Looping

Iteration (दोहराव) means repeating the same steps again and again until a condition is true.

To make a loop, we define three values:

  1. Start value (प्रारंभिक मान) – where the loop begins.

  2. Test condition (जाँच की शर्त) – the condition that must be true to continue.

  3. Increment or Decrement value (बढ़ाना या घटाना) – how much the value changes each time.


While Loop Syntax

i = 0 while i < 10: print(i) i = i + 1

Output:

0 1 2 3 4 5 6 7 8 9

Explanation:
The loop starts from 0 and continues printing numbers until i becomes 10.


More While Loop Programs

Program 1 – Reverse Order (10 to 1)

i = 10 while i > 0: print(i) i = i - 1

Output:

10 9 8 7 6 5 4 3 2 1

Program 2 – Print Name Five Times

name = input("Enter your name: ") i = 1 while i <= 5: print("Hello", name) i = i + 1

Output:

Enter your name: Ajeet Hello Ajeet Hello Ajeet Hello Ajeet Hello Ajeet Hello Ajeet

For Loop

The for loop is used to repeat (दोहराना) a set of instructions for a fixed number of times.

Syntax:

for variable in range(start, stop, step): loop body

Example

for i in range(1, 6): print(i)

Output:

1 2 3 4 5

Membership Operators

Membership operators are used to test whether a value is part of a list or not.

OperatorPurpose
inChecks if a value exists in a list.
not inChecks if a value does not exist in a list.

Example

print(20 in [10, 20, 30]) # True print(50 not in [10, 20, 30]) # True

Example Program – Using ‘in’ Operator

for i in [5, 10, 15, 20, 25]: print(i)

Output:

5 10 15 20 25

The range() Function

The range() function helps to create a list of numbers in order.
It is often used with the for loop.

Syntax:

range(start, stop, step)

Meaning of Parameters

  1. Start value (शुरुआती मान) – where the list begins.

  2. Stop value (अंत मान) – where the list ends (not included).

  3. Step value (अंतर) – the difference between two numbers.

Example

for i in range(0, 10, 2): print(i)

Output:

0 2 4 6 8

Program – Printing Odd and Even Numbers

for i in range(1, 100, 2): # odd numbers print(i) for i in range(2, 101, 2): # even numbers print(i)

Program – Fibonacci Series

A Fibonacci Series (फिबोनाची श्रेणी) is a sequence where each number is the sum of the previous two numbers.

n = int(input("Enter number of terms: ")) a = 0 b = 1 print(a) print(b) for i in range(1, n+1): c = a + b print(c) a = b b = c

Output:

0 1 1 2 3 5 8 13 21 34

Explanation:
The program starts with 0 and 1, then keeps adding the previous two numbers to get the next one.


Applying Condition with for Loop

We can use if or if…else statements inside a for loop to apply conditions.

Syntax:

for variable in range(start, stop, step): if condition: statement

Using break Statement

The break  statement is used to stop the loop before it finishes completely.

Example

n = int(input("Enter a number: ")) for i in range(0, n+1): if n % 2 == 0: print("Even Number") break

Output:

Enter a number: 8 Even Number

New Terms

TermMeaning
Iteration or LoopingRepeating a set of instructions many times.
Iterative StatementsCommands used to create loops like while and for.
Counter VariableA variable that keeps count of how many times a loop runs.
Membership OperatorsOperators such as in and not in that check if a value exists in a list.

Post a Comment