Home > Backend Development > Python Tutorial > Day Mastering the Art of Conditional Statements and Loops

Day Mastering the Art of Conditional Statements and Loops

Mary-Kate Olsen
Release: 2025-01-03 11:33:40
Original
1013 people have browsed it

Day 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")
Copy after login
Copy after login

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")
Copy after login
Copy after login

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.")
Copy after login
Copy after login

3. Ternary Conditional Statements

bob_score=87
alen_score=92
answer=bob_score if bob_score>alen_score else alen_score
print(answer)
Copy after login
Copy after login

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)
Copy after login
Copy after login

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)
Copy after login
Copy after login

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)
Copy after login
Copy after login

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

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.

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")
Copy after login
Copy after login

Answer: 0n 1 n 2n 3n

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

age=19
if age>18 and age<25:
   print("the person is an Young Adult")
Copy after login
Copy after login

here start=0 and step=1 by default.

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.")
Copy after login
Copy after login

Answer:1n 2n
1n 3n

While Loop:

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

bob_score=87
alen_score=92
answer=bob_score if bob_score>alen_score else alen_score
print(answer)
Copy after login
Copy after login

Answer: 4n 3n 2n 1n

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.

listl = ['lemon','Orange','apple', 'apricot']
new_list = [i for i in listl if i.startswith('a')]
pri nt(new_li st)
Copy after login
Copy after login

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

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.

listl = ['lemon','Orange','apple', 'apricot']
new_list = [i for i in listl if i.endswith('e')]]
pri nt(new_li st)
Copy after login
Copy after login

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

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.

a=[1,2,3,4]
for i in a:
   print(a)
Copy after login
Copy after login

Answer: 0n 1n 2n 4n

The above is the detailed content of Day Mastering the Art of Conditional Statements and Loops. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template