PYTHON FUNDAMENTAL
CHAPTER -IV (PART - 2)
CLASS – XI
Operators
Operators
can be defined as symbols that are used to perform operations on operands. In
other word, operators are tokens that trigger some computation when applied to
variables and other objects in an expression. Variables and objects to which
the computation is applied, are called operands.
Types of Operators
1.
Arithmetic Operators
2.
Relational Operators
3.
Assignment Operators
4. Bitwise
Operators
5. Logical
Operators
6.
Membership Operators
7. Identity
Operators
Operators |
Description |
Example |
+ |
Perform addition of two number |
x+y |
- |
Perform subtraction of two number |
x-y |
/ |
Perform division of two number |
x/y |
* |
Perform multiplication of two number |
x*y |
% |
Modules = return remainder |
x%y |
// |
Floor Division = remove digits after the decimal
point |
x//y |
** |
Exponent = perform raise to power |
x**y |
2. Relational Operators:
Relational operators
are used to compare the values.
Operators |
Description |
Example |
== |
Equal to, return true if x equal to y |
x == y |
!= |
Not equal, return true if x is not equals to y |
x ! = y |
> |
Greater than, return true if x is greater than y |
x > y |
>= |
Greater than or equal to, return true if x is greater
than y or x is equals to y |
x >= y |
< |
Less than, return true if x is less than y |
x < y |
<= |
Less than or equal to, return true if x is less than
y or x is equals to y |
x <= y |
3. Assignment Operators:
Used to assign values
to the variables.
Operators |
Description |
Example |
= |
Assigns calues from right side operands to left side
operand |
x=y |
+= |
Add 2 numbers and assigns the result to left
operand. |
x+=y |
/= |
Divide 2 numbers and assigns the result to left
operand. |
x/=y |
*= |
Multiply 2 numbers and assigns the result to left
operand. |
x*=y |
-= |
Subscrats 2 numbers and assigns the result to left
operand. |
x-=y |
%= |
Modules 2 numbers and assigns the result to left
operand. |
x%=y |
//= |
Perform floor division on two numbers and assigns
the result to left operand. |
x//=y |
**= |
Calculate power on operators and assigns the result
to left operand. |
x**=y |
4. Logical Operators:
It is performed logical
operations on the given two variables or values.
Operators |
Description |
Example |
and |
Return true if both conditions are true |
x and y |
or |
Return true if either or both conditions are true |
x or y |
not |
Reverse the condition |
Not(x>y) |
Example:
x = 5
y = 10
if(x==5 and
y==10):
print(“Hello
Python”)
Output
Hello
Python
5. Membership Operators:
The membership
operators in Python are used to validate whether a value is found within a
sequence such as strings, lists, or tuples.
Operators |
Description |
Example |
in |
Return true if value exists in sequence, else false. |
a in list |
not in |
Return true if value does not exists in sequence,
else false. |
a not in list |
a = 10
list = [10,
20, 30, 40, 50]
y = a in
list
z = a not
in list
print(y)
print(z)
Output:
True
False
Operators |
Description |
Example |
is |
Return true if variables point the same object, else
false. |
a is b |
is not |
Returns true if two variables point the different
object, else false. |
a is not b |
Example:
a = 15
b = 14
if(a is b):
print(“Both
a and b has same identity”)
else:
print(“a
and b has different identity”)
b = 75
if (a is
b):
print(“Both
a and b has same identity”)
else:
print(“a
and b has different identity”)
Output:
Both a and
b has same identity
a and b has
different identity
7. Bitwise Operators:
Operators |
Description |
Example |
& |
Bitwise AND |
a & b |
^ |
Bitwise exclusive OR (XOR) |
a ^ b |
| |
Bitwise OR |
a | b |
Punctuators: Punctuators are symbols that are used in
programming languages to organize sentence structures, and indicate the rhythm
and emphasis of expressions, statements, and program structure.
Most common punctuators are:
Barebones of a Python Program:
Now we are going to
talk about the basic structure of a Python program – what all it can contain.
Before we proceed, have a look at following sample code.
A Python program contain the following
components
a.
Expression
b.
Statement
c. Comments
d. Function
e. Block
& Indentation
a. Expression: Which is evaluated and produce
result. E.g. (20+5)/5
b. Statement: Instruction that does something.
E.g.
a =
25
print
(“Calling in proper sequence”)
c. Comments: Which is readable for programmer but
ignored by python interpreter.
1. Single line comment: Which
begins with # sign.
2. Multi line comment
(docstring): either write multiple line beginning with # sign or use
triple quoted multiple line. E.g.
‘’’this is my
First python
Multi line comment
‘’’
d. Function: A code that has some name and it can
reused. E.g. keyArgFunc in above program
e. Block & indentation:
Group of statements is
block. Indentation at same level create a block. E.g. all 3 statement of keyArgFunc function
VARIABLES: amed labels, whose values can be used and processed during program run,
are called Variables.
Assigning
Values To Variable
name =
‘Python’ # String Data Type
sum = None # variable without value
a = 35 # Integer
b = 5.5 # Float
sum = a + b
print(sum)
Multiple Assignment:
Assign a single value
to many variables like
a = b = c =
1 # single value to multiple
variable
a, b = 5, 6
# multiple value to multiple
variables
a, b = b, a =# value of a and b
is swaped
Variable Scope And Lifetime in Python Program
1. Local Variable
def fun():
x = 10
print (x)
fun()
print(x) #error will be shown
2. Global Variable
x = 15
def fun():
print(x) # Calling variable ‘x’ inside fun()
fun()
print(x) # Calling variable ‘x’ outside fun()
Dynamic
Typing
Data type
of a variable depend/change upon the value assigned to a variable on each next
statement.
X = 40 # integer type
X = ‘Hello’ # X variable data type change to
string on just next line
Now programmer should be aware that not to
write like this:
Y = X/5 #
error because String cannot be devided.
Input and
Output
print()
Function in Python is used to print output on the screen.
Syntax of
Print
print(expression/variable)
e.g.
print(95)
Output: 95
print(‘Hello
Python!’)
Output: Hello Python!
print(‘Informatics’,
‘Practices’)
print(‘Informatics’
‘Practices’, sep = ‘&’)
print(‘Informatics’
‘Practices’, sep = ‘&’, end=’.’)
Output:
Informatics
Practices
Informatics
& Practices
Informatics
& Practices.
No comments:
Post a Comment
Please do not any spam in the comment box.