Operators.pptx
Operators.pptx
2
INTERACTIVE MODE PROGRAMMING
Once Python is
installed,
typing
will python
invoke thein interpreter
the command
in
immediate mode. line
We can directly typein
code, and press Enter to get
Python
the output.
This prompt can be used
as a calculator.
To exit this mode, type quit()
and press enter.
For example,
3
SCRIPT MODE PROGRAMMING - IDE
For example,
PYTHON KEYWORDS
5
RULES FOR WRITING IDENTIFIERS
Multi-line statement
▪ The end of a statement is marked by a newline character (\).
a=1+2+3+\
4+5+6+\
7+8+9
▪ Multiple statements in a single line using semicolons.
a = 1; b = 2; c = 3
7
CONTINUATION…
Python does not use braces({}) to indicate blocks of code for class and
function definitions or flow control.
Blocks of code are denoted by line indentation, which is rigidly
enforced.
For example −
if True:
print ("True")
else:
8
print ("False")
PYTHON COMMENTS
All characters after the #, up to the end of the physical line, are part of the
comment and the Python interpreter ignores them.
# First comment
print (“Welcome to college!") # second
comment This produces the following result −
Welcome to college!
9
QUOTATION IN PYTHON
Python accepts single ('), double (") and triple (''' or """) quotes to denote string literals, as
long as the same type of quote starts and ends the string.
The triple quotes are used to span the string across multiple lines.
For example,
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
10
CLEAR WINDOWS
>>> import os
>>> clear =
os.system("cls")
11
STANDARD DATA TYPES
>>> xyz
15
PYTHON - NUMBERS
You can also delete the reference to a number object by using the del
statement.
You can delete a single object or multiple objects by using the del
statement.
For example,
>>> del abc
>>> abc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'abc' is not defined
>>> xyz
150
>>> aa=9
>>> bb=5
>>>
>>> aa
bb
14
9
5
>>> del aa,bb
PYTHON - NUMBERS
For example,
15
PYTHON - STRINGS
For example,
Strings in Python are identified as a
>>> it="Welcome to Python class"
contiguous set of characters represented
>>> it
in the quotation marks.
'Welcome to Python class'
Python allows either pair of single or >>> print(it)
double Welcome to Python class
quotes. >>> print(it[0])
W
Subsets of strings can be taken using the
>>> print(it[8:10])
slice operator ([ ] and [:] )
Indexes 0 Thebeginnin of the to
starting at in g >>>
string and their wayfrom to the print(it[11:])
working -1 Python class 16
For example,
The (+) sign is thestring >>> print(it)
plus
concatenation Welcome to Python class
operator.
The asterisk (*) is the repetition >>> print("Hearty " + it)
operator. Hearty Welcome to Python class
>>> print(it+" By CSE
Department")
Welcome to Python class By CSE
Department
>>> print(it*2)
Welcome to Python classWelcome 17
to Python class
PYTHON - LISTS
print(list1[3:])
The plus (+) sign is the list concatenation [1234, 15.95, 'cse']
['cse', 100.5]
PYTHON - TUPLES
For example,
A tuple is another sequence data type that is >>> tuple1 = ( "FX", "cse","Department" )
similar >>> tuple2=("Welcome",1234)
>>> print(tuple1)
to the list.
('FX', 'cse', 'Department')
A tuple consists of a number of values >>> print(tuple2)
('Welcome', 1234)
separated by commas.
>>> print(tuple1+tuple2)
Unlike lists, however, tuples are enclosed ('FX', 'cse', 'Department', 'Welcome', 1234)
within parenthesis. >>> print(tuple2*3)
('Welcome', 1234, 'Welcome', 1234, 'Welcome',
The values stored in a tuple can be accessed 1234)
using >>> print(tuple1[0])
the slice operator ([ ] and [:]) FX
>>>
Indexes starting at 0 in the beginning of the
print(tuple1[1:3])
tuple and working their way to end -1. >>> 'Department')
('cse',
19
print(tuple1[1:])
The plus (+) sign is the concatenation
('cse', 'Department')
PYTHON - TUPLES
For example,
The main difference between lists and tuples >>> list2=["Welcome",1234]
are − >>> tuple2=("Welcome",1234)
>>> print(list2)
Lists are enclosed in brackets ( [ ]
['Welcome', 1234]
) and their elements and size can be
>>> print(tuple2)
changed,
('Welcome',
Tuples are enclosed in parentheses ( 1234)
( ) ) and >>> list2[1]=5000
cannot be updated. >>> print(list2)
Tuples can be thought of as read-only lists. ['Welcome', 5000]
>>> tuple2[1]=5000
Traceback (most recent call last):
File "<stdin>", line 1, in 20
<module>
PYTHON - DICTIONARY
For example,
>>> dict1={}
Python's dictionaries are kind of hash-table >>> dict1['one'] = "ONE"
>>> dict1[2] = "TWO"
type.
>>> print(dict1)
A dictionary consist of key-value pairs. {'one': 'ONE', 2: 'TWO'}
>>> print(dict1['one'])
Dictionaries are enclosed by curly braces ({ ONE
}) and values can be assigned and accessed >>> print(dict1[2])
using square braces ([]). TWO
>>> dict2 = {'name': "class",'code':7, 'dept':
"cse"}
>>> print(dict2)
{'name': 'class', 'code': 7, 'dept': 'cse'}
>>> print(dict2.keys())
>>> print(dict2.values()) 21
dict_keys(['name', 'code',
dict_values(['class', 7,
'dept'])
'cse'])
TYPES OF OPERATOR
** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 21
=
100000000000000000000
0
// Floor Division - The division of operands where the result 9//2 = 4 and 9.0//2.0 =
is the quotient in which the digits after the decimal 4.0,
PYTHON COMPARISON OPERATORS
!= If values of two operands are not equal, then condition becomes (a!= b) is true = True
true.
> If the value of left operand is greater than the value of right (a > b) is not true =
operand, then condition becomes true. False
< If the value of left operand is less than the value of right (a < b) is true. = True
operand, then condition becomes true.
>= If the value of left operand is greater than or equal to the (a >= b) is not true =
value of right operand, then condition becomes true. False
<= If the value of left operand is less than or equal to the value (a <= b) is true = True 24
+= Add AND It adds right operand to the left operand and assign the c += a is equivalent to c = c + a
result to left operand
-= Subtract AND It subtracts right operand from the left operand and c -= a is equivalent to c = c - a
assign the result to left operand
*= Multiply AND It multiplies right operand with the left operand and c *= a is equivalent to c = c * a
assign the result to left operand
/= Divide AND It divides left operand with the right operand and assign c /= a is equivalent to c = c / ac
the result to left operand /= a is equivalent to c = c
/a
%= Modulus AND It takes modulus using two operands and assign the result c %= a is equivalent to c = c % a
to left operand 25
**= Exponent AND Performs exponential (power) calculation on operators and c **= a is equivalent to c = c ** a
PYTHON BITWISE OPERATORS
^ Binary XOR It copies the bit, if it is set in one operand but not both. (a ^ b) = 49 (means 0011 0001)
>> Binary Exactly opposite to the left shift operator. The left operand bits a >> 2 = 15 (means 0000
Right Shift are moved towards the right side, the right side bits are removed. 11112)6
PYTHON LOGICAL OPERATORS
or Logical OR If any of the two operands are non-zero (true) then (a or b) is True. (means 1)
condition becomes true.
not Logical Used to reverse the logical state of its operand. ❖ Not (a and b) is True.
NOT (means True)
❖ Not (a or b) is
False. (means
False)
27
PYTHON MEMBERSHIP OPERATORS
if x is a member of sequence y.
not in Evaluates to true if it does not finds a variable x not in y,
in the specified sequence and false
otherwise. here not in results in a 1,
id(y).
is not Evaluates to false if the variables on either side x is not y,
of the operator point to the same object and
true otherwise. here is not results in 1,
The following table lists all operators from highest precedence to the lowest. Order
- PEMDAS.Operators Meaning
() Parentheses
** Exponent
+, - Addition, Subtraction
<<, >> Bitwise shift operators
^ Bitwise XOR
| Bitwise OR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisons, Identity, Membership operators
or Logical OR 30