➢ Approaches To Develop the Program in Python/ Solving Problems in Python:
In Python, a program refers to a set of optimized instructions or code written in the Python
programming language to perform a specific task or achieve a particular goal. Python programs
are typically composed of statements, expressions, functions, classes, and modules.
Optimized instructions take less execution time and less time.
The purpose of writing a program is to solve the real time problems and to real time tasks.
Source Code Extension- .py
In python, we have 1 types of approaches to develop a program. They are –
1. Interactive Approach:
In interactive approach, python programmer can issue one statement at a time and gets one
output at a time.
Example:
a. Python Command Prompt
b. Python IDLE Shell
2. Batch Mode Approach:
In Batch Mode Approach programmer will write group of instructions in a file with an
extension ‘.py’.
It is useful to real time application in the form of programs.
Example:
a. Python IDLE Shell
b. Third Party IDEs (Integrated Development Environment): Pycharm, Anakonda Jupiter
Note Book, Anakonda Spider, Atom, VS Code, Google Clab, Sublime Text, etc.
➢ Displaying the Result of Python Program on the Console/ print()
We use a pre-defined function called print() to display the result of python program on the
console.
This function can be used in 5 ways:
a. print(value)
This syntax either displays single or multiple values.
b. print(message1,message2,message3,message4…)
print(message1+message2+message3+message4…)
c. print(message cum values)
d. print(message cum values with format)
e. print(values cum message with format specifiers)
%d → Displaying Integer Data
%f → Displaying Float Data
%s → Displaying Str Data
To print other types of data change them into str type and then print.
f. print(value,end= "delimiter")
This syntax displays the contents horizontally.
➢ Reading the data dynamically from keyboard/ input() / input(message)
To read the data dynamically from keyboard, we use 2 pre-defined functions.
a. input() →
Var_name=input()
The input given will be stored in str class, but we can change it into other classes using type
casting functions.
b. input(message)
To get a user-prompting message before storing the input to a variable.
➢ Operators & Expressions in Python:
An operator is a symbol which is used to perform some operation on objects and values.
If any operator connects two or more values or objects, then it is called expression.
In python, we have 7 types of operators:
Python programming doesn’t support unary operators like ++ and --
Python programming doesn’t support ternary operators. (res=Expr1?: Expr2?: Expr3?)
Python Programming supports Short hand operators (+=, -=, ~= etc)
Python supports its own ternary operator called ‘if else Operator’.
1. Arithmetic Operators: These are those which are used to perform arithmetic operations such
as addition, subtraction, multiplication etc.
If an arithmetic operator connects with 2 or more objects/values, then it is called arithmetic
expression.
We have 7 types of arithmetic operator in python:
a. + → Addition
b. - → Subtraction
c. * → Multiplication/Repetition
d. / → Division (Gives Float Quotient)
e. // → Floor Division (Gives Int Quotient)
f. % → Modulo Division (Gives Remainder)
g. ** → Exponentiation (Power)
(a+b)^2
(a^m)/(a^n)
(a^m)^n
2. Assignment Operator:
The purpose of assignment operator is to assign or transfer RHS value/expression to the LHS
variable.
The symbol of assignment operator is ( = ).
We can use it in 2 ways:
a. Single line assignment: For one variable only.
b. Multi line assignment: For multiple variables.
Swapping Logic:
3. Relational Operators: Also Called Comparison Operators.
The purpose of relational operators is to compare two values.
If relational operator connected with two object values, then it is called relational
expression.
The result of relational expression is either True OR False.
The relational expression is also called Test Condition.
In Python Programming we have 6 types of relational operators:
a. > → Greater Than
b. < → Less Than
c. == → Equality Operator
d. != → Not Equal To
e. >= → Greater Than Or Equal To
f. <= → Less Than Or Equal To
4. Logical Operators: Also called Comparison Operators.
The purpose of logical operators is to compare to or more relational expressions.
If a logical operator connected with two or more relational expression, then it is called
logical expression or compound test condition.
The result of logical expression is either True or False.
We have 3 logical operators in Python:
a. and → Physical Adding
Syntax: RelExp1 and RelExp2
Truth Table
RelExp1 RelExp2 RelExp1 and RelExp2
T F F
F T F
F F F
T T T
When first expression is False, then PVM will not check second and subsequent
expressions. This is called short circuit evaluation.
If the and operator connects multiple relational operators and the result of initial
relational expression is False, then PVM will not evaluate other relational expressions.
Final result will be False. This evaluation process is called short circuit evaluation.
b. or → Physical OR
Syntax: RelExp1 or RelExp2
Truth Table
RelExp1 and
RelExp1 RelExp2
RelExp2
T F T
F T T
F F F
T T T
If the or operator connects multiple relational operators and the result of initial
relational expression is True, then PVM will not evaluate other relational expressions.
Final result will be True. This evaluation process is called short circuit evaluation.
c. not
Syntax: not RelExp
Truth Table
RelExp RelExp
T F
F T
5. Bitwise Operators:
Bitwise operators are applicable on integer data and not on floating type data. Because float
data internally has so many decimal places.
The execution process of bitwise operator is that it first converts integer data to binary data,
then perform the operations binary data depending upon type of bitwise operators and
gives the result in the form of decimal number system.
Since these operators performs the operations in the form of bit by bit, hence they are
named as bitwise operators.
In python, we have 6 bitwise operators.
a. Bitwise Left-Shift Operator: <<
Syntax: var_name=Given Number << Number of bits
a=10 ( Saved in Bits )
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
a<<3 ( Now all the bits will be shifted 3 steps towards left )
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
The execution process of Bitwise Left-Shift Operator is that ‘it moves number of bits
towards the left by adding number of zeroes by flipping off same number of bits at right
side.’
Formula for calculation:
a=10
a<<3 = 10 × 2^3=80
a<<4 = 10 × 2^4=160
Resulting Number = Original Number × 2^(Number of bits shifted towards left)
b=19
b<<4=19× 2^4=304
b. Bitwise Right-Shift Operator: >>
The execution process of Bitwise Left-Shift Operator is that ‘it moves number of bits
towards the left by adding number of zeroes by flipping off same number of bits at right
side.’
a=10
0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
a>>3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Formula=Original Number // 2^(Number of bits shifted towards right)
c. Bitwise Or Operator: |
Syntax: var1 | var2
The functionality of Bitwise Or Operator is shown below:
var1 var2 var1 | var2
0 1 1
1 0 1
0 0 0
1 1 1
a 1 0 1 0
b 0 1 1 0
c 1 1 1 0
When we apply this on two sets, then each value of first set will be compared with
second set and then each value of second set will be compared with first set. It is
supported by sets but not directly on str.
d. Bitwise And Operator: &
Syntax: var1 & var2
The functionality of Bitwise And operator is shown below:
a 1 0 1 0
b 0 1 1 0
c 0 0 1 0
4 0 1 0 0
3 0 0 1 1
4 and 3 0 0 0 0
e. Bitwise Complement Operator: ~
Syntax: var_name= ~ Given_number
The execution process of bitwise complement operator is that it inverts the bits.
Inverting of bits means 0 becomes 1 and 1 becomes 0.
Formula = - ( Original Number + 1)
~10 = -11
10 = 1010
~10 = 0101 (which is the binary form of -11)
Here 11 is the counter part of -11
All negative number stored in main memory are in the form of 2’s complement (1’s
complement +1)
11 = 1011
1’s complement of 11 = 0100 (1 becomes 0, 0 becomes 1)
2’s complement of 11 = 0100 + 0001 = 0101 (which is the complement of 11)
Binary Addition: ( 1+1=0 with carry 1, 0+0=0, 1+0=1, 0+1=1)
f. Bitwise XOR Operator: ^
Syntax: var_name = val1 ^ val2
The functionality of XOR operator is shown below: Similar Bits → 0, different bits → 1
val1 0 1 0 1
val2 1 0 0 1
val1 ^ val2 1 1 0 0
10 1 0 1 0
7 0 1 1 1
10^7 1 1 0 1
Swapping: Write a program that accepts two integer values and swaps them by using
Bitwise XOR operator.
6. Membership Operators:
The purpose of membership operators is that to check the existence of a particular object in
iterable object.
An iterable object is the one that contains more than 1 value. Ex- Str, bytes, bytearray, list,
tuple, set, frozenset, dict.
In python, we have 2 types of membership operators:
a. In operator:
Syntax: value in iterable-object
In operator returns True provided “Value” is present in “Iterable-Object” and vice-versa.
b. Not in operator:
Syntax: value not in iterable-object
In operator returns True provided “Value” is not present in “Iterable-Object” and vice-
versa.
Keys are considered in dict
Int comparison with str is not possible.
7. Identity Operator:
The purpose of identity operators is to compare the memory addresses of two objects.
In python, we have 2 types of identity operators:
a. Is operator: obj1 is obj2
Here is operator returns True provided obj1 and obj2 contains same memory address.
Here is operator returns False provided obj1 and obj2 contains different memory
address.
Is operator applied on deep copied object returns True.
Is operator applied on shallow copied object returns False.
b. Is not operator: obj1 is not obj2
Here is operator returns True provided obj1 and obj2 contains same memory address.
Here is operator returns False provided obj1 and obj2 contains different memory
address.
Is operator applied on deep copied object returns False.
Is operator applied on shallow copied object returns False.
It is because of str constant value. In python, one memory address is created for string
have same case, same meaning and same order.
0-256 values always have same memory address, because of 8-bit resisters.
-1 to -5 also have reserved memory addresses for signs.
In multi-line assignment, PVM will internally take 300 and points both the variables
towards it.
For fundamental type values only M.L. Assignment gives same memory address and not
for others.
➢ Python Ternary Operator:
The name of python ternary operator is ‘if..else’ operator.
Syntax: var_name= Expr1 if Test_Condition Expr2
The execution process of ternary operator is that first test condition is evaluated. If the test
condition is true then PVM executes Expr1 and whose result is assigned to LHS variable. If the
test condition is false then PVM executes Expr2 and whose result is assigned to LHS variable.