P2S2 LabExperiment1
P2S2 LabExperiment1
Learning objectives:
We will see the different ways to develop a python program/application are
1. Interactive Mode:
Python comes with an interactive interpreter. Typically the interactive mode is
used to test the features of the python, or to run a smaller script that may not be reusable.
Now you can type any valid python expression at the prompt.
Python reads the typed expression, evaluates it and prints the result
2. Running Python scripts in Command Prompt:
We can write a group of python statements in any one of the following editors or IDEs
Editors:
Notepad
Notepad++
editPlus
nano
gedit
IDLE
Etc..
IDEs :
Pycharm
IDLE
Eric
Eclipse
Netbeans Etc..
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
Python language supports the following types of operators.
1.Arithmetic Operators
2.Comparison Relational Operators
3.Assignment Operators
4.Logical Operators
5.Bitwise Operators
6.Membership Operators
7.Identity Operators
Output:
3. Assignment Operators:
We can use assignment operator to assign value to the variable
4. Bitwise Operators :
Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and
b = 13; Now in binary format they will be as follows –
a = 0011 1100
b = 0000 1101
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
Example:
print(4&5) ==>4
print(4|5) ==>5
print(4^5) ==>1
5. Logical Operators:
Following are the various logical operators used in Python.
And
Or
not
You can apply these operators for boolean types and non-boolean types, but the behavior
is different.
For boolean types behaviour:
and ==>If both arguments are True then only result is
True or ====>If atleast one argument is True then result is
True not ==> complement
True and False
==>False True or False
===>True not False
==>True
Eg:
Output:
6. Membership Operators:
Python’s membership operators test for membership in a sequence, such as strings, lists, or
tuples. There are two membership operators as explained below
7. Ternary Operator (or) Conditional Operator:
1. If the operator operates on only one operand, we will call such operator as
unary operator. For eg:, ~a.
3. If the operator operates on Three operands, we will call such operator as Ternary
operator.
Syntax:
x = firstValue if condition else secondValue
If condition is True then firstValue will be considered else secondValue will be considered