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.
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 ofa
andb
.Subtraction (
-
): Subtracts the second operand from the first.
Example:a - b
results in the difference betweena
andb
.Multiplication (
*
): Multiplies two operands.
Example:a * b
results in the product ofa
andb
.Division (
/
): Divides the first operand by the second.
Example:a / b
results in the quotient ofa
divided byb
. 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 ofa
divided byb
.Increment (
++
): Increases the value of an operand by 1.
Example:a++
results ina
being increased by 1.Decrement (
--
): Decreases the value of an operand by 1.
Example:a--
results ina
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 intrue
ifa
is equal tob
.Not Equal to (
!=
): Checks if two operands are not equal.
Example:a != b
results intrue
ifa
is not equal tob
.Greater than (
>
): Checks if the first operand is greater than the second.
Example:a > b
results intrue
ifa
is greater thanb
.Less than (
<
): Checks if the first operand is less than the second.
Example:a < b
results intrue
ifa
is less thanb
.Greater than or equal to (
>=
): Checks if the first operand is greater than or equal to the second.
Example:a >= b
results intrue
ifa
is greater than or equal tob
.Less than or equal to (
<=
): Checks if the first operand is less than or equal to the second.
Example:a <= b
results intrue
ifa
is less than or equal tob
.
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 (
&&
): Returnstrue
if both operands are true.
Example:(a > b) && (c > d)
results intrue
only if both conditions are true.OR (
||
): Returnstrue
if at least one of the operands is true.
Example:(a > b) || (c > d)
results intrue
if at least one condition is true.NOT (
!
): Reverses the logical state of the operand.
Example:!(a > b)
results intrue
ifa
is not greater thanb
.
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 ofa
andb
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 ofa
orb
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 ofa
orb
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 ina
.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 ofa
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 ofa
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++), orinput()
(Python) are used for input.Output: Displaying data to the user or another destination.
Common functions for output areprintf
(C),cout
(C++), orprint()
(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 in3.14
.
- Example: In Python,
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.
- Example:
Reading from a File: Reading data using functions like
read()
,readline()
, orreadlines()
.Writing to a File: Writing data to a file using functions like
write()
orwritelines()
.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.
0 Comments