Basic & Operator in Python
Basic & Operator in Python
Python Featues
=============
1. Easy
a. Easy to Learn
b. Easy to Code
a. Easy to Learn
==> Simple English
==> No Complex syntax
a. The statements not end with ;
b. no explict datatypes for creating variables
c. there is no { } for defining blocks
b. Easy to code
==> less coding
C python
= ======
//Swaping logic x=10
void main() y=20
{ x,y=y,x
int x=10,y=20;
int z;
z=x;
x=y;
y=z;
}
C Java Python
#include<stdio.h> class Test
void main() {
{ public static void main(String args[]) print("Hello World")
printf("Hello World"); {
} System.out.println("Hello World");
}}
Free ==> Python software Free to download, you can download this software from
www.python.org
What is variable?
variable-name=<value>
>>> n1=100
>>> n2=200
>>> n1
100
>>> n2
200
>>> id(n1)
1614851552
>>> id(n2)
1614853152
>>> n3=100
>>> id(n3)
1614851552
>>> n1=300
>>> id(n1)
59547536
type() : type() is predefined function in python. This function
return type of object hold by variable.
>>> type(n1)
<class 'int'>
>>> type(n2)
<class 'int'>
>>> type(n3)
<class 'int'>
>>> type(n4)
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
type(n4)
NameError: name 'n4' is not defined
immutable
>>> n1=999999999999999999999
>>> n1
999999999999999999999
>>> n1=256
>>> id(n1)
1614854048
>>> n2=256
>>> id(n2)
1614854048
>>> x=257
>>> y=257
>>> id(x)
59546480
>>> id(y)
59547616
>>>
float
===
>>> x=1.5
>>> type(x)
<class 'float'>
>>> y=15e-1
>>> y
1.5
>>> type(y)
<class 'float'>
>>>
The value of e is 10
N=15e-10 15x10 pow -1 1.5
>>> z=1.234567895678956789
>>> z
1.2345678956789568
complex
=======
1. real
2. imag
real+imagj
>>> type(c1)
<class 'complex'>
>>> comp1=1+2j
>>> comp1
(1+2j)
>>> type(comp1)
<class 'complex'>
>>> comp1.real
1.0
>>> comp1.imag
2.0
>>> comp1.real=1.5
comp1.real=1.5
>>> comp1.imag=2.5
comp1.imag=2.5
>>> comp3=3j
>>> type(comp3)
<class 'complex'>
>>> comp3
3j
>>> comp3.real
0.0
>>> comp3.imag
3.0
>>>
Boolean
======
>>> a=True
>>> b=False
>>> type(a)
<class 'bool'>
>>> type(b)
<class 'bool'>
String
=====
>>> str1='naresh'
>>> str2='python 3.8'
>>> type(str1)
<class 'str'>
>>> type(str2)
<class 'str'>
>>> str3='123'
>>> type(str3)
<class 'str'>
>>> str4='python is a programming language'
>>> str5='python is high level programming language
SyntaxError: EOL while scanning string literal
>>> str1
>>> str2
>>> str1='''python
is
programming
language'''
>>> print(str1)
python
is
programming
language
>>> str2="""python
is
high level
programming
langauge"""
>>> print(str2)
python
is
high level
programming
langauge
>>>
print()
=====
>>> print()
>>> print(100)
100
>>> print(100,200)
Changing sep:
===========
>>> print(100)
100
>>> print(100,200)
100 200
>>> print(100,200,300,400,500,600,700)
>>> print(100,200,300,400,500,600,700,sep=':')
100:200:300:400:500:600:700
>>> print(100,200,300,400,500,600,700,sep='\n')
100
200
300
400
500
600
700
>>> print(100,200,300,400,sep='\t')
>>> print(100,200,300,400,500,sep=',')
100,200,300,400,500
>>> print(100,sep=',')
100
Chaning end
==========
print(100)
print("Python")
print(200,end=':')
print("Python")
print()
print(100,200,"Python",sep=',',end=':')
print(300,400,"Django")
Output:
100
Python
200:Python
Changing file
==========
the default file is console.
we can use any other file in order write or print data.
print("Hello Python")
f=open("d:\\file1.txt","w")
print("Hello Python",file=f,flush=True)
Working in scripting mode or programming mode in IDLE (IDE)
Q: What is indentation?
Indentation is a space.
Every statement in python starts at 1st column
The statement should not start with space.
Space define a block, only blocked statements are given
space.
def myFun():
print("Hello")
print(myFun.__doc__)
print(print.__doc__)
print(input.__doc__)
print(int.__doc__)
n1=100
n2=200
n3=n1+n2
print(n1,n2,n3)
input()
======
Syntax:
input(prompt=None)
Ex1:
a=input()
b=input()
print(a)
print(b)
print(type(a))
print(type(b))
output:
100
200
100
200
<class 'str'>
<class 'str'>
Eg2:
a=input()
b=input("Enter b value")
print(a)
print(b)
print(type(a))
print(type(b))
Ex3:
n3=n1+n2
print(n1,n2,n3)
output:
Syntax1: int(str,base=10)
Syntax2: int(num)
Syntax1 convert string to integer. That string should contain the integer
with base 10(decimal number)
Ex1:
>>> n1=int(100)
>>> n2=int(1.5)
>>> print(n1,n2)
100 1
>>> n2=int(1.987)
>>> n2
Ex2:
>>> s1="65"
>>> n1=int(s1)
>>> print(type(s1),type(n1))
65 65
Ex3:
n1=int(a)
n2=int(b)
n3=n1+n2
print(n1,n2,n3)
c=a+b
print(a,b,c)
>>> n1=87
>>> n2="87"
>>> a=int(n2)
>>> print(n1,n2,a)
87 87 87
>>> n1=65
>>> n2=0o101
>>> print(n1,n2)
65 65
>>> print(n1,oct(n2))
65 0o101
>>> x="65"
>>> n3=int(x)
>>> y="0o101"
>>> n4=int(y)
n4=int(y)
>>> n4=int(y,base=8)
>>> n4
65
>>> print(n4,oct(n4))
65 0o101
>>> n5=26
>>> n6=0x1a
>>> print(n5,n6)
26 26
>>> n7="0x1a"
>>> n8="26"
>>> x=int(n8)
>>> y=int(n7)
y=int(n7)
>>> y=int(n7,base=16)
>>> print(x,y)
26 26
>>> print(x,hex(y))
26 0x1a
>>> b1=9
>>> b2=0b1001
>>> print(b1,b2)
99
>>> print(b1,bin(b2))
9 0b1001
>>> b3="0b1001"
>>> b4=int(b3)
b4=int(b3)
>>> b4=int(b3,base=2)
>>> print(b1,hex(b4))
9 0x9
>>> print(b1,bin(b4))
9 0b1001
>>>
# write a program or script to add two binary numbers
n1=int(b1,base=2)
n2=int(b2,base=2)
n3=n1+n2
print(bin(n1),bin(n2),bin(n3))
Ouput:
r=float(input("Enter R"))
area=3.147*r*r
print("area of circle is ",area)
s1="1.5"
f1=float(s1)
s2="12"
f2=float(s2)
s3="15e-1"
f3=float(s3)
s4="1.5e0"
f4=float(s4)
print(f1,f2,f3,f4)
i1=12
f5=float(i1)
print(f5)
# write a program to find area of triangle
complex()
========
s1="1+2j"
c1=complex(s1)
print(type(s1),type(c1))
print(s1,c1)
comp3=comp1+comp2
print(comp1,comp2,comp3)
real1=1.5
img1=2.5
c1=complex(real1,img1)
real2=1.5
img2=1.2
c2=complex(real2,img2)
print(c1,c2)
bool()
>>> x="True"
>>> b=bool(x)
>>> type(x)
<class 'str'>
>>> type(b)
<class 'bool'>
>>> n1=1
>>> b=bool(n1)
>>> type(n1)
<class 'int'>
>>> type(b)
<class 'bool'>
>>> n1
>>> b
True
>>> bool(True)
True
>>> bool(1)
True
>>> bool(0)
False
>>> bool(100)
True
>>> bool(200)
True
>>> bool(0)
False
>>> bool("False")
True
str()
s1=str(100)
s2=str(1.5)
s3=str(1+2j)
s4=str(True)
print(s1,s2,s3,s4)
name=input("Enter name")
sub1=int(input("Enter sub1"))
sub2=int(input("Enter sub2"))
sub3=int(input("Enter sub3"))
total=sub1+sub2+sub3
avg=total/3
print(rno,name,sub1,sub2,sub3,total,avg)
PYTHON OPERATOR
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
8. Conditional Operators
>>> a=1.5
>>> b=1.2
>>> a+b
2.7
>>> a.__add__(b)
2.7
>>>
1. Arithmetic Operators
These operators are used to perform arithmetic operations.
These are binary operators, which operate on two operands.
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Float division
// Floor division
% Modular
** Power of Operator or expo operator
Always operators are eval based on the precedence of operators.
+ operator is used to perform two operations.
1. Adding numbers
2. Concat sequence
+ operator is used to perform two operations.
1. Adding numbers
2. Concatenating sequences (String, List, Tuple,..)
# adding numbers
res1=100+200
res2=1.5+2.2
res3=(1+2j)+(1+3j)
print(res1)
print(res2)
print(res3)
# concatenation of strings
str1="python"
str2="java"
str3=str1+str2
print(str1,str2,str3)
str3=str1+str(3.9)
print(str3)
res1=10+20 # int+int=int
res2=10+1.5 # int+float=float
res3=10+(1+2j) # int+complex=complex
print(res1,res2,res3)
res4=1+2.4+(1+2j)
print(res4)
res1=10+2.5
print(res1)
res2=int(10+2.5)
print(res2)
print(type(res1),type(res2))
res1=100-200
res2=1.5-1.2
res3=(2+1j)-(1+0j)
print(res1,res2,res3)
n2=1.2*1.3 # float*float=float
n3=(1+2j)*(1+1j) # complex*complex=complex
print(n1,n2,n3)
str1="python"
print(str1)
print(str2)
print("*"*40)
print("-"*40)
print(res1,res2,res3,res4)
print(type(res1),type(res2),type(res3),type(res4))
print(res5)
res1=5/0
print(res1)
res1=0/2
print(res1)
res2=0.0/0.0
print(res2)
print(res1)
print(res2)
print(res3)
print(type(res1),type(res2),type(res3),type(res4))
res1=5%2
res2=5%3
print(res1)
print(res2)
res3=4%2
print(res3)
res1=2.5%2
print(res1)
a=3**2
print(a)
b=3**-2
print(b)
Relational Operators
Operator Meaning
> Greater than
< Less than
>= Greater than equal
<= Less than or equal
!= Not Equal
== Equal
b1=100>200
b2=200>100
b3=100<200
b4=200<100
b5=100>=100
b6=200<=200
b7=100>=101
b8=200<=198
print(b1,b2,b3,b4,b5,b6,b7,b8)
b9='A'=='A' # 65==65
b10='A'<'B' # 65<66
print(b9,b10)
b11="abc"=="abc"
b12="abc"=="ABC"
print(b11,b12)
Logical Operators
Operators Meaning
and Logical and operator
or Logical or operator
not Logical not operator
sub1=70
sub2=80
sub3=90
print(b1,b2)
b3=True or False
b4=True and True
print(b3,b4,b5,b6)
b7=1 and 0
print(b7)
print(b8)
print(b9,b10)
b11=100 or 200
b12=0 or 200
print(b11,b12)
b1=True
b2=not b1
b3=not b2
print(b1,b2,b3)
Bitwise Operators
Operator Meaning
>> Bitwise Right shift operator
<< Bitwise Left shift operator
& Bitwise and operator
| Bitwise or operator
~ Bitwise not operator
^ Bitwise XOR operator
Syntax:
Opr>>n
n1=12
n2=n1>>1
print(n1)
print(n2)
print(bin(n1))
print(bin(n2))
n3=n1>>2
print(n3)
print(bin(n3))
Syntax:
Opr<<n
Formula : num*2 pow n 15*2 pow 2 15*4 60
n1=15
n2=n1<<2
print(n1)
print(n2)
print(bin(n1))
print(bin(n2))
Bitwise &(and) ,| (or) ,^ (XOR)
a=5
b=4
c=a&b
print(a,b,c)
print(bin(a),bin(b),bin(c))
x=10
y=5
z=x|y
print(x,y,z)
print(bin(x),bin(y),bin(z))
p=10
q=4
r=p^q
print(p,q,r)
print(bin(p),bin(q),bin(r))
Identity Operators
list1=[10,20,30]
list2=[10,20,30]
print(id(list1))
print(id(list2))
print(list1 is list2)
print(list1==list2)
list3=list1
print(id(list1))
print(id(list3))
print(list1 is list3)
Membership Operators
a=10
b=5
e=(c:=a+b)*(d:=a-b)
print(a,b,c,d,e)
Conditional Operator Or Conditional Expression
Operator Description
:= Assignment expression
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
in, not in, is, is not, <, <=, >, >=, Comparisons, including membership tests and
!=, == identity tests
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<, >> Shifts
+, - Addition and subtraction
Multiplication, matrix multiplication, division,
*, @, /, //, %
floor division, remainder
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation
await x Await expression
x[index], x[index:index],
Subscription, slicing, call, attribute reference
x(arguments...), x.attribute
(expressions...),
Binding or parenthesized expression, list
[expressions...], {key: value...}, display, dictionary display, set display
{expressions...}
5 4 ^
12O - O =12.O
X= 4 2 << 1
b 2
4) ° (s
9 3
>7