Day 15: A Closer Look at Python Control Statements

Archana PrustyArchana Prusty
3 min read

Introduction :

Welcome back to my Python journey! Yesterday, I laid the foundation with control structures in python.

Today, I dove into control statements, essential building blocks of any programming language. Let's explore what I learned!

Control Statement

break

continue

pass : it is a key word.

pass is a control statment.

pass is used to define empty body in python.

pass:

  • if True:

  • pass

  • no body

Loop Termination :

Loop can terminated in two different ways:

  1. Normal: if the loop is terminates with the head condition false

  2. AbNormal : if the loop is terminated with the head condition true. (using break)

Example :

for i in range (1, 10) :

if i == 8 :

break

print (i)

print ("out of loop")

Example :

for i in range (1, 10) :

if i == 6 :

i+=2

continue

print (i)

example:

from time import sleep

i = 1

while i <= 10 :

j =1

if i == 3 :

break

while j <= 5 :

if j == 3 :

break

print ("i am inner while loop")

j += 1

print ("i am while loop")

i +=1

print ("outside of while loop")

Example:

#print multiplication table from 1 to 8

from time import sleep

number = 1

while number <= 10:

if number > 8:

break

i=1

while i <=10:

print (i * number,end = ' ')

i += 1

number += 1

print ('\n')

sleep(1)

Example:

#print multiplication table from 1 to 10 but not print 5 table

from time import sleep

number = 1

while number <= 10:

if number==5:

number +=1

continue

if number > 8:

break

i=1

while i <=10:

print (i * number,end = ' ')

i += 1

number += 1

print ('\n')

sleep(1)

break :

  • break transfer the control from body to outside of the loop and terminate the loop.

continue:

  • continue control statement is used to avoid / skip the execution of loop body for a specific condition.

  • continue transfer the control from body to head position

Note:

break & continue is not allowed outside loop pass is allowed at any position

Example:

#Count the number of Digits of an int number given by user in runtime

number= int(input("enter a number"))

count = 0

while number > 0 :

count +=1

number= number //10

print ("number of digit",count)

Example:

#Check a number is Palindrome or not

number= int(input("enter a number"))

temp = number

rev =0 while number > 0 :

digit = number % 10

rev = rev * 10 + digit

number //=10

if temp == rev :

print ("number is palindrome")

else:

print ("number is NOT palindrome")

Challenges :

  1. Understanding loop iteration.

  2. Handling errors.

Resources :

  1. Official Python Documentation: Control Structures

  2. W3Schools' Python Tutorial: Control structures

  3. Scaler's Python Course: Control Flow

Goals for Tomorrow :

  1. Explore something more on functions.

    Conclusion :

Day 15’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ https://github.com/p-archana1 ]

LinkedIn : [ https://www.linkedin.com/in/archana-prusty-4aa0b827a/ ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!

0
Subscribe to my newsletter

Read articles from Archana Prusty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Archana Prusty
Archana Prusty

I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.