Mastering Python Conditionals (if, else, elif)

Mastering Python Conditionals (if, else, elif)

Hey everyone! ??

Imagine making decisions in your daily life:

  • You play PUBG if it’s Sunday.
  • You order ice cream when it’s sunny. ??
  • You go hiking if your parents allow it. ???

These are all conditional decisions—and just like that, in Python, we can make similar choices using if, else, and elif statements! Let’s break it down:

?? What Are Python Conditionals?

In Python, we use conditionals to execute certain blocks of code only if a specific condition is met. This is how we write programs that react to different inputs or events.

1. The if Statement:

  • This is used to check if a condition is true.
  • If true, the code block under if is executed.
  • If false, it skips the code block.

2. The else Statement:

  • If none of the if conditions are true, the else block will run.
  • It’s like a fallback—when nothing else works, do this.

3. The elif Statement:

  • Short for "else if," it allows you to check multiple conditions in sequence.
  • Once a condition is met, the rest of the conditions are ignored.

IMPORTANT NOTES:

  1. There can be any number of elif statements.
  2. Last else is executed only if all the conditions inside elifs fail.

Syntax Breakdown:

Example:

Let's check if a number is greater than 10.

If num is 15, the output will be: "Greater than 10".

  • Quick Quiz: Write a program to print yes when the age entered by the user is greater than or equal to 18.

?? Exploring Relational & Logical Operators:

Relational Operators: These Operators are used to evaluate conditions inside the if statements. Some examples of relational operators are:

  • == : Equals
  • != : Not equals
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to

Logical Operators: Used to combine multiple conditions:

  • and: True if both conditions are true.
  • or: True if at least one condition is true.
  • not: Reverses the result—True becomes False and vice versa.


Example:


Here, the user is eligible for the offer if their age is between 18 and 60.

Real-World Applications:

  • Grading System: Determine a student’s grade based on their score.
  • Spam Detection: Detect keywords like “buy now” or “click here” in comments.
  • Access Control: Grant or restrict access to resources based on user input.

?? Why This Is Critical for Data Science:

Conditionals form the foundation of decision-making in programming. Whether you're analyzing datasets or building machine learning models, being able to apply logic to make decisions is essential for:

  • Data filtering: Apply conditions to select data subsets.
  • Feature engineering: Create new features based on existing data.
  • Automating processes: Make your code smarter by reacting to various inputs dynamically.

???? Practice Challenges:

  1. Write a program to find the greatest of four numbers entered by the user.
  2. Write a program to find out whether a student has passed or failed if it requires a total of 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user.
  3. A spam comment is defined as a text containing following keywords: “Make a lot of money”, “buy now”, “subscribe this”, “click this”. Write a program to detect these spams.
  4. Write a program to find whether a given username contains less than 10 characters or not.
  5. Write a program which finds out whether a given name is present in a list or not.
  6. Write a program to calculate the grade of a student from his marks from the following scheme:

90 – 100 => Ex

80 – 90 => A

70 – 80 => B

60 – 70 =>C

50 – 60 => D

<50 => F

7. Write a program to find out whether a given post is talking about “Saurabh” or not.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了