Week - 3 Exploring Python Operators & Conditional Statements
Satender Kumar
Information Security Analyst | SIEM & Threat Detection (Splunk, Wireshark) | Cloud Security | Python & Automation | Risk & Compliance (NIST, ISO 27001, GDPR) | Security+ | CySA+ | SSCP
" Python offers a plethora of operators & Conditional Statements, each serving a specific purpose in programming"\end"). Let's delve into the various types
Exploring Python Operators
- These operators perform mathematical operations.
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
- Floor Division (//)
- Comparison operators are used to compare values.
- Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
- Logical operators combine conditional statements.
- Logical AND (and)
- Logical OR (or)
- Logical NOT (not )
- Assignment operators assign values to variables.
- Assignment (=)
- Add and assign (+=)
- Subtract and assign (-=)
- Multiply and assign (*=)
- Divide and assign (/=)
- Modulus and assign (%=)
- Exponentiation and assign (**=)
- Floor Division and assign (//=)
领英推荐
- Bitwise operators perform bit-level operations on integers.
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Bitwise NOT (~)
- Left Shift (<<)
- Right Shift (>>)
- Membership operators test whether a value is present in a sequence.
- in: Returns True if the specified value is present in the sequence.
- not in: Returns True if the specified value is not present in the sequence.
- Identity operators compare the memory locations of two objects.
- is: Returns True if both operands point to the same object.
- is not: Returns True if both operands do not point to the same object.
Conditional Statements
Conditional statements in Python allow you to execute different blocks of code based on the evaluation of a condition. Here's a list of conditional statements in Python:
1. If Statement:
The if statement is used to execute a block of code if a condition is true.
x = 10
if x > 0:
print("x is positive")
The if-else statement is used to execute one block of code if the condition is true and another block if it's false.
2. If-else Statement:
The if-else statement is used to execute one block of code if the condition is true and another block if it's false.
x = -5
if x > 0:
print("x is positive")
else:
print("x is non-positive")
3. If-elif-else Statement:
The if-elif-else statement allows you to check multiple conditions and execute different blocks of code accordingly.
x = 0
if x > 0:
print("x is positive")
elif x < 0:
print("x is negative")
else:
print("x is zero")
4. Nested If Statement:
You can nest one if statement inside another to create more complex conditional logic.
x = 10
if x > 0:
if x % 2 == 0:
print("x is positive and even")
else:
print("x is positive and odd")
else:
print("x is non-positive")
5. Ternary Conditional Operator:
Python also supports a ternary conditional operator for concise conditional expressions.
x = 10
message = "even" if x % 2 == 0 else "odd"
print("x is", message)
"Below Chart Show All in One"