Skip to main content

Operators in python with example

 In Python, operators are symbols or special characters that perform various operations on operands (variables, values). Python supports a wide range of operators, including arithmetic, comparison, logical, assignment, bitwise, and more. Let's go through some of the commonly used operators with examples:

1. Arithmetic Operators:

   - Addition (+): Adds two operands.
   ```python
   a = 5
   b = 3
   result = a + b
   print(result)  # Output: 8
   ```

   - Subtraction (-): Subtracts the right operand from the left operand.
   ```python
   a = 5
   b = 3
   result = a - b
   print(result)  # Output: 2
   ```

   - Multiplication (*): Multiplies two operands.
   ```python
   a = 5
   b = 3
   result = a * b
   print(result)  # Output: 15
   ```

   - Division (/): Divides the left operand by the right operand, returning a floating-point result.
   ```python
   a = 5
   b = 3
   result = a / b
   print(result)  # Output: 1.6666666666666667
   ```

   - Floor Division (//): Divides the left operand by the right operand, returning the largest integer that is less than or equal to the result.
   ```python
   a = 5
   b = 3
   result = a // b
   print(result)  # Output: 1
   ```

   - Modulus (%): Returns the remainder when the left operand is divided by the right operand.
   ```python
   a = 5
   b = 3
   result = a % b
   print(result)  # Output: 2
   ```

   - Exponentiation (**): Raises the left operand to the power of the right operand.
   ```python
   a = 2
   b = 3
   result = a ** b
   print(result)  # Output: 8
   ```

2. Comparison Operators:

   - Equal to (==): Checks if two operands are equal.
   ```python
   x = 5
   y = 5
   result = x == y
   print(result)  # Output: True
   ```

   - Not equal to (!=): Checks if two operands are not equal.
   ```python
   x = 5
   y = 3
   result = x != y
   print(result)  # Output: True
   ```

   - Greater than (>): Checks if the left operand is greater than the right operand.
   ```python
   x = 5
   y = 3
   result = x > y
   print(result)  # Output: True
   ```

   - Less than (<): Checks if the left operand is less than the right operand.
   ```python
   x = 5
   y = 3
   result = x < y
   print(result)  # Output: False
   ```

   - Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
   ```python
   x = 5
   y = 3
   result = x >= y
   print(result)  # Output: True
   ```

   - Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
   ```python
   x = 5
   y = 3
   result = x <= y
   print(result)  # Output: False
   ```

3. Logical Operators:

   - Logical AND (and): Returns True if both operands are True.
   ```python
   x = True
   y = False
   result = x and y
   print(result)  # Output: False
   ```

   - Logical OR (or): Returns True if at least one operand is True.
   ```python
   x = True
   y = False
   result = x or y
   print(result)  # Output: True
   ```

   - Logical NOT (not): Returns the opposite of the operand's value.
   ```python
   x = True
   result = not x
   print(result)  # Output: False
   ```

4. Assignment Operators:

   - Assignment (=): Assigns the value of the right operand to the left operand.
   ```python
   x = 5
   ```

   - Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
   ```python
   x = 5
   x += 3  # Equivalent to x = x + 3
   print(x)  # Output: 8
   ```

   The above pattern applies to other arithmetic operators as well, such as `-=`, `*=`, `/=`, `//=`, `%=`, `**=`, etc.

5. Bitwise Operators:

   - Bitwise AND (&): Performs a bitwise AND operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x & y
   print(result)  # Output: 1 (Binary representation: 001)
   ```

   - Bitwise OR (|): Performs a bitwise OR operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x | y
   print(result)  # Output: 7 (Binary representation: 111)
   ```

   - Bitwise XOR (^): Performs a bitwise XOR operation on the binary representations of the operands.
   ```python
   x = 5   # Binary representation: 101
   y = 3   # Binary representation: 011
   result = x ^ y
   print(result)  # Output: 6 (Binary representation: 110)
   ```

   - Bitwise NOT (~): Performs a bitwise NOT operation, which inverts the bits of the operand.
   ```python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = ~x
   print(result)  # Output: -6 (Binary representation: 1111 1111 1111 1010)
   ```

   - Bitwise Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified in the right operand.
   ```python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = x << 2
   print(result)  # Output: 20 (Binary representation: 0000 0000 0001 0100)
   ```

   - Bitwise Right Shift (>>): Shifts the bits of the left operand to the right by the number of positions specified in the right operand.
   ```

python
   x = 5   # Binary representation: 0000 0000 0000 0101 (assuming 32-bit)
   result = x >> 1
   print(result)  # Output: 2 (Binary representation: 0000 0000 0000 0010)
   ```

These are some of the commonly used operators in Python. Understanding how to use these operators is essential for performing various calculations, comparisons, and logical operations in Python programs.

Comments

Popular posts from this blog

Web Programming: HTML, DHTML, XML, Scripting, Java, Servlets, Applets

 Web programming encompasses various technologies and concepts used to develop web applications. Let's explore each of them in detail: 1. HTML (Hypertext Markup Language): HTML is the standard markup language used to create the structure and content of web pages. It uses tags to define elements like headings, paragraphs, images, links, forms, etc. Example: ```html <!DOCTYPE html> <html> <head>     <title>My Web Page</title> </head> <body>     <h1>Hello, World!</h1>     <p>This is a paragraph.</p>     <img src="image.jpg" alt="Image">     <a href="https://www.example.com">Visit Example</a> </body> </html> ``` 2. DHTML (Dynamic HTML): DHTML is a combination of HTML, CSS, and JavaScript that allows web pages to become more dynamic and interactive. Example (DHTML with JavaScript): ```html <!DOCTYPE html> <htm...

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...