DEV Community

Cover image for Day 3: Mastering the Art of Conditional Statements and Loops
Praneeth
Praneeth

Posted on

Day 3: Mastering the Art of Conditional Statements and Loops

Conditional Statements

Like other programming languages, Python also includes conditional statements. But the only difference is that instead of else if, we have elif.
Conditional statements control the flow of a program based on specific conditions. They enable decision-making by allowing the program to execute different blocks of code depending on whether a condition evaluates to True or False.
instead of explaining the if,elif and else individually let us cover them all in a single example.

if a%2==0:
   print("The Number is an Even Composite")
elif not_prime(a):
   print("The Number is an Odd Composite")
else:
   print("The Number is a Prime")
Enter fullscreen mode Exit fullscreen mode

here, let the number be 3.
First the program will check whether the number is divisible by 2 (if a%2==0)
since it is not even, it goes to elif satement(if not_prime(a))
since neither the if, nor the elif are not true, the program will go to the else part and it will print:
The Number is a Prime

Key Features:

1. Logical Operators for Conditions

age=19
if age>18 and age<25:
   print("the person is an Young Adult")
Enter fullscreen mode Exit fullscreen mode

2. Nested Conditional Statements

You can nest conditional statements within one another to evaluate complex conditions.

age = 20
if age >= 18:
    if age < 25:
        print("You are a young adult.")
    else:
        print("You are an adult.")
else:
    print("You are not an adult yet.")
Enter fullscreen mode Exit fullscreen mode

3. Ternary Conditional Statements

bob_score=87
alen_score=92
answer=bob_score if bob_score>alen_score else alen_score
print(answer)
Enter fullscreen mode Exit fullscreen mode

Answer:92

💡 Trick of the Day:

startswith() and endswith()

  • The startswith() and endswith() are string methods that return True if a specified string starts with or ends with a specified value.
  • Let’s say you want to return all the names in a list that starts with
    "a."

  • here is how you would use startswith() to accomplish that.

  • Using startswith():

listl = ['lemon','Orange','apple', 'apricot']
new_list = [i for i in listl if i.startswith('a')]
pri nt(new_li st)
Enter fullscreen mode Exit fullscreen mode

Answer: ['apple', 'apricot']

  • Using endwith():

listl = ['lemon','Orange','apple', 'apricot']
new_list = [i for i in listl if i.endswith('e')]]
pri nt(new_li st)
Enter fullscreen mode Exit fullscreen mode

Answer: ['apple', 'Orange']

Loops

In Addition to decision-making statements, Python programming also supports looping statements. There are

1. while
2. for

1. For Loop:

The for loop in Python iterates over a sequence (such as a list, tuple, string, or range) and performs an operation for each item in that sequence.

a=[1,2,3,4]
for i in a:
   print(a)
Enter fullscreen mode Exit fullscreen mode

Answer: 0\n 1\n 2\n 3\n 4\n

Here, the for loop iterates through all the elements in the list a and prints them.
Using range() with for:
You can use the range() function to generate a sequence of numbers.

for i in range(4):
    print(i)
Enter fullscreen mode Exit fullscreen mode

Answer: 0\n 1 \n 2\n 3\n

Range():
The basic syntax of the range() function is:

range(start, end, steps) 
Enter fullscreen mode Exit fullscreen mode

here start=0 and step=1 by default.

for i range(1,3):
   print(i)
for j range(1,4,2):
   print(j)
Enter fullscreen mode Exit fullscreen mode

Answer:1\n 2\n
1\n 3\n

While Loop:

The while loop continues to execute the block of code as long as the condition evaluates to True.

number=4
while(number!=0):
   print(number)
   number--
Enter fullscreen mode Exit fullscreen mode

Answer: 4\n 3\n 2\n 1\n

1. break Statement

The break statement is used to terminate a loop prematurely, regardless of its condition. Once the break statement is executed, the control exits the loop.

for i in range(10):
    if i == 5:
        break  # Exit the loop when i equals 5
    print(i)
Enter fullscreen mode Exit fullscreen mode

Answer: 10\n 9\n 8\n 7\n 6\n

2. continue Statement

The continue statement is used to skip the rest of the code in the current iteration and proceed to the next iteration of the loop.

for i in range(10):
   if i%2==0:
     continue
   print(i)
Enter fullscreen mode Exit fullscreen mode

Answer: 1\n 3\n 5\n 7\n 9\n

3. pass Statement

The pass statement is a placeholder used when a block of code is syntactically required but you don't want to execute any code. It literally does nothing.

for i in range(5):
    if i == 3:
        pass  # Do nothing when i equals 3
    else:
        print(i)
Enter fullscreen mode Exit fullscreen mode

Answer: 0\n 1\n 2\n 4\n

Top comments (0)