Chapter - 1 of Computer Science Class 12
Chapter - 1 of Computer Science Class 12
It converts the message or an object into string before writing it on the screen.
It can have multiple parameter.
It Supports multiple escape sequences to format the output.
Ex. “/n” (new line)
“/t”( tab space)
sep argument is space (‘ ‘) used for filling the space between the words of string.
Ex.
Print(“hello”, “welcome”, “to”, “python”,sep= ‘*’)
Output :- hello*welcome*to*python
It append new line character at the end of the line unless you give you own end argument.
Ex. print(“hello india”)
Print(“I love my india”)
Out put
Hello India
Python programming
It will add”\n” automatically
The print() works this way only when you not specified any end argument with it because by default
print() takes “\n” as value for end argument
But if you give an end argument with a print() function then
the print() will print and end it with the string specified with the end argument
Ex. print(“hello welcome”,end=’@’)
Print(“python programming”)
Hello welcome@python programming
if condition
The if condition is considered the simplest of the three and makes a decision based on
whether the condition is true or not. If the condition is true, it prints out the indented
expression. If the condition is false, it skips printing the indented expression.
The if-else condition adds an additional step in the decision-making process compared to the
simple if statement. The beginning of an if-else statement operates similar to a
simple if statement; however, if the condition is false, instead of printing nothing, the indented
expression under else will be printed.
num=int(input("enter your age"))
if num>18:
print("welcome to pubg")
else:
print("watch pogo")
if-elif-else condition
The most complex of these conditions is the if-elif-else condition. When you run into a situation
where you have several conditions, you can place as many elif conditions as necessary between
the if condition and the else condition.
num1=float(input("enter a number:"))
num2=float(input("enter a number:"))
op=input("enter operator +-*/:")
if op=='+':
print(num1+num2)
elif op=='-':
print(num1-num2)
elif op=='*':
print(num1*num2)
elif op=='/':
print(num1/num2)
else:
print("invalid operator")
A bitwise operation operates on two-bit patterns of equal lengths by positionally matching
their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both
the first AND second bits are 1. If only one bit is a 1, the result is 0. AND can also be used
to test individual bits in a bit string to see if they are 0 or 1.
A logical OR (|) operation functions differently from the AND operations. For each bit
pair, the result is 1 if the first OR second bit is 1. If neither bit is 1, the result is 0.
A logical XOR (~) of each bit pair results in a 1 if the two bits are different, and 0 if they
are the same (both zeros or both ones).
Left shift (<<), right shift (>>) and zero-fill right shift (>>>) bitwise operators are also
known as bit shift operators.
Bitwise operators
Bitwise operators are characters that represent actions (bitwise operations) to be performed
on single bits. They operate at the binary level and perform operations on bit patterns that
involve the manipulation of individual bits. Thus, unlike common logical operators like +
or - which work with bytes or groups of bytes, bitwise operators can check each individual
bit within a byte.
The most common bitwise operators used in C/C++ are given in the table below.
& Bitwise AND Copies a bit to the result if it exists in both operands. The To set up a
result is 1 only if both bits are 1. mask to
check the
Operator Name Description Application
values of
specific bits
| Bitwise OR Copies a bit to the result if it exists in either operand. The To add two
result is 1 if either bit is 1. numbers if
there is no
carry
involved
^ Bitwise Copies a bit to the result if it exists in either operand. So, if To toggle bits
Exclusive OR one of the operands is TRUE, the result is TRUE. If neither or swap two
(XOR) operand is TRUE, the result is FALSE. variables
without using
a third
temporary
variable
To find
specific
types of
numbers
(e.g., odd) in
a series of
numbers
(e.g., all
even)
To find
nonrepeating
elements
To detect if
two integers
have opposite
signs
Operator Name Description Application
~ Bitwise NOT Also known as bitwise complement and bitwise inversion, it To flip or
flips zeros into ones and ones into zeros. invert bits
<< Shift left The left operand value is shifted left by the number of bits To align bits
specified by the right operand.
>> Shift right The left operand value is shifted right by the number of bits To align bits
specified by the right operand.
Multiple bitwise operators are used in bit manipulation. These operations happen very fast
and optimize system performance and time complexity.
It's important to keep in mind that the left shift and right shift operators should not be used
for negative numbers. Doing this can result in undefined behaviors in the programming
language.
Also, bitwise operators should not be used in place of logical operators because they work
differently. Logical operators consider non-zero operands as 1 and their result is either 0 or
1. In contrast, bitwise operators return an integer value.
operand, discards
displaced bits, and shifts in
zeros from the left
Bitwise AND
The bitwise AND operator produces an output of 1 if the corresponding bits of both the
operands are 1. If not, the output is 0.
0 0 0
0 1 0
1 0 0
1 1 1
Example 2: Bitwise AND operation of two integers: 28 and 17; the & operator compares
each binary digit of these integers.
Binary digits
28 0 0 0 1 1 1 0 0
17 0 0 0 1 0 0 0 1
Bitwise OR
The bitwise OR operator produces an output of 1 if either one of the corresponding bits is
1. Otherwise, the output is zero.
0 0 0
Left operand Right operand Result
0 1 1
1 0 1
1 1 1
Example 2: Let's consider the previous example of two integers: 28 and 17.
Binary digits
28 0 0 0 1 1 1 0 0
17 0 0 0 1 0 0 0 1
Bitwise OR output 0 0 0 1 1 1 0 1
Thus: 28 | 17 (bitwise OR) = 00011101 (binary) = 29 (decimal).
0 0 0
0 1 1
1 0 1
1 1 0
Example 2: Let's see how bitwise XOR works for our two integers 28 and 17.
Binary digits
28 0 0 0 1 1 1 0 0
17 0 0 0 1 0 0 0 1
Are the two digits opposite of each other? No No No No Yes Yes No Yes
Bitwise NOT
The bitwise NOT operator reverses the bits. Unlike other bitwise operators, it accepts only
one operand.
Example: Let's consider the bitwise NOT operation of the integer 28.
Binary digits
28 0 0 0 1 1 1 0 0
Binary digits
Example: Let's perform the bitwise left shift operation on the integer 6. Each bit will be
shifted left by 1.
6 = 0110
Example: Let's perform the right shift by two bits operations on the integer 8. Each bit will
be shifted right by 2.
8 = 1000
A for loop is a control flow statement that executes code for a predefined number of iterations. The keyword
used in this control flow statement is “for”. When the number of iterations is already known, the for loop is
used.
# input list
inputList = [10, 20, 30, 40, 50]
print("Input list elements:")
# traversing through all elements of the list using for loop
for element in inputList:
# printing each element of the list
print(element)
A loop that runs a single statement or a set of statements for a given true condition. This loop is
represented by the keyword "while." When the number of iterations is unknown, a "while" loop is used.
The statement is repeated until the boolean value is false. Because the condition is tested at the
beginning of a while loop, it is also known as the pre-test loop.
The initialization and condition checking are done at the beginning of the loop.
It is used only when the number of iterations isn’t known.
If the condition is not mentioned in the 'while' loop, it results in a compilation error.
If the initialization is done when the condition is being checked, then initialization occurs
every time the loop is iterated through.
The iteration statement can be written within any point inside the loop.
Syntax
while ( condition) {
statements;
//body of the loop
}
Example
The following program prints all the list elements using the for loop −
# Initializing a dummy variable with 1
i = 1
print(i)
i += 1
Output
On executing, the above program will generate the following output −
1
2
3
4
5
6
7
8
9