0% found this document useful (0 votes)
33 views

Chapter - 1 of Computer Science Class 12

Uploaded by

School Reports
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Chapter - 1 of Computer Science Class 12

Uploaded by

School Reports
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Class 12th (chapter -1) computer science

Variable and assignment


multiple assignment
1. assignment same valses to multiple variable:-
a=b=c=10,
2. assignment multiple values to multiple variable,
a,b,c=10,20,30
Note:-In python variable not created until some value assign to it.
Print(a)
a=10
output produce a error
Dynamic typing
A variable pointing to a value of a certain type, can be change to different type . This is called Dynamic typing.
For ex. a=10
Print(a)
a=”hello”
print(a)
output:-
hello
Simple input and output
In python to get input from user you can built in function input()
Syntax:- variable=input(“ hello world”)
Name=input(“any text”)
Note:- The input() function always return a value of string type
For example:- num1=input(“enter any no”)
num2=input(“enter any no.”)
sum=num1+num2
print(sum)
output will not add the no. because it will convert no. in string
Note:- If you want to take input from user “Numeric”
Ex. age=int(input(“enter you age”)
age+1
print(age)
result will add 1 to the age
ex.2
num1=int(input(“enter any no”))
num1=int(input(“enter any no”))
sum=num1+num2
print(sum)
output will add the no. because it will convert string to int.

Feature of print function()

 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.

num=int(input("enter your age"))


if num>18:
print("welcome to pubg")
print("python is easy")
if-else condition

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).

Logical NOT is represented as ^.

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.

Operator Name Description Application

& 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.

The table below defines the JavaScript bitwise operators.

Operator Name Type Action

& Bitwise AND Binary If bits of both operands


are ones, returns a one in
each bit position
Operator Name Type Action

| Bitwise OR Binary If bits of either operand


are ones, returns a one in
a bit position

^ Bitwise XOR Binary If a single operand is a one,


returns a one in a bit
position

~ Bitwise NOT Unary Flips the bits in the


operand

<< Left shift Binary Shifts first operand a


number of bits to the left
as specified in the second
operand, shifting in zeros
from the right

>> Right shift Binary Shifts first operand a


number of bits to the right
as specified in the second
operand, and discards
displaced bits

>>> Zero-fill right shift Binary Shifts first operand a


number of bits to the right
as specified in the second
Operator Name Type Action

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.

Example 1: Bitwise AND operation of two one-bit operands.

Left operand Right operand Result

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

Are both digits 1? No No No Yes No No No No

Bitwise AND output 0 0 0 1 0 0 0 0

Thus: 28 & 17 (bitwise AND) = 00010000 (binary) = 16 (decimal).

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.

Example 1: The bitwise OR operation of two one-bit operands.

Left operand Right operand Result

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

Is either digit 1? No No No Yes Yes Yes No Yes

Bitwise OR output 0 0 0 1 1 1 0 1
Thus: 28 | 17 (bitwise OR) = 00011101 (binary) = 29 (decimal).

Bitwise exclusive OR (XOR)


The bitwise exclusive OR (XOR) operator returns 1 if the bits of both operands are
opposite. Otherwise, it returns 0.

Example 1: The bitwise XOR operation of two one-bit operands.

Left operand Right operand Result

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 XOR output 0 0 0 0 1 1 0 1

Thus: 28 ^ 17 (bitwise XOR) = 00001101 (binary) = 13 (decimal).

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

Bitwise NOT output 1 1 1 0 0 0 1 1

Thus: ~28 (bitwise NOT) = 11100011 (binary) = 227 (decimal).

Bitwise left shift


The bitwise left shift operator shifts the bits left by the bits specified by the right operand.
The positions vacated by the left shift operator are filled with 0.

Example: Let's perform the bitwise left shift operation on the integer 6. Each bit will be
shifted left by 1.

6 = 0110

6<<1 = 1100 (binary) = 12 (decimal)

Bitwise right shift


Like the left shift operator, the bitwise right shift operator shifts the bits right by the bits
specified by the right operand. The positions vacated by the right shift operator are filled
with 0.

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

8>>2 = 0010 (binary) = 2 (decimal)

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)

Input list elements:


10
20
30
40
50

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

# Iterate until the given condition is true

while i < 10:

# printing the current value of the above variable

print(i)

# incrementing the value of i value by 1

i += 1

Output
On executing, the above program will generate the following output −
1
2
3
4
5
6
7
8
9

You might also like