Chapter 2: Operators and Formatting Notes, Polytechnic 3rd semester computer programming notes

 Hello Everyone, Welcome back to Rajasthan Polytechnic BTER. This is Garima Kanwar Welcome you all to Rajasthan Polytechnic BTER at where you will get notes of rajasthan polytechnic 1st semester, acnd cs branch 3rd and 5th semester.

In todays post you will get polytechnic 3rd semester Computer Programming Notes of Chapter 2: Operators and Formatting. so Let's start.

2.1 Introduction to Operators

Operators are special symbols in programming that perform specific operations on one, two, or more operands (values or variables). They are essential for manipulating data and making decisions in a program. Operators can be broadly categorized into several types based on their functionality.

2.1.1 Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. These include:

  • Addition (+): Adds two operands.
    Example: a + b results in the sum of a and b.

  • Subtraction (-): Subtracts the second operand from the first.
    Example: a - b results in the difference between a and b.

  • Multiplication (*): Multiplies two operands.
    Example: a * b results in the product of a and b.

  • Division (/): Divides the first operand by the second.
    Example: a / b results in the quotient of a divided by b. Note that if both operands are integers, this operation will perform integer division in some programming languages.

  • Modulus (%): Returns the remainder of the division.
    Example: a % b results in the remainder of a divided by b.

  • Increment (++): Increases the value of an operand by 1.
    Example: a++ results in a being increased by 1.

  • Decrement (--): Decreases the value of an operand by 1.
    Example: a-- results in a being decreased by 1.

2.1.2 Relational Operators

Relational operators are used to compare two values. The result of a relational operation is always a boolean value, either true or false.

  • Equal to (==): Checks if two operands are equal.
    Example: a == b results in true if a is equal to b.

  • Not Equal to (!=): Checks if two operands are not equal.
    Example: a != b results in true if a is not equal to b.

  • Greater than (>): Checks if the first operand is greater than the second.
    Example: a > b results in true if a is greater than b.

  • Less than (<): Checks if the first operand is less than the second.
    Example: a < b results in true if a is less than b.

  • Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second.
    Example: a >= b results in true if a is greater than or equal to b.

  • Less than or equal to (<=): Checks if the first operand is less than or equal to the second.
    Example: a <= b results in true if a is less than or equal to b.

2.1.3 Logical and Bitwise Operators

Logical operators are used to combine multiple conditions, while bitwise operators work on individual bits of data.

Logical Operators:

  • AND (&&): Returns true if both operands are true.
    Example: (a > b) && (c > d) results in true only if both conditions are true.

  • OR (||): Returns true if at least one of the operands is true.
    Example: (a > b) || (c > d) results in true if at least one condition is true.

  • NOT (!): Reverses the logical state of the operand.
    Example: !(a > b) results in true if a is not greater than b.

Bitwise Operators:

  • AND (&): Performs a bitwise AND operation on two operands.
    Example: a & b results in a value where each bit is 1 if both corresponding bits of a and b are 1.

  • OR (|): Performs a bitwise OR operation.
    Example: a | b results in a value where each bit is 1 if at least one of the corresponding bits of a or b is 1.

  • XOR (^): Performs a bitwise XOR operation.
    Example: a ^ b results in a value where each bit is 1 if only one of the corresponding bits of a or b is 1.

  • NOT (~): Performs a bitwise NOT operation, flipping all the bits.
    Example: ~a results in a value where each bit is the complement of the corresponding bit in a.

  • Shift Left (<<): Shifts the bits of the first operand left by the number of positions specified by the second operand.
    Example: a << 2 shifts the bits of a two positions to the left.

  • Shift Right (>>): Shifts the bits of the first operand right by the number of positions specified by the second operand.
    Example: a >> 2 shifts the bits of a two positions to the right.

2.2 Input, Output, Formatting, and File I/O

In programming, input, output, and file I/O are essential for interacting with users and handling data. Formatting ensures that the data is presented in a readable and organized way.

Input and Output:

  • Input: Reading data from the user or another source.
    In most programming languages, scanf (C), cin (C++), or input() (Python) are used for input.

  • Output: Displaying data to the user or another destination.
    Common functions for output are printf (C), cout (C++), or print() (Python).

Formatting: Formatting refers to the way data is presented. It includes:

  • String Formatting: Aligning and padding strings, controlling the number of decimal places, and formatting dates and times.

    • Example: In Python, "{:.2f}".format(3.14159) will format the number to two decimal places, resulting in 3.14.
  • Escape Sequences: Special characters used to control the format of the output, such as \n for a new line or \t for a tab space.

File I/O: File Input/Output allows data to be read from and written to files.

  • Opening a File: Opening a file in different modes such as read (r), write (w), or append (a).

    • Example: file = open("example.txt", "r") opens a file in read mode.
  • Reading from a File: Reading data using functions like read(), readline(), or readlines().

  • Writing to a File: Writing data to a file using functions like write() or writelines().

  • Closing a File: Always close a file after completing the file operations using the close() method to free up system resources.

  • File Handling Exceptions: Proper error handling techniques to manage scenarios like file not found or permission denied.


This blog post is written by Garima Kanwar, and the blog name is 'Rajasthan Polytechnic BTER.' We hope this post has provided you with a clear understanding of operators, input/output, and file handling in programming. For more such detailed notes, please visit 'Rajasthan Polytechnic BTER' regularly.

If you want any other subject notes or any other study material please let me know in Comment Section.

Post a Comment

0 Comments