0% found this document useful (0 votes)
4 views15 pages

PratyushPaul PYTHONCA1

The presentation covers conditional statements in Python, including 'if', 'elif', and 'else', which are essential for decision-making in programming. It discusses syntax, practical examples, best practices, and common mistakes to avoid when using these statements. Understanding these concepts enhances programming proficiency and problem-solving skills.

Uploaded by

Sundari Kali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

PratyushPaul PYTHONCA1

The presentation covers conditional statements in Python, including 'if', 'elif', and 'else', which are essential for decision-making in programming. It discusses syntax, practical examples, best practices, and common mistakes to avoid when using these statements. Understanding these concepts enhances programming proficiency and problem-solving skills.

Uploaded by

Sundari Kali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CA1 Presentation on

CONDITIONAL STATEMENTS IN
PYTHON AND THEIR USE

Presented By :-

Pratyush Paul (21342723061)

Paper Name: Python Programming


Paper Code: BCAC 301
Department: BCA
College Name: The Heritage Academy
Maulana Abul Kalam Azad University of Technology, West Bengal
August,2024
Introduction
Conditional statements are fundamental in Python. They allow the execution
of code based on specific conditions. Mastering these statements is crucial for
efficient programming and decision-making. The primary conditional
statements are if, elif, and else. An if statement checks a condition, and if it's
true, executes a block of code. The elif (short for "else if") statement allows
for multiple conditions to be checked sequentially if the previous conditions
were false. The else statement provides a block of code that will execute if
none of the previous conditions were true. We will cover syntax, examples,
and best practices to master their use effectively.
The 'if' Statement
The if statement is the simplest form of true conditional execution. It evaluates
a condition and executes a block of code if the condition is true. This allows for
basic decision-making in your code.

1 Basic Syntax 2 Flowchart

Output
x is greater than 5
The 'if-else' Statement
The else statement provides an alternative block of code that
executes when the if condition is false. This is useful for defining a
default action when no conditions are met, enhancing program
control.

1 Basic Syntax 2 Flowchart

Output

x is not greater than 5


The 'if-elif-else' Statement
The elif statement is used to check multiple conditions. It allows for more complex
decision- making by providing additional conditions to evaluate if the previous ones
are false. This creates a more flexible flow

1 Basic Syntax 2 Flowchart

Output

x is equal to 5
Nested Conditional Statements

Nested conditional statements are when an if or else statement is placed


inside another. This allows for more intricate decision-making processes in
your code. However, it can also lead to complexity if not managed properly.

1 Basic Syntax 2 Flowchart

Output

x is greater than 5 and y is greater than 15


Conditional Expressions
(Ternary Operator)
This can be used to write the if-else statements in a
single line where only one statement is needed in both
the if and else blocks.

Basic Syntax
Output
Adult
Logical Operators in Conditional Statements
AND Operator
Both conditions must be true. Example:
if x >5 and x <10: print("In range")

OR Operator
Only one condition needs to be true.
Example: if x <5 or x >10: print("Out of
range") Output

Both conditions are true


NOT Operator
Inverts a condition. Example: if not(x ==
10): print("X is not 10")
Comparison Operators in Conditional Statements

Operator Description Example

== Equal to X==10

!= Not Equal to X!=10

> Greater than X>10

< Less than X<10


Practical Example
In this slide, we demonstrate a practical example of using conditional
statements to assign grades based on a student's score. The code begins
by assigning a value to the variable score. This pattern continues with
additional elif statements for scores greater than or equal to 70 and 60,
assigning grades 'C' and 'D' respectively. If none of these conditions
are met, the else statement assigns the grade 'F’.

Finally, the code prints out the grade. This example illustrates how
conditional statements can be used to categorize or make decisions
based on numerical values, showcasing a real-world application of
these concepts.

Output
Your grade is B
Best Practices for Conditional
Conditional Statements
Keep conditions simple Use descriptive names
Avoid complex conditions, use Name your variables clearly to
multiple statements instead. enhance readability.

Maximize readability Test thoroughly


Use consistent style and proper Ensure all conditions are tested
indentations for clarity. through varied inputs.
Common Mistakes
1. Using assignment instead of comparison: Confusing = (assignment) with ==
(equality comparison)
2. Improper indentation: Failing to indent the block of code that follows the
conditional statement correctly.
3. Using elif or else without an initial if: Starting with elif or else without a preceding
if statement.
4. Misunderstanding operator precedence: Not using parentheses to clarify the
order of operations when combining and or in a condition
5. Using mutable default arguments: Defining a function with a mutable default
argument and then modifying it within the function
6. Omitting the colon: Forgetting to include the colon: at the end of the conditional
statement.
7. Comparing incompatible types: Comparing variables of different types that can't
be meaningfully compared.
8. Misusing logical operators: Using and or with non-boolean values in a way that
may not yield the intended result.
9. Unintended variable scope: Assuming a variable defined in a conditional or loop is
accessible outside of it
10. Using is instead of == for value comparison: Confusing is (identity comparison)
with == (equality comparison).
Conclusion
In conclusion, conditional statements are a fundamental part of Python
programming that enables decision-making within your code. Using if, else, and
elif statements, you can execute specific blocks of code based on whether given
conditions are true or false. Logical operators like and, or, and do not allow for
combining multiple conditions, making your code more flexible and dynamic.
Understanding and avoiding common mistakes, such as incorrect indentation
and misuse of comparison operators, is crucial for writing error-free code. By
mastering conditional statements, you can write more complex and efficient
programs, ultimately enhancing your problem-solving skills and programming
proficiency
References
• GeeksforGeeks (2024). Conditional Statements in Python. [online] GeeksforGeeks.
Available at: https://www.geeksforgeeks.org/conditional-statements-in-python/
[Accessed 5 Aug. 2024]

• OluseyeJeremiah (2023). How to Use Conditional Statements in Python – Examples


of if, else, and elif. [online] freeCodeCamp.org. Available at:
https://www.freecodecamp.org/news/how-to-use-conditional-statements-if-else-
elif-in-python/#:~:text=Conditional%20statements%20(if%2C%20else%2C.

• W3Schools (2019). Python Conditions. [online] W3schools.com. Available at:


https://www.w3schools.com/python/python_conditions.asp.

• www.javatpoint.com. (2022). Conditional Expressions in Python - Javatpoint.


[online] Available at: https://www.javatpoint.com/conditional-expressions-in-
python [Accessed 5 Aug. 2024].
THANK YOU

You might also like