Loops: While and For Loops

  Question: Suppose Mohan's Mother is working in a factory and she got the task from her boss to sort the list of the members Started with M and N. Write the program which print\show "inserted in list1" when name starts with M and show "inserted in list 2" when name started with N. 

Program Note:

Heads up, friends!
The question mentioned earlier includes the use of for and while loops — concepts we’re just about to explore in today’s blog.
But don’t worry! By the end of this post, you’ll understand loops well enough to solve it confidently.
Also, I’ll update the previous blog to better fit the learning flow — thanks for your patience and sorry for the inconvenience!

Loops in python: 



For Loop:

The for loop is used to iterate over a sequence (like a list or range).

Syntax:
                

Example: for i in range(5):
                       print(i)

Now let us talk about the while loop

While Loop:

Syntax:




Example: count = 1
                while count <= 5:
                    print(count)
                    count += 1
 
Now we take some of the Examples of these loops:

Question: You are Raghav of Vidhya Pati School,Delhi and your teacher asked you to draw the pattern given pattern in python. So write the code to do so.

pattern:
54321
4321
321
21
1

programme:

for i in range(1,6):
    for j in range(5,i-1,-1):
        print(j,end="")
    print()

Before Moving to the next Question, lets talk about ASCII CODE and ORDINARY Number.

Ordinary number: Every ASCII Codes Like "A","B"etc are know as Ordinary number. Each ASCII codes have the unique ordinary number.

For converting Ordinary number to the charactor belong to that number we use the function chr() and to convert character to their respected ordinary number we use the function named ord().

Now we will take one Question.

Question: Print the following pattern.

pattern:

           EDCBA  
           EDCB  
           EDC  
           ED  
           E  

Programme: 

for i in range(65,70):
    for j in range(69,i-1,-1):
        print(chr(j),end="")
    print()           

before arriving to the next question lets understand some more concept.

Split(): This function is applicable to String datatype and this function break the string and store each word in the list.

Used as let the string S then S.split()

Example: 

S='Ram is the best part of my life'
m=S.split()
print(m)
 
Output:["Ram","is","the","best","part","of","my","life"]

Append(): This function is used to add one list, string, number etc in the defined string. In this string you can only pass one argument

Syntax: 

n=252
L=[]
L.append(n)
print(L)                            

Output:[252]

Question: You are Amit and your sir ask you to take the input from the user and print the words that start with letter enter my the user in the list.

Example: String entered: Ram is the best person in the ramoji hotel.
                 Output: ["Ram","Ramoji"]

Programme:

s=input("enter the string")
p=input("enter the letter on which you have to find")
L=[]
m=s.split()
for i in m:
    if i[0]==p:
        L.append(i)
print(L)

Now you can try the above question which by mistake i had given in previous Blog.

Q1. Print the following pattern:

FGHIJ
GHIJ
HIJ
IJ
J

Q2. Print the following pattern:

54321
 4321
  321
   21
    1

Q3. Print the following pattern:

54321
4321
321
21
1

Q4. Print the following pattern:

1
12
123
1234
12345

Q5. Print the following pattern:

1
21
321
4321
54321

Q6. Print the following pattern:

5
54
543
5432
54321

 7. Write the following pattern: 

EDCBA
 DCBA
  CBA
   BA
    A

8. Write the following pattern:

     E
   DE
  CDE
 BCDE
ABCDE

9. Write the following pattern: 

    A
   BA
  CBA
 DCBA
EDCBA

10. Write the following pattern: 

ABCDE
 BCDE
  CDE
   DE
    E
Now take some more questions.

Question11: Write the code to print the first n prime number.
Question12: Write the programme to print the fibonacci Series.
Question13: Write the programme to print the Geometric progression.
Question14: Write the programme to print that the string entered by the user is palindrome or not.

Now Lets discuss the Homework Question of the previous Blog and I will provide for question regarding the pattern and Loops.

Question2: Suppose your father is the head of the labour factory and He wants to sort the name of the labour on the basis of their wadges. Wether they are rich or poor. So, write the programme to help him.  
Programme:
wadge=int(input("enter the wadge of the worker"))
if wadge>=1000:
    print("The worker is rich!!")
else:
    print("the worker is not rich!!")

Question3: Suppose you are Reema head Girl of the class and your class teacher asked you to sort the student in the category of height as shortest , short , tall and tallest. So, create a programme for helping your teacher to short the students.

Programme: 

h=int(input("enter the height of the student"))
if h>170:
    print("The boy is tallest")
elif h>150 and h<170:
    print("The boy is short")
else:
    print("the boy is shortest")

✅ Conclusion

That's all for today's loops lesson, my friends! 🚀
I hope you now clearly understand how for and while loops work, along with how to create various patterns and solve real-life problems using loops. Make sure to practice the homework questions I’ve shared — they will help you master these concepts even better.

👉 If you’re still building your foundation, you can first check my previous blog on Python Basics – A Beginner's Guide 🐍, which covers all the basic concepts step-by-step.

In the upcoming blogs, we’ll continue learning more exciting topics in Python, so stay tuned and keep coding consistently. 💻✨

As always — Practice makes coding perfect! 💪 

Thank you!!!


Post a Comment

2 Comments