Kickstart Your Python Programming Journey Part-3: Python Control Flow!

Kickstart Your Python Programming Journey Part-3: Python Control Flow!

Hi Friends,

Hope you are doing good!

In my quest to master Python programming, I've already shared two insightful articles documenting my learning journey. In the first two parts ((Part-1 & Part-2)), I've delved into IDEs and code editors, as well as the foundational aspects of Python, including variables, data types, and operators.

In this article I will share some examples related to conditional statements and loops.

Python Control Flow!

  • if: Executes a block of code if a specified condition is true.
  • if else: Executes one block of code if a condition is true, and another block if it is false.
  • Elif: Checks multiple conditions, executing the corresponding block when a condition is true.
  • For loop: Iterates over a sequence (such as a list, tuple, or range) and executes a block of code for each item.
  • while loop: Repeatedly executes a block of code as long as a specified condition is true.
  • range: Generates a sequence of numbers, commonly used to iterate over with for loops.

To have a clarity on above concepts, I have practiced below set of code-

Conditional Statements Examples:

This covers if, else & elif.


#To determine if a year is a leap year using nested conditional statement

 num=int(input("Enter the number: "))

if num>0:
    print("Number is positive")
    if num%2==0:
        print("The number is even")
    else:
        print("The number is odd")
else:
    print("The number is zero or negative")        
Article content
Article content

#To Determine whether given year is a leap year

year=int(input("Enter the year: "))

div=year%4;

if div==0:
    if year%100==0:
        if year%400==0:
            print("The given year ",year," is a leap year!")
else:
    print("The given is not a leap year!")            
Article content
Article content
Article content

#To create simple calculator with basic operators

#simple calculator
operator=input("Enter the operator:")
num1=float(input("Enter the first number:"))
num2=float(input("Enter the second number:"))

if operator=='+':
    print("Its Addition!")
    result=num1+num2
elif operator=='-':
        print("Its Subtraction!")
        result=num1-num2
elif operator=='*':
        print("Its Multiplication!")
        result=num1*num2
elif operator=='/':
        if num2==0:
               print("Its Invalid Division!")
        else:
               print("Its Division!")
               result=num1/num2
else:
       print("Please give a valid")

print(result)        
Article content

Loops Examples:

This covers range, for loop & while loops.


#Range examples- Syntax of range includes 3 parameters- start, stop & step

Article content
Article content
Article content
Article content

#For loop & string example

Article content


#while loop executes till count is less than or equal to 20
print("While loop executes till count is less than or equal to 20")
count=10
while count<=20:
    print(count)
    count=count+1

#while loop executes till modulus of 3 is equal to 0
print("While loop executes till modulus of 3 is equal to 0")
number=12
while number%3==0:
   print(number)
   number=number+1        
Article content

#Example of nested for loops

#nested Loops- loop inside a loop. complete the inner loop and then outside loop

for i in range(5):
    for j in range(2):
        print(f"i:{i} and j:{j}")        
Article content


#Example#1- Calculate the sum of first n natural number using for loop

i=int(input("Enter any natural nummner"))

print(f"The sum of first {i} natural numbers :")


count=0

for j in range(1,i+1):
    #print(j)    
    count=count+j

print(count)          
Article content

#Additional keywords - Break, continue & pass

#break Statement exists the loop prematurely

for i in range(6):
    if i==2:
        break
    print(i)

print("***********************")    

#continue kepts the current iteration and continues with it

for i in range(6):
    if i%2!=0:
        continue
    print(i)
print("***********************")        

#pass statement is a null operation and does nothing.
for i in range(6):
    if i==3:
        pass 
    print(i)   
print("***********************")                  
Article content

Conclusion:

I hope these examples have provided you with a clear understanding of how conditional statements and loops function in Python.

By practicing these fundamental concepts, you can efficiently control the flow of your programs and solve complex problems with ease. Keep experimenting with different scenarios and continue building your Python skills. Stay tuned for the next part of our journey, where we'll explore even more advanced topics. Happy coding!

Enjoy learning and sharing 😊

Thank You All 😊


To view or add a comment, sign in

More articles by Preetha R.

Explore topics