Computer Science Notes PDF
Computer Science Notes PDF
Computer Science Notes PDF
Day- 1
Data Types
Class Work
Data Types
Numbers : The Number data types are used to store numeric values in Python. The Numbers in Python
have following core data types:
1) Integer
2) Floating point
3) Complex Numbers
Integers :- Integers are whole numbers such as 5,39 etc. They have no fractional parts. Integers are
represented in Python by numeric values with no decimal point. It can be positive or negative.
Floating point Numbers :- A number having fractional part is a floating – point number. It can be
positive and negative. For ex 3.14
1) Fractional Form : 3500.67
2) Exponential Form:- 3.50075E03, 1.479E02
Complex Numbers :- A complex number is in the form A + Bi where i is the imaginary number, equal to
square root of -1 i.e. .
1.5 +5.1j
String
Any thing enclosed in ‘ or “ quotes become a string. A string can hold any type of
known characters i.e. letters, numbers and special characters.
0 1 2 3 4 Forward Indexing
H E L L 0
-5 -4 -3 -2 -1
len() :- len function return the total number of character present
Backward in the string.
Indexing
Mutable :- The mutable types are those whose values can be changed in place.
Immutable :- The immutable types are those that can never change their value in
place. For ex : integers, floating point numbers, Boolean , strings, tuples etc.
Day 2
Variable Internals:-
Operators
The operator can be defined as a symbol which is responsible for a particular operation between two operands.
Operators are the pillars of a program on which the logic is built in a particular programming language. Python
provides a variety of operators described as follows.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators
Arithmetic operators
Arithmetic operators are used to perform arithmetic operations between two operands. It includes +(addition), -
(subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and exponent (**).
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first operand. If the first operand is
less than the second operand, the value result negative. For example, if a = 20, b = 10
=> a - b = 10
/ (divide) It returns the quotient after dividing the first operand by the second operand. For
example, if a = 20, b = 10 => a/b = 2
* It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a *
(Multiplication) b = 200
% (reminder) It returns the reminder after dividing the first operand by the second operand. For
example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the first operand power to second
operand.
// (Floor It gives the floor value of the quotient produced by dividing the two operands.
division)
Comparison operator
Comparison operators are used to comparing the value of the two operands and returns boolean true or false
accordingly. The comparison operators are described in the following table.
Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition becomes
true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.
Python assignment operators
The assignment operators are used to assign the value of the right expression to the left operand. The assignment
operators are described in the following table.
Operator Description
= It assigns the the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and assign the modified
value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b and
therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assign the modified
value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a = a- b and
therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and assign the modified
value back to left operand. For example, if a = 10, b = 20 => a* = b will be equal to a = a* b and
therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assign the reminder
back to left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b and
therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.
DAY -3
Bitwise operator
The bitwise operators perform bit by bit operation on the values of the two operands.
For example,
1. if a = 7;
2. b = 6;
3. then, binary (a) = 0111
4. binary (b) = 0011
5.
6. hence, a & b = 0011
7. a | b = 0111
8. a ^ b = 0100
9. ~ a = 1000
Operator Description
& (binary If both the bits at the same place in two operands are 1, then 1 is copied to the result.
and) Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero otherwise the resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different otherwise the resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will
be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the right operand.
>> (right The left operand is moved right by the number of bits present in the right operand.
shift)
Logical Operators
The logical operators are used primarily in the expression evaluation to make a decision. Python supports the
following logical operators.
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the two expressions, a
→ true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are the two expressions,
a → true, b → false => a or b → true.
not If an expression a is true then not (a) will be false and vice versa.
DAY -4 AND 5
Membership Operators
Python membership operators are used to check the membership of value inside a Python data structure. If the
value is present in the data structure, then the resulting value is true otherwise it returns false.
Operator Description
in It is evaluated to be true if the first operand is found in the second operand (list, tuple, or
dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or
dictionary).
Identity Operators
Operator Description
is It is evaluated to be true if the reference present at both sides point to the same object.
is not It is evaluated to be true if the reference present at both side do not point to the same object.
Operator Precedence
The precedence of the operators is important to find out since it enables us to know which operator should be
evaluated first. The precedence table of the operators in python is given below.
Operator Description
** The exponent operator is given priority over all the others used in the expression.
<= < > >= Comparison operators (less then, less then equal to, greater then, greater then equal
to).
DAY 6
CH-4
IF – CONDITION :- IF Check the condition is true or false. If the condition is true the
statement inside the if block get execute and if it is false nothing is display in the screen.
Syntax:
if(condition)
Statement
Ex :-
A=int(input(“Enter A:”))
B=int(input(“Enter B:”))
if(A > B)
}
Day- 2
Barebones of a Python Program
Expressions
Statements
Comments
Function
Blocks and indentation
Variables and Assignment
Statements :- A statement is a programming instruction that does some thing i.e. some action
takes place.
Example :- print(“hello”)
Comments :- Comments are the additional readable information to clarify the source code.
Comment in Python begin with symbol # and generally end with end of physical line.
(I) The physical lines beginning with # are the full line comments. There are three full
line comments in the above program are :
i) Add a # symbol in the beginning of every physical line part of the multi-line
comments e.g.
# Multi-line comments are useful for detailed additional information.
# Related to the program in question.
#I thelps clarify certain important things
ii) Type comment as a triple quoted multi-line string e.g.
``` Multi –line comments are useful for detailed additional information related to the
program in question. It helps clarify certain important things
```
Functions :- A function is a code that has a name and it can be reused by specifying its name in
the program, where needed.
For ex :-
if b < 5:
print (“Value of b is less than 5”)
print(“Thank you”)
Variable is a name which is used to refer memory location. Variable also known as identifier and used to
hold value.
In Python, we don't need to specify the type of variable because Python is a type infer language and smart
enough to get variable type.
Variable names can be a group of both letters and digits, but they have to begin with a letter or an
underscore.
We don't need to declare explicitly variable in Python. When we assign any value to the variable that
variable is declared automatically.
Eg:
Output:
1. >>>
2. 10
3. ravi
4. 20000.67
5. >>>
Activity :-
Assignment:
Multiple assignment
Variable definition
Dynamic Typing
Simple Input and output
Class Work
Multiple Assignment
Python allows us to assign a value to multiple variables in a single statement which is also known as multiple
assignment.
We can apply multiple assignments in two ways either by assigning a single value to multiple variables or assigning
multiple values to multiple variables. Lets see given examples.
Eg:
1. x=y=z=50
2. print iple
3. print y
4. print z
Output:
1. >>>
2. 50
3. 50
4. 50
5. >>>
Eg:
1. a,b,c=5,10,15
2. print a
3. print b
4. print c
Output:
1. >>>
2. 5
3. 10
4. 15
5. >>>
Variable definition :- A variable is defined only when you assign some value to it. Using an undefined
variable in an expression /statement cause an error called NameError.
X=20
print(X)
Dynamic Typing: - Python is a dynamically typed language. It doesn’t know about the type of the
variable until the code is run. So declaration is of no use. What it does is, It stores that value at some
memory location and then binds that variable name to that memory container. And makes the contents of
the container accessible through that variable name. So the data type does not matter. As it will get to
know the type of the value at run-time.
# This will store 6 in the memory and binds the
print(type(x))
print(type(x))
Output:
<class 'int'>
<class 'str'>
Simple Input and output:- Python provides numerous built-in functions that are readily available to
us at the Python prompt.
Some of the functions like input() and print() are widely used for standard input and output
operations respectively. Let us see the output section first.
a=5
Python Input
Up till now, our programs were static. The value of variables were defined or hard coded into the source
code.
To allow flexibility we might want to take the input from the user. In Python, we have the input() function
to allow this. The syntax for input() is
input([prompt])
Here, we can see that the entered value 10 is a string, not a number. To convert this into a number we can
use int() or float() functions.
>>> int('10')
10
>>> float('10')
10.0
This same operation can be performed using the eval() function. But it takes it further. It can evaluate even
expressions, provided the input is a string
>>> int('2+3')
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '2+3'
>>> eval('2+3')
5