Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Python Course - III Operators

This document provides an overview of basic Python programming concepts including Boolean data types, operators, and expressions. It discusses how the bool() method is used to return or convert a value to True or False. It also describes common operators like +, -, *, /, % and how they are used in Python. Logical operators like and, or, not are also explained. The document concludes with examples of Python programs demonstrating the use of these basic concepts like Boolean values, mathematical and logical operators, conditional statements, and shorthand assignments. Assignments for students to practice these concepts are also provided.

Uploaded by

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

Python Course - III Operators

This document provides an overview of basic Python programming concepts including Boolean data types, operators, and expressions. It discusses how the bool() method is used to return or convert a value to True or False. It also describes common operators like +, -, *, /, % and how they are used in Python. Logical operators like and, or, not are also explained. The document concludes with examples of Python programs demonstrating the use of these basic concepts like Boolean values, mathematical and logical operators, conditional statements, and shorthand assignments. Assignments for students to practice these concepts are also provided.

Uploaded by

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

Python Programming Course

Hand Notes 3

Hand Notes for:

Basic Programming (Bool Datatypes, Operators and Expressions)

Basic Programming (Assignment and Logical Operators)

1
Python Programming - III

Session 5 & 6: Basic Concepts

BOOL DATATYPES

The bool() method is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth
testing procedure. The bool() method in general takes only one parameter(here x), on which the standard truth
testing procedure can be applied. If no parameter is passed, then by default it returns False. So, passing a parameter
is optional. It can return one of the two values.
 It returns True if the parameter or value passed is True. True is represented as 1.
 It returns False if the parameter or value passed is False. False is represented as 0.

OPERATORS& EXPRESSIONS

Operators Description
+ (plus) Adds two objects: 3 + 5 gives 8 . 'a' + 'b' gives 'ab' .
- (minus) Gives the subtraction of one number from the other; if the first operand is absent it is assumed to be
zero.
* (multiply) Gives the multiplication of the two numbers or returns the string repeated that many times : 2 * 3 gives
6 . 'la' * 3 gives 'lalala'.
** (power) Returns x to the power of y: 3 ** 4 gives 81 (i.e. 3 * 3 * 3 * 3 )
/ (divide) Divide x by y: 13 / 3 gives 4 . 13.0 / 3 gives 4.333333333333333
% (modulo) Returns the remainder of the division: 13 % 3 gives 1 . -25.5 % 2.25 gives 1.5
<< (left shift) Shifts the bits of the number to the left by the number of bits specified. (Each number is represented in
memory by bits or binary digits i.e. 0 and 1)
2 << 2 gives 8 . 2 is represented by 10 in bits.
Left shifting by 2 bits gives 1000 which represents the decimal 8 .
>> (right shift) Shifts the bits of the number to the right by the number of bits specified.
11 >> 1 gives 5. 11 is represented in bits by 1011 which when right shifted by 1 bit gives 101`which is
the decimal `5 .
& (bit-wise Bit-wise AND of the numbers: 5 & 3 gives 1 .
AND)
| (bit-wise OR) Bitwise OR of the numbers: 5 | 3 gives 7
^ (bit-wise Bitwise XOR of the numbers: 5 ^ 3 gives 6
XOR)
~ (bit-wise The bit-wise inversion of x is -(x+1): ~5 gives -6 .
invert) More details at http://stackoverflow.com/a/11810203
< (less than) Returns whether x is less than y. All comparison operators return True or False. Note the capitalization
of these names.
5 < 3 gives False and 3 < 5 gives True .
Comparisons can be chained arbitrarily: 3 < 5 < 7 gives True.
> (greater than) Returns whether x is greater than y
5 > 3 returns True . If both operands are numbers, they are first converted to a common type.
Otherwise, it always returns False
# (less than or Returns whether x is less than or equal to y
equal to) x = 3; y = 6; x # y returns True
>= (greater Returns whether x is greater than or equal to y
than or equal to) x = 4; y = 3; x >= 3 returns True
2
Python Programming - I

Operators Description
== (equal to) Compares if the objects are equal
x = 2; y = 2; x == y returns True .
x = 'str'; y = 'stR'; x == y returns False .
x = 'str'; y = 'str'; x == y returns True .

!= (not equal to) Compares if the objects are not equal


x = 2; y = 3; x != y returns True .
not (boolean) If x is True , it returns False . If x is False , it returns True .
x = True; not x returns False .
and (boolean) x and y returns False if x is False , else it returns evaluation of y
x = False; y = True; x and y returns False since x is False. In this case, Python will not evaluate y since it
knows that the left hand side of the 'and' expression is False which implies that the whole expression
will be False irrespective of the other values. This is called short-circuit evaluation.
or (boolean) If x is True , it returns True, else it returns evaluation of y
x = True; y = False; x or y returns True . Short-circuit evaluation applies here as well.

Shortcut for Math operation: a = a* 3 can also be written as a*= 3

3
Python Programming - I

Programs in the Class

bool1 = True #bool type


bool2 = False
bool3 = "TRUE"
bool4 = bool(bool3)
print(type(bool3))
print(type(bool4))
# =================== Operations ########
a=5 # 101 (biniary)
b=3 # 11 (biniary)
print(a+b)
print(a-b)
print(a*b)
print(a/b) # / simple divide
print(a//b) # // will integer - Quotient
print(a%b) # % (modulus) will Reminder
print(a**b) # 5 * 5 * 5
print(a<<2) # Left Shift works on Binary number
print(a>>2) # Right Shift works on Binary number

#AND X NAND
#OR X NOR
#XOR Even or Odd number of True conditions : Even T -> F / Odd T -> T
#NOT -> Reverse the output
#logical statements
a=5 # Assignment
#Mathematical: +,-,*,**,/,//, %
x=a>7
y=a>=5 #We read it as Is a greater than or equal to 5
z=a<=5 #We read it as Is a less than or equal to 5
print("Datatype of Z: ",type(z))
# >, < , <=, >=, ==, !=
print(x) # We read it as Is a greater than 7
print(y)
print(z)
print(x or y) # or, and, not
print(x or not (y and z)) # OR is like PLUS AND is like MULTIPLY
print(x and not y)

a==10 # Is a equal to 10
print(a==10) # = : assignment == Condition
print(a==5) # Is a equal to 5
print(a!=10) # Is a not equal to 10
print(not a==10)

#Swapping values
a=5
b=10
a,b,c,d,e,f=b,a,a,a,a,a
print("Value of a = ",a)
print("Value of b = ",b)

#Short hands
a = 5

4
Python Programming - I

a=a+10
a+=10 # a=a+10
a/=10 #a=a/10

print(a)

Assignments
1. WAP to input values of 3 sides of a rectangle - area and perimeter - > input, format and make it user
friendly
2. WAP to input values of 3 sides of a triangle - area and perimeter - > input, format and make it user
friendly
3. WAP Marks, total, avg -> input, format
4. WAP to input a number and display its Double and Half values using SHIFT operator

5. WAP to take input Dividend and Divisior and print its Quotient and Reminder

6. WAP to input 2 numbers and print 2 output: 1) in Decimal format and 2) Mixed Integer format ( e.g.
Q . R/D, 10/3 = 3.1/3)
7. WAP to take a person’s nationality and age and display the same in the following format:

Person is 23 year old and is American Citizen

8. Once I had been to the post-office to buy stamps of five rupees, two rupees and one rupee. I paid
the clerk Rs. 20, and since he did not have change, he gave me three more stamps of one rupee. If
the number of stamps of each type that I had ordered initially was more than one, what was the
total number of stamps that I bought.
9. When an object is thrown into the air with a starting velocity of r feet per second, its distance d in
feet, above its starting point t seconds after it is thrown is about d=rt−16 t square. Evaluate to show
the distance of an object from its starting point that has an initial velocity of 80 feet per second and
5 seconds after it left the ground.

- - - End of Session 3 - - -

You might also like