Int 213 Lec3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Operators in python

INT213
Immutable= can not be changed
Points to be noted
• A floating-point number is accurate up to 15 decimal places.
• Complex numbers are written in the form, x + yj, where x is the real
part and y is the imaginary part.
• In set and dictionary no duplicate members are allowed
• In tuple and list duplicate members are allowed
• To find keys in dictionary <disctionary_name>.keys()
• To find values in dictionary <disctionary_name>.values()
• s = '''A multiline
string''‘
Maximum size of the int variable
in Python 3
A. 2 bytes
B. Half of the free memory
C. Equals to the free memory
D. 32 bits
The modulus operator
• The modulus operator works on integers (and integer expressions)
and yields the remainder when the first operand is divided by the
second.
• In Python, the modulus operator is a percent sign (%).
Example
• The syntax is the same as for other operators:
>>> quotient = 7 / /3
>>> print (quotient)
2
>>> remainder = 7 % 3
>>> print (remainder)
1
• So 7 divided by 3 is 2 with 1 left over.
Uses
• Check whether one number is divisible by another if x % y is zero,
then x is divisible by y.
• you can extract the right-most digit or digits from a number.
• For example,
x % 10 yields the right-most digit of x (in base 10). Similarly x %
100 yields the last two digits.
Boolean expressions
• A Boolean expression is an expression that is either true or false.
• One way to write a Boolean expression is to use the operator ==, which
compares two values and produces a Boolean value:
>>> 5 == 5
True
>>> 5 == 6
False
• True and False are special values that are built into Python.
Comparison Operators
• x != y
# x is not equal to y
• x>y
# x is greater than y
• x<y
# x is less than y
• x >= y
# x is greater than or equal to y
• x <= y
# x is less than or equal to y
NOTE: “= is an assignment operator and == is a comparison operator”. Also,
there is no such thing as =< or =>.
Which of the following pair
can store only unique values
1. List,Tuple
2. Dictionary,List
3. Tuple,set
4. Dictionary,set
Logical operators
• There are three logical operators:
 and,
 or
 not
• For example, x > 0 and x < 10 is true only if x is greater than 0 and less
than 10.
• n%2 == 0 or n%3 == 0
• not(x > y) is true if (x > y) is false, that is, if x is less than or equal to y.
Logical operators

A B A and B A or B not A
T T T T F
T F F T F
F T F T T
F F F F T
Identity operators
• Identity operator (“is” and “is not”) is used to compare the object’s
memory location. When an object is created in memory a unique
memory address is allocated to that object.
• ‘==’ compares if both the object values are identical or not.
• ‘is’ compares if both the object belongs to the same memory
location.
• The built-in Id() a function is used to get the “identity” of an object.
An integer that will be unique and constant for the object during its
lifetime.
In operator
• in operator : The ‘in’ operator is used to check if a value exists in a
sequence or not. Evaluates to true if it finds a variable in the specified
sequence and false otherwise.
• ‘not in’ operator- Evaluates to true if it does not finds a variable in
the specified sequence and false otherwise.
• Example
k=[1,2,3,4]
3 in k
Output:
True
Bitwise Operators

• Python bitwise operators are used to perform bitwise calculations on


integers. The integers are converted into binary format and then
operations are performed bit by bit, hence the name bitwise
operators.
• Python bitwise operators work on integers only and the final output is
returned in the decimal format. Python bitwise operators are also
called binary operators.
Bitwise Operators
Continue…
• Any nonzero number is interpreted as “true."
>>> x = 5
>>> x and 1
1
>>> y = 0
>>> y and 1
0
The result of 1234%100 is
A.1
B.12
C.4
D.34
Keyboard Input
• input(): built in function to get data from keyboard.
• Takes data in the form of string.
• Eg:
>>> input1 = input ()
What are you waiting for?
>>> print (input1)
What are you waiting for?
• Before calling input, it is a good idea to print a message telling the user
what to input. This message is called a prompt.
• A prompt can be supplied as an argument to input.
• Eg:
>>> name = input ("What...is your name? ")
What...is your name? Arthur, King of the Britons!
>>> print(name)
Arthur, King of the Britons!
• If we expect the response to be an integer, then type conversion needs
to be done.
• Eg:
prompt = "What is the airspeed velocity of an unladen swallow?"
speed =int(input(prompt))

You might also like