Operators in Python

Operators in Python

Relational Operators in Python

  • Relational operators in Python are used to compare two values or expressions and determine their relationship or relative order.
  • These operators return a Boolean value (True or False) based on the comparison result.
  • Relational operators help you make decisions and control the flow of your program by evaluating conditions.
  • Here are the commonly used relational operators:
Greater Than (>): Checks if the value on the left is greater than the value on the right.
Example: x > y returns True if x is greater than y, otherwise, it returns False.
Loading…
Less Than (<): Check if the value on the left is less than the value on the right.
Example: x < y returns True if x is less than y, otherwise, it returns False.
Loading…
Greater Than or Equal To (>=): Check if the value on the left is greater than or equal to the value on the right.
Example: x >= y returns True if x is greater than or equal to y, otherwise, it returns False.
Loading…
Less Than or Equal To (<=): Check if the value on the left is less than or equal to the value on the right.
Example: x <= y returns True if x is less than or equal to y, otherwise, it returns False.
Loading…
Equal To (==): Checks if the values on both sides are equal.
Example: x == y returns True if x is equal to y, otherwise, it returns False.
Loading…
Not Equal To (!=): Checks if the values on both sides are not equal.
Example: x != y returns True if x is not equal to y, otherwise, it returns False.
Loading…
These examples demonstrate how relational operators compare values and return either True or False based on the specified condition. You can use these comparisons to make decisions and control the flow of your Python programs.

Logical Operators in Python

Logical operators in Python are like decision-makers in your code. They help you make decisions based on conditions, and they work with either "True" or "False" values. Here's how they work in simple terms:

1. and

  • The "and" operator checks if both conditions on its left and right are True.
  • If both are True, the whole expression becomes True; otherwise, it's False.
  • Example: If it's sunny and warm, then it's good weather.
Loading…

2. or

  • The "or" operator checks if at least one of the conditions on its left or right is True.
  • If either is True, the whole expression becomes True; if both are False, it's False.
  • Example: You can have ice cream if it's hot or you're happy.
Loading…

3. not

  • The "not" operator flips the condition. If something is True, "not" makes it False, and vice versa.
  • Example: If it's not raining, you can go out.
Loading…
Logical operators are handy for making choices and controlling the flow of your Python programs. They help your code make decisions, just like you do in real life.

Bitwise Operators in Python

  • Bitwise operators in Python are used to perform operations at the binary level, which means they work with 0s and 1s (binary digits) of data.
  • For beginners, think of them like switches that can turn bits (0 or 1) on or off within numbers.
  • There are several bitwise operators, but the key idea is manipulating individual bits to achieve specific results in your code.
  • Here's a simple example:
  • Imagine you have two binary numbers: 1010 and 1100. Bitwise operators allow you to perform operations like AND (&), OR (|), XOR (^), NOT (~), and bit shifting (<< and >>) on each corresponding pair of bits.
  • For instance, if you use the & operator on 1010 and 1100, you get 1000. This means it turned off (set to 0) the bits where both numbers had 1s.
  • In everyday programming, bitwise operators are often used for tasks like data compression, cryptography, and low-level hardware control, making them essential for certain types of applications but less commonly used for typical beginner-level coding.
Loading…
  • In this code, we demonstrate the bitwise AND, OR, XOR, NOT, left shift, and right shift operators with binary and decimal results.
  • These operators manipulate the individual bits of numbers, allowing you to perform various operations at the binary level.

Membership Operator in Python

  • The membership operator in Python is like a detective tool that helps you find out if something is inside a group or a collection of things.
  • It's all about checking whether a particular item is a member of a sequence, like a list or a string.
  • Imagine you have a bag of fruits, and you want to know if there's an apple inside it.
  • The membership operator is what you'd use. It answers the question, "Is this thing in here?" "Is this here?" It answers the question.
In Python, we use two membership operators:

in

  • This operator checks if something is a sequence.

not in

  • This operator checks if something is not inside a sequence.
Loading…
This code checks if "banana" is in the list of fruits and prints the result accordingly.
Loading…
Here, we use the not-in operator to check if "giraffe" is not in the list of animals and print the result.
These simple examples illustrate how the membership operators can help you quickly check for the presence or absence of items in lists or sequences in Python.