Lab#03
Lab#03
OBJECTIVE
The Purpose of the lab is to Understand the Operators and input().output() function in
Python Programming.
THEORY
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 operators
Arithmetic operators
Operator Operation Example
+ Addition 5 + 2 = 7
- Subtraction 4 - 2 = 2
* Multiplication 2 * 3 = 6
/ Division 4 / 2 = 2
// Floor Division 10 // 3 = 3
% Modulo 5 % 2 = 1
** Power 4 ** 2 = 16
Assignment operators
Operator Name Example
= Assignment Operator a = 7
+= Addition Assignment a += 1 # a = a + 1
Lab#03
-= Subtraction Assignment a -= 3 # a = a - 3
*= Multiplication Assignment a *= 4 # a = a * 4
/= Division Assignment a /= 3 # a = a / 3
%= Remainder Assignment a %= 10 # a = a % 10
**= Exponent Assignment a **= 10 # a = a ** 10
Comparison operators
Operator Meaning Example
== Is Equal To 3 == 5 gives us False
!= Not Equal To 3 != 5 gives us True
> Greater Than 3 > 5 gives us False
< Less Than 3 < 5 gives us True
>= Greater Than or Equal To 3 >= 5 give us False
<= Less Than or Equal To 3 <= 5 gives us True
Logical operators
Operator Example Meaning
Logical AND:
and a and b
True only if both the operands are True
Logical OR:
or a or b
True if at least one of the operands is True
Logical NOT:
not not a
True if the operand is False and vice-versa.
Identity operators
Operator Meaning Example
is True if the operands are identical (refer to the same object) x is True
is not
True if the operands are not identical (do not refer to the same x is not
Lab#03
object) True
Membership operators
Operator Meaning Example
in True if value/variable is found in the sequence 5 in x
not in True if value/variable is not found in the sequence 5 not in x
Bitwise operators
Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ 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)
OPERATOR PRECEDENCE
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise NOT
Multiplication, division, floor division, and
* / // %
modulus
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
Lab#03
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is not in not Comparisons, identity, and membership
in operators
Not Logical NOT
And AND
Or OR
input() function
the input() function allows user input.
Example:
print('Enter your name:')
x = input()
print('Hello, ' + x)
Output:
Hello sana
Example:
x = input('Enter your name:')
print('Hello, ' + x)
Exercise
1. Create a program that prompts the user to input two numeric values (an integer
and a float) and displays the data types of those values.
2. Create a program that prompts the user to input any number, checks whether it
is between 1 and 100 and display the result.
3. Create a program that prompts the user to input any number and check if it is
both positive and even.
4. Create a program that prompts the user to input any number and check if it is
either negative or zero
5. Create a program that prompts the user to input any number check if it is not a
positive number.