Operators and basic constructs in python
Operators and basic constructs in python
– Compilers.
The shell prompt, >>>, is the prompt the interpreter uses to indicate that
it is ready. If you type 1 + 1, the interpreter replies 2.
Python …. Shell
Python…. Script Mode
• Can also store code in a file and use the interpreter to execute
the contents of the file, which is called a script.
• But for anything more than a few lines, should save your code
as a script so you can modify and execute it in future.
Python….Script Mode …
File name : first.py
print(4+3)
print(4-3)
print(4>3)
print("hello World")
Operators in Python
Operators in Python
Operators in general are used to perform operations on values and
variables in Python. These are standard symbols used for the
purpose of logical and arithmetic operations.
Arithmetic operators
Ternary operator
Relational operators
Assignment operators
Logical operators
Bitwise operators
Special operators
Basic Arithmetic operators in Python
Command Name Example Output
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4*5 20
/ Division 19 / 3 6.3333
// Floor Division 19//3 6
% Remainder (modulo) 19 % 3 1
** Exponent 2 ** 4 16
# Examples of Arithmetic Operator
a=9
b=4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Order of Operations
1.parentheses ()
2.exponents **
5. Assignment operator =
Order of Operations
Operator Operation Precedence
() parentheses 0
** exponentiation 1
* multiplication 2
/ division 2
// int division 2
% remainder 2
+ addition 3
- subtraction 3
21
• The computer scans the expression from left to right,
22
Example 1 Order of operations
>>> 1 + 2 * 3
>>> (1 + 2) * 3
• In the first example, the computer calculates 2 * 3 first, then adds 1 to it.
This is because multiplication has the higher priority (at 3) and addition is
below that (at a lowly 4).
>>> 4 - 40 - 3
-39
>>> 4 - (40 - 3)
-33
Syntax Eg:
x if y else z >>> a = 2
>>>b = 1 if a>1 else 0
>>>b
1
>>> a = 1
>>>b = 1 if a>1 else 0
>>>b
0
Biggest of two numbers
# a > b is False
print(a > b)
# a < b is True
print(a < b)
# a == b is False
print(a == b)
# a != b is True
print(a != b)
# a >= b is False
print(a >= b)
# a <= b is True
print(a <= b)
Assignment Operator
Comparison Operators
Referred as the name suggests, it is used to declare assignments to the operands; the
following are the types of assignment operators in python.
NOT operations.
Identity operators-
is and is not are the identity operators both are used to check if two
values are located on the same part of the memory. Two variables that
are equal does not imply that they are identical.is True if the operands
print(a2 is b2)
Membership operators-
Output:
print(‘VIT' in x) True
True
print(‘VIT' not in x) False
True
print(‘Chennai' not in x)
False
print(3 in y)
print('b' in y)
Example Problem
• Number_Of_choc = Number_Of_choc +
(Number_Of_choc /Wrapper_Offer)
• PRINT num_of_chocolates
#Program for calculating number of chocolates
N=int(input("Enter the amount in hand"))
C=int(input("Enter the cost of one chocolate"))
M=int(input("Enter the number of wrappers for
discount"))
a=N//C
b=a//M
T=a+b
print("The number of chocolate BOB gets to eat=",a+b)
Problem 2
ABC company Ltd. is interested to computerize the pay
calculation of their employee in the form of Basic Pay,
Dearness Allowance (DA) and House Rent Allowance
(HRA). DA is 80% of Basic Pay, and HRA is 30% of Basic
pay. They have the deduction in the salary as PF which is
12% of Basic pay.
Input Processing Output Solution
Alternative
Basic Pay Calculate Salary Nothing
Salary
( Basic Pay + (
Basic Pay * 0.8) +
( Basic Pay * 0.3 -
( Basic Pay * 0.12)
Conditional Statements in Python
If – Statements in Python
Syntax:
if condition:
# Statements to execute if
# condition is true (Indentation)
Example
# if statement example
Output:
if 10 > 5: 10 greater than 5
Program ended
print("Program ended")
x=3
if x == 4:
print("Yes")
else:
print("No")
IF-else construct
Syntax: Example:
N=int(input(“Enter n:”))
if(condition):
if(N%2==0):
statements
print(N,”is even”)
else:
else:
statements print(N,”is Odd”)
Example
You can also chain if..else statement with more than one condition.
Nested if Statement
Nested if Statement:
if statement can also be checked inside other if statement.
This conditional statement is called nested if statement. This
means that inner if condition will be checked only if outer if
condition is true and by this, we can see multiple conditions
to be satisfied.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Example
if-elif Statement
if-elif Statement:
is true.
Syntax: if-elif Statement
if (condition):
statement
elif (condition):
statement
elif (condition):
statement
..
..
..
else:
statement
Example
Selection pattern