Operator and Decision Making - Jupyter Notebook | PDF | Arithmetic | Numbers
0% found this document useful (0 votes)
100 views

Operator and Decision Making - Jupyter Notebook

Here is a program to calculate percentage and grade of 5 subjects: total_marks = 0 for i in range(5): marks = int(input(f"Enter marks of subject {i+1}: ")) total_marks += marks percentage = total_marks/500 * 100 print(f"Percentage is: {percentage}") if percentage >= 90: print("Grade is: A") elif percentage >= 80 and percentage < 90: print("Grade is: B") elif percentage >= 60 and percentage < 80: print("Grade is: C") else: print("Grade is: Fail")

Uploaded by

Shivam Motla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Operator and Decision Making - Jupyter Notebook

Here is a program to calculate percentage and grade of 5 subjects: total_marks = 0 for i in range(5): marks = int(input(f"Enter marks of subject {i+1}: ")) total_marks += marks percentage = total_marks/500 * 100 print(f"Percentage is: {percentage}") if percentage >= 90: print("Grade is: A") elif percentage >= 80 and percentage < 90: print("Grade is: B") elif percentage >= 60 and percentage < 80: print("Grade is: C") else: print("Grade is: Fail")

Uploaded by

Shivam Motla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Tutorial On Python 3

Python Operators
Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

Arithmetic operators

Assignment operators

Comparison operators

Logical operators

Identity operators

Membership operators

Bitwise operator

Python Arithmetic Operators


Symbol Operator Example

+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y

In [1]: #Float Division


10/3

Out[1]: 3.3333333333333335

In [3]: # Floor Division or Int division


10//3

Out[3]: 3

In [4]: # Exponent opr


2**3

Out[4]: 8

In [ ]:

Python Comparison Operators


Symbol Operator Example

== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


In [5]: 10 == 10

Out[5]: True

In [6]: 20>10

Out[6]: True

In [7]: 12.3<12

Out[7]: False

Python Logical Operators


Operator Description Example

and Returns True if both statements are true x < 5 and x < 10

or Returns True if one of the statements is true x < 5 or x < 4

not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

In [8]: 10>5 and 10<20

Out[8]: True

In [9]: 10>5 and 20<10

Out[9]: False

In [10]: 10>5 or 20<10

Out[10]: True

In [11]: # Nonzero values are true and Zero false

not 10

Out[11]: False

In [12]: not 0

Out[12]: True

In [13]: not 12.4

Out[13]: False

Python Membership Operators


Membership operators are used to test if a sequence is presented in an object:

Operator Description Example

in Returns True if a sequence with the specified value is present in the object x in y

not in Returns True if a sequence with the specified value is not present in the object x not in y

In [16]: st = "red green yellow blue white"


'green' in st

Out[16]: True
In [17]: st = "red green yellow blue white"
'Green' in st

Out[17]: False

In [19]: st = "red green yellow blue white"


'black' not in st

Out[19]: True

identity operator
Identity Operator compare the memory location of two objects

Operator

is

is not

In [20]: a=10
b=10
print(id(a),id(b))

140718834033328 140718834033328

In [21]: a is b

Out[21]: True

In [22]: a is not b

Out[22]: False

In [23]: a=10
b=10.0
a is b

Out[23]: False

Bitwise operators
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

Operator Meaning Example

& Bitwise AND x& y = 0 (0000 0000)

Bitwise OR

~ Bitwise NOT ~x = -11 (1111 0101)

^ Bitwise XOR x ^ y = 14 (0000 1110)

>> Bitwise right shift x>> 2 = 2 (0000 0010)

<< Bitwise left shift x<< 2 = 40 (0010 1000)

In [24]: 10&4

Out[24]: 0

In [28]: 10&6

Out[28]: 2
In [29]: ~10

Out[29]: -11

In [30]: 10<<1

Out[30]: 20

In [31]: 10>>1

Out[31]: 5

Decision Making
What is an if statement?

An if statement tests for a condition, and then responds to that condition.


If the condition is true, then whatever action is listed next gets carried out.
You can test for multiple conditions at the same time, and respond appropriately to each condition.

In [3]: if 10<5:
print("10 is Greater")
print("hi")
print("hi")
print("hi")
print("Outside")

Outside

In [8]: # WAP to find largest number amoung two number


a=int(input("Enter first number"))
b=int(input("Enter Second number"))
if a>b:
print(f"{a} is largest")
else:
print(f"{b} is largest")

Enter first number12


Enter Second number34
34 is largest

In [ ]:

The if-elif...else chain


You can test whatever series of conditions you want to, and you can test your conditions in any combination you want.

In [11]: a=int(input("Enter first number"))


b=int(input("Enter Second number"))
c=int(input("Enter Third number"))
if a>b and a>c:
print(f"{a} is Largest")
elif b>c:
print(f"{b} is Largest")
else:
print(f"{c} is Largest")

Enter first number1


Enter Second number23
Enter Third number3
23 is Largest
In [6]: if 10-9:
print("True")
else:
print("False")

True

In [1]: not 0

Out[1]: True

True and False values


Every value can be evaluated as True or False. The general rule is that any non-zero or non-empty value will evaluate to
True.

In [2]: if 0:
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to False.

In [7]: if -51:
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to True.

In [8]: # Arbitrary non-zero numbers evaluate to True.


if 1253756:
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to True.

In [9]: # Negative numbers are not zero, so they evaluate to True.


if -1:
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to True.

In [13]: # An empty string evaluates to False.


if '':
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to False.

In [14]: # Any other string, including a space, evaluates to True.


if ' ':
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to True.

In [ ]: # Any other string, including a space, evaluates to True.


if 'hello':
print("This evaluates to True.")
else:
print("This evaluates to False.")
In [15]: # None is a special object in Python. It evaluates to False.
if None:
print("This evaluates to True.")
else:
print("This evaluates to False.")

This evaluates to False.

In [16]: a=None

In [18]: print(a)

None

In [19]: type(a)

Out[19]: NoneType

WAP that accepts marks of five subjects and finds percentage and prints grades according to th
e
following criteria:
Between 90-100%--------------Print „A‟
80-90%----------------------------Print „B‟
60-80%---------------------------Print „C‟
Below 60%----------------------Print „D‟

In [25]: m1 = float(input("Enter marks of m1 subject"))


m2 = float(input("Enter marks of m2 subject"))
m3 = float(input("Enter marks of m3 subject"))
m4 = float(input("Enter marks of m4 subject"))
m5 = float(input("Enter marks of m5 subject"))
total = float(input("Enter Total Marks"))
# per = sum of all marks / total marks * 100

per = (m1+m2+m3+m4+m5)/total *100

if per>=90 and per<=100:


print("A Grade")
elif per>=80 and per<90:
print("B Grade")
elif per>=60 and per<80:
print("C Grade")
else:
print('D Grade')

Enter marks of m1 subject50


Enter marks of m2 subject50
Enter marks of m3 subject50
Enter marks of m4 subject50
Enter marks of m5 subject50
Enter Total Marks500
D Grade

In [1]: # How we create empty Block of code

if 10>2:
pass
print("Hi")

Hi

In [ ]:

You might also like