0% found this document useful (0 votes)
138 views

Python Notes

The document discusses various Python operators like arithmetic, relational, logical, assignment, bitwise and membership operators. It explains operators like addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), power (**) and provides examples of using each operator on numbers, strings, lists, tuples etc. It also discusses conversion functions like int(), float(), complex(), bool() and provides examples of converting between data types using these functions.

Uploaded by

Priyanshi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
138 views

Python Notes

The document discusses various Python operators like arithmetic, relational, logical, assignment, bitwise and membership operators. It explains operators like addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), power (**) and provides examples of using each operator on numbers, strings, lists, tuples etc. It also discusses conversion functions like int(), float(), complex(), bool() and provides examples of converting between data types using these functions.

Uploaded by

Priyanshi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Spetember

Tuesday

Subtitle

Title
To get started right away, just click any
placeholder text (such as this) and start typing
to replace it with your own. Want to insert a
picture from your files or add a shape? You got
it! On the Insert tab of the ribbon, just tap the
option you need.

Sunday Monday Tuesday Wednesda Thursday Friday Saturday


y

Tuesday Tuesday Tuesday Tuesday01 Tuesday Tuesday Tuesday


01 01 01 01 01

1 1 1 1 1 1 1

1 1 1 1 1 1 1

1 1 1 1 1 1 1
00311 00311 00311 00311 00311 00311 00311

00311 00311

View and edit this document in Word on your computer, tablet, or phone. You can
edit text; easily insert content such as pictures, shapes, and tables; and seamlessly
save the document to the cloud from Word on your Windows, Mac, Android, or iOS
device.
Python Notes Day/Datewise:

1st September
input and output statements or functions
=================================

==> Every program required 3 things.


1. input
2. process
3. output

==> input is data or information given to the program. in input data is flow inside
the program. input is given to the program from different
source(keyboard,file,database,...).
==> process is performing some operations.
==> output is result, this output is displayed on monitor,file,database or any dest.

eg:

# write a program to add two numbers


num1=100
num2=200
res=num1+num2
print(res)

# ==> is used to define comment. comments are ignored by python trnaslator.


this single comment.
''' ''' ==> this is used for multiline comment or document string.

print() function
============

==> print is a predefined function in python.


==> this function is used to print data on stdout(standard output device). default
output is monitor.
==> print function is used to print zero or more values.
==> after printing insert newline.
==> when print function print more than one value it uses defualt sep as space.

print() # insert new line


print(100)
print(200,300)
print("NARESHIT","PYTHON",3.9)
print()
print("Python Langauge")

==> print function available in builtins module. this is default module imported by
any python program.

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

print(100,200,300)
print(100,200,300,sep=',')
print(101,"Naresh","Python",4000.0,sep=":")

What is output?

1. print(100,200,300,sep='\n')
100
200
300

2. print(100,200,300,sep='$')

100$200$300

3. print("Python",sep="%")

Python

4. print(100,200,300,end='$')
print("Python")

100 200 300$python

eg:

f=open("d:\\file1","w")
print("Hello Python")
print("Hello Python",file=f,flush=True)

input()
=====
==> input() is predefined function in python.
==> this function is available in builtins module.
==> this function is used to read data from keyboard at runtime.
==> this function return input value as string type.
==> it read only one value.

syntax: input([prompt])

eg:

a=input()
b=input()
c=input()
d=a+b+c
print(a,b,c,d)
print(type(a),type(b),type(c),type(d))

eg:

name=input("Enter your Name :")


fname=input("Entre your father name :")
print(name)
print(fname)

int()
===

==> this function is used to create an integer object using int value,float value and
integer value represented as string.
==> int is conversion function.

syntax-1: int(value)
syntax-2: int(value,base=10)

2nd September
nt()
===

==> this function is used to create an integer object using int value,float value and
integer value represented as string.
==> int is conversion function.

syntax-1: int(value)
syntax-2: int(value,base=10)

syntax-1 : this syntax 1 is used to convert float to int


and int to int
x=1.5
y=int(x) ==> this convert float to int
eg:
>>> x=1.5
>>> y=int(x)
>>> x
1.5
>>> y
1
>>>
=====================================
a=10
b=int(a) ==> creating integer using existin integer
=============================================

syntax-2 : this syntax is used to convert string representation of int to int


default base of the string representation of int is 10.

s1="65"
x=int(s1)
s2="0o45"
y=int(s2) ==> Error
y=int(s2,base=8)

s3="0xab"
z=int(s3) ==> Error
z=int(s3,base=16)
s4="0b101"
p=int(s4) ==> Error
p=int(s4,base=2)

# find output

x="65"
y=int(x,base=8)
print(x,y)

# Find output
s3="65"
a=int(s3)
b=int(s3,base=8)
c=int(s3,base=16)
#d=int(s3,base=2)
print(a,b,c)

eg:

# write a program to add two numbers

x=input("Enter first number")


y=input("Enter second number")
n1=int(x)
n2=int(y)
n3=n1+n2
print(n1,n2,n3)
print(x,y)
print(type(x),type(y))
z=x+y
print(x,y,z)

# write a program to add two numbers

x=int(input("Enter first number"))


y=int(input("Enter second number"))
z=x+y
print(x,y,z)

# write a program to swap two numbers

n1=int(input("Enter first number"))


n2=int(input("Enter second number"))
#n3=n1
#n1=n2
#n2=n3
n1,n2=n2,n1
print(n1,n2)
# int function which is used to convert boolean to int

n1=int(True)
n2=int(False)
print(n1,n2)

float()
=====

==> this function is used to convert int to float,float to float and string to float.

float(value)
==> value can be int,float or float value represented as string.

eg:

a=float(15)
b=float(1.5)
c=float("17.65")
print(a,b,c)

eg:

a="1.5"
b="15e-1"
x=float(a)
y=float(b)
print(a,b)
print(x,y)
print(type(a),type(b))
print(type(x),type(y))

eg:

# write a program/script to find area of triangle

base=float(input("Enter base"))
height=float(input("Enter height"))
area=0.5*base*height
print(base,height,area)

complex()
========

==> this function to construct complex number.

c1=complex("1+2j") ==> converting string to complex


c2=complex(real=1.5,imag=2.5) ==> using real and image as float
c3=complex(real=1,imag=2) ==> using real and imag as int
c4=complex(2.5) ==> creating with real value imag is default 0
c5=complex(imag=1.5) ==> creating with imag with real default 0
print(c1,c2,c3,c4,c5,sep='\n')

# write a program to add two complex numbers

comp1=complex(input("Enter complex number"))


comp2=complex(input("Enter complex number"))
comp3=comp1+comp2
print(comp1,comp2,comp3)

bool()
=====
==> it is used to convert int to boolean

x=bool("True")
y=bool("False")
print(x,y) ==> True,True
z=bool("0")
print(z) ==> True
a=bool("nit")
print(a) ==> True
a=bool(0)
b=bool(1)
print(a,b) ==> False,True

Operators
=========

Q: What is operator?

==> operator special symbol which is used to perform operations.


==> based on the operands on which it perform operations, the operators are divided
into 3 categories.

1. unary operator ==> which operates on one operands


2. binary operator ==> which operates on two operands
3. ternary operator ==> which operates on three operands

Types of operators

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

Arithmetic Operators
=================

==> These operators are used to perform arithmetic operations.

+ Add
- sub
* multiply
/ float division
// floor division
% modulas
** expo/pow of operator

+
=
In python + operator is used to perform two operations.
1. adding numbers
2. concat strings
=> if two operands are numbers it perform addition
=> if two operands are strings it perform concat

eg:

res1=100+200
res2=1.4+2.4
res3=1+2j+1+3j
res4="nit"+"Python"
res5="15"+"25"
res6="1.5"+"2.5"
print(res1,res2,res3,res4,res5,res6,sep='\n')

output:

300
3.8
(2+5j)
nitPython
1525
1.52.5

3rd September
==> every operator in python is bind with function.

int class ==> datatype


float class ==> datatype
complex class ==> datatype

>>> n1=10
>>> n2=20
>>> n3=n1.__add__(n2)
>>> print(n1,n2,n3)
10 20 30
>>> n4=n1+n2
>>> n4
30

- arithmetic subtract operator

==> this operator is used only with numbers.

n1=100
n2=50
n3=n1-n2
f1=2.5
f2=1.2
f3=f1-f2
c1=1+2j
c2=2+5j
c3=c1-c2
print(n1,n2,n3)
print(f1,f2,f3)
print(c1,c2,c3)

Find Output?

x=True+True
y=True+False
z=False-True
print(x)
print(y)
print(z)

output : 2 1 -1

Find output?

z=True+10
x=10-True
print(x)
print(z)
Output: 11 9

* ==> multiplication operator and repeate operator

==> it is used as multiply operator on numbers.


==> it is used as repeate operator with string or sequences(string,list,tuple)

n1=10
n2=5
n3=n1*n2
f1=1.5
f2=2.3
f3=f1*f2
print(n1,n2,n3)
print(f1,f2,f3)
====================================
str1="python"
str2=str1*3
print(str1)
print(str2)
list1=[10,20,30,40,50]
list2=list1*2
print(list1)
print(list2)

/ division operator or float division operator

==> this operator perform division operation and return result in float type.
int/int=float
float/float=float
int/float=float
float/int=float

x=5
y=2
z=x/y
print(x,y,z)
n1=4
n2=2
n3=n1/n2
print(n1,n2,n3)
p=4.5
q=2
r=p/q
print(p,q,r)

// floor division

==> this operator perform division and return result as int type.

int//int ==> int


int//float ==> float
float//int ==> float

10+1.5 ==> 11.5


10+20 ==> 30
10+1.5+5 ==> 16.5

n1=5
n2=2
n3=n1//n2
print(n1,n2,n3)

a=5
b=2.0
c=a//b
print(a,b,c)

x=9
y=4
z=x//y
print(x,y,z)
p=x/y
print(x,y,p)
m=9.0
n=4
o=m//n
print(m,n,o)

5/2=2.5
5//2=2
5//2.0=2.0

% modulas operator, this operator returns remainder

5%2=1
4%2=0
9%3=0
9%2=1

** ==> power of operator or expo operator

num**p

res1=5**2
res2=6**2
print(res1,res2)

===========================================================
==========

# write a program to read rno,name,3 subject marks


# calculate total and avg

rno=int(input("Enter Rollno"))
name=input("Enter name")
sub1=int(input("Enter subject1"))
sub2=int(input("Enter subject2"))
sub3=int(input("Enter subject3"))
total=sub1+sub2+sub3
avg1=total/3
avg2=total//3
print("*"*40)
print("Rollno ",rno)
print("Name ",name)
print("Subject1 ",sub1)
print("Subject2 ",sub2)
print("Subject3 ",sub3)
print("*"*40)
print("Total ",total)
print("Avg ",avg1,avg2)
print("*"*40)
===========================================================
=======
temp conversion

# temp conversion Celsius to Fahrenheit


# f=(c*9/5)+32

c=float(input("Enter temp in Celsius"))


f=(c*9/5)+32
print(f)

c=(f-32)*5/9
print(c)

+ - * / // % **
===========================================================
===========

Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.

4th September
Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.

> greater than


< less than
>= greater than or equal
<= less than or equal
!= not equal
== equal/compare

==> these operators return boolean values(True/False).

a=10
b=5
c=a>b
d=a<b
print(a,b,c,d)
e=b<a
print(e)
x=100
y=100
z=x>y
print(x,y,z)
p=x>=y
print(p)
================================================
a=100
b=200
c=100
d=a==b
e=a==c
print(a,b,c,d,e)
=================================================

Logical operator

==> Logical operators are used to combine two conditions or boolean expressions.
==> Logical operators are represented using three keywords.

1. and
2. or
3. not

truth table of and operator


======================
opr1 opr2 opr1 and opr2

True False False


True True True
False True False
False False False

a=True and True


b=True and False
c=False and True
d=False and False
print(a,b,c,d)

e=100>50 and 200>50


print(e)
f=100>50 and 200<50
print(f)
g=100<50 and 200>50
print(g)

h=True and True and True


i=True and False and True
print(h,i)
========================================================

truth table of or operator

opr1 opr2 opr1 or opr2

True False True


False True True
True True True
False False False

eg:

a=True or False
b=False or True
c=True or True
d=False or False
print(a,b,c,d)

a=10>20 or 20>10
print(a)
b=20>10 or 10>20
print(b)

not operator
==========
truth table of not operator

opr1 not opr1


True False
False True

x=not True
y=not False
exists=True
z=not exists
exists=False
p=not exists
print(p)
lock=True
l1=not lock
lock=False
l2=not lock
print(l1,l2)
===========================================================
Bitwise operator
==============

==> bitwise operators are used to perform operations on bits.

shift operators

<< ==> left shift operator


>> ==> right shift operator

leftshift operator is used to add bits at right side by shift bits towards leftside. this
allows to increment value by adding bits.

right shift operator is used to remove bits at right side by shifting bits towards
rightside. this allows to decrement value by removing bits.

x=12
y=x>>1
print(x,y)
z=x<<1
print(x,z)
b1=0b1010
b2=b1<<2
print(b1,b2)
print(bin(b1),bin(b2))

b3=0b1010
b4=b3>>1
print(b3,b4)
print(bin(b3),bin(b4))

right shift ==> num/2 pow n


left shift ==> num * 2 pow n

num=5
x=num>>2
bitwise & (AND)
bitwise | (OR)
bitwise ^ (XOR)
bitwise ~ (complement)

==> These operators used to apply logical gates.

bitwise & (AND) operator


====================

truth table

opr1 opr2 opr1 & opr2

1 0 0
0 1 0
1 1 1
0 0 0

x=5 ==> 1 0 1
y=5 ==> 1 0 1
z=x&y ==> 1 0 1
print(x,y,z)

x=5 ==> 1 0 1
y=6 ==> 1 1 0
z=x&y ==> 1 0 0
print(x,y,z) ==> 5 6 4

bitwise | (or) operator


=================

truth table of bitwise or operator

opr1 opr2 opr1 | opr2

1 0 1
0 1 1
1 1 1
0 0 0

x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x|y ==> 1 0 1

print(x,y,z) ==> 5 4 5

Relational Operators
=================
==> Relational operators are used to find relation between two operands.
==> Relational operators are binary operators, which required two opeands.

> greater than


< less than
>= greater than or equal
<= less than or equal
!= not equal
== equal/compare

==> these operators return boolean values(True/False).

a=10
b=5
c=a>b
d=a<b
print(a,b,c,d)
e=b<a
print(e)
x=100
y=100
z=x>y
print(x,y,z)
p=x>=y
print(p)
================================================
a=100
b=200
c=100
d=a==b
e=a==c
print(a,b,c,d,e)
=================================================

Logical operator

==> Logical operators are used to combine two conditions or boolean expressions.
==> Logical operators are represented using three keywords.

1. and
2. or
3. not

truth table of and operator


======================
opr1 opr2 opr1 and opr2

True False False


True True True
False True False
False False False

a=True and True


b=True and False
c=False and True
d=False and False
print(a,b,c,d)

e=100>50 and 200>50


print(e)
f=100>50 and 200<50
print(f)
g=100<50 and 200>50
print(g)

h=True and True and True


i=True and False and True
print(h,i)
========================================================

truth table of or operator

opr1 opr2 opr1 or opr2

True False True


False True True
True True True
False False False

eg:

a=True or False
b=False or True
c=True or True
d=False or False
print(a,b,c,d)

a=10>20 or 20>10
print(a)
b=20>10 or 10>20
print(b)

not operator
==========
truth table of not operator

opr1 not opr1


True False
False True

x=not True
y=not False
exists=True
z=not exists
exists=False
p=not exists
print(p)
lock=True
l1=not lock
lock=False
l2=not lock
print(l1,l2)
===========================================================

Bitwise operator
==============

==> bitwise operators are used to perform operations on bits.

shift operators

<< ==> left shift operator


>> ==> right shift operator

leftshift operator is used to add bits at right side by shift bits towards leftside. this
allows to increment value by adding bits.

right shift operator is used to remove bits at right side by shifting bits towards
rightside. this allows to decrement value by removing bits.

x=12
y=x>>1
print(x,y)
z=x<<1
print(x,z)
b1=0b1010
b2=b1<<2
print(b1,b2)
print(bin(b1),bin(b2))

b3=0b1010
b4=b3>>1
print(b3,b4)
print(bin(b3),bin(b4))

right shift ==> num/2 pow n


left shift ==> num * 2 pow n

num=5
x=num>>2
bitwise & (AND)
bitwise | (OR)
bitwise ^ (XOR)
bitwise ~ (complement)

==> These operators used to apply logical gates.

bitwise & (AND) operator


====================

truth table

opr1 opr2 opr1 & opr2

1 0 0
0 1 0
1 1 1
0 0 0

x=5 ==> 1 0 1
y=5 ==> 1 0 1
z=x&y ==> 1 0 1
print(x,y,z)

x=5 ==> 1 0 1
y=6 ==> 1 1 0
z=x&y ==> 1 0 0
print(x,y,z) ==> 5 6 4

bitwise | (or) operator


=================

truth table of bitwise or operator

opr1 opr2 opr1 | opr2

1 0 1
0 1 1
1 1 1
0 0 0

x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x|y ==> 1 0 1

print(x,y,z) ==> 5 4 5

bitwise xor(^) operator

truth table of xor operator

opr1 opr2 opr1^opr2

0 0 0
0 1 1
1 0 1
1 1 0

x=5 ==> 1 0 1
y=4 ==> 1 0 0
z=x^y ==> 0 0 1
print(x,y,z) ==> 5 4 1

Assignment Operators (OR) Augmented assignment


===========================================

Augmented assignment is the combination, in a single statement, of a binary


operation and an assignment statement.

operator
+=
-=
*=
/=
//=
%=
**=
>>=
<<=
&=
|=
^=

x=100
x=x+1 ==> x+=1 ==> 101

y=200
y=y+20 ==> 220
y+=20 ==> y=y+20 ==> 220

a,b,c,d,e,f,g,h=10,20,30,40,50,60,70,80
print(a,b,c,d,e,f,g,h)
a+=5
b-=5
c*=5
d/=5 # float division
e//=5 # floor division
f%=5
g**=5
h>>=2
print(a,b,c,d,e,f,g,h)

output:

10 20 30 40 50 60 70 80
15 15 150 8.0 10 0 1680700000 20

identity operators
===============

==> identity operators are used to compare identity/address of objects.


==> there are two identity operators.
1. is
2. is not

list1=[10,20,30,40]
list2=[10,20,30,40]
t=list1==list2
print(t)
print(id(list1))
print(id(list2))
t1=list1 is list2
print(t1)
list3=list1
t2=list1 is list3
print(t2)
t3=list1 is not list3
print(t3)
t4=list1 is not list2
print(t4)

member ship operators


===================

==> membership operators are used to verify/check/search given member exists


inside iterable.
==> membership operators are two.
1. in
2. not in

list1=['java','python','.net','c','oracle']
t1="python" in list1
t2="aws" in list1
t3="oracle" in list1
print(t1,t2,t3)
t4="python" not in list1
t5="aws" not in list1
print(t4,t5)

Operator precedence
==================

==> order of executing operators in a given expression is called operator precedence.


==> in a given expression all operators are having same precedence the eval is done
from left to right.
==> The below table define operator precedence for least to highest

Operator Description

:= Assignment expression (python 3.8)


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
[5]
+x, -x, ~x Positive, negative, bitwise NOT
** Exponentiation
await x Await expression
x[index], x[index:index], x(arguments...), x.attribute Subscription, slicing, call,
attribute reference

(expressions...),
[expressions...], {key: value...}, {expressions...}

Binding or parenthesized expression, list display, dictionary display, set display

ex1=> 10+20-5 ==> 30-5==> 25


ex2=> 10-5*2 ==> 10-10 ==> 0
ex3=> 5+2*5-2 ==> 5+10-2 ==> 15-2 ==> 13
ex3=> x=5
y=2
z=x>>2+5 ==> z=x>>7

if-else operator or statement


========================

==> if..else is called ternary operator.

syntax:

opr1 if test else opr2

==> if test condition is True it eval opr1


==> if test condition is False it eval opr2

x=5
y=2
z=x if x>y else y

7th September
# write a program to find given number is even or odd

num=int(input("Enter any number"))


print("even") if num%2==0 else print("odd")

# write a program to find a person is elg to vote

name=input("Enter name")
age=int(input("Enter age"))
print(name,"is elg") if age>=18 else print(name,"is not elg")

#opr1 if opr2 else opr3

# if opr2 is True, it eval opr1


# if opr2 is False, it eval opr3

:= Assignment expression (python 3.8) or walrus operator

x=10
y=20
z=x+y
print(x,y,z)
z=(x:=100)+y
print(z)
print(x)

===========================================================
=
a=100
b=50
c=200
d=100

'''e=a+b
f=c-d
g=e-f '''

g=(e:=a+b)-(f:=c-d)
print(e,f,g)

=====================================================
x=10+5*5-2 # 10+25-2 ==> 35-2 ==> 33
y=5-2+3-1 # 3+3-1 ==> 6 -1 ==> 5
z=10+(a:=5+2)
print(x,y,z,a)

x=0b1010
b1=x>>2+5 # x>>7 # 0b1010>7 # 0
print(b1)
b2=x<<5-3 # x<<2 # 0b1010<<2 # 0b101000
print(b2)

n1=100
n2=50
n3=60
b1=n1>n2 and n1>n3 or n2>n3 and n2>n1
# True and True
# True or n2>n3 and n2>n1
# True or False # True

print(b1)
==========================================================

string formatting
==============

==> string formatting is useful for foratting result or output.


==> python provide 3 types of string formatting

1. old style string formatting using %


2. new-style of string formatting using format function
3. f-string ==> it is introduced in python 3.8

1. old style string formatting using %

==> In this, string is formatted with sufix with %


==> within string, formatting field or replacement field is defined using formatting
specifier %s

"string %s "%(variable1,variable2,variable3,..)

n1=int(input("Enter first number"))# 100


n2=int(input("Enter second number")) # 50
n3=n1+n2
n4=n1-n2
n5=n1*n2
print("sum of %s and %s is %s"%(n1,n2,n3))
print("diff of %s and %s is %s"%(n1,n2,n4))
print("product of %s and %s is %s"%(n1,n2,n5))
print("%d"%(n1))
print("%f"%(n1))

# %d => decimal integer


# %f ==> float
# %s ==> string

===========================================================
======
# find simple intrest

p=float(input("Enter amount"))
r=float(input("Enter rate"))
t=int(input("Enter time"))
si=(p*t*r)/100
print("Amount=%f,Rate=%f,Time=%d,SimpleIntrest=%f"%(p,r,t,si))
print("Amount=%.2f,Rate=%.2f,Time=%d,SimpleIntrest=%.2f"%(p,r,t,si))

===========================================================
========
print("%d%d%d%d"%(100,200,300,400))
print("name is %s age is %d"%("naresh",50))

8th September
string format method
=================

str.format()

Perform a string formatting operation.


The string on which this method is called can contain literal text or replacement fields
delimited by braces {}. Each replacement field contains either the numeric index of a
positional argument, or the name of a keyword argument. Returns a copy of the
string where each replacement field is replaced with the string value of the
corresponding argument.

"{},{}".format(100,200)
===========================================================
==
a=100
b=200
print("{},{},{}".format(a,b,a+b))
print("{0},{1},{2}".format(a,b,a-b))
===========================================================
==========
# write a program to find area of circle

r=float(input("Enter r"))
area=3.147*r*r
print("area of cirlce is {} with r value {}".format(area,r))
===========================================================
=========
rollno=int(input("Enter rollno"))
name=input("Enter name")
sub1=int(input("Enter subject1"))
sub2=int(input("Enter subject2"))
sub3=int(input("Enter subject3"))
print("Rollno {}".format(rollno))
print("Name {}".format(name))
print("Subject1 {0} : Subject2 {1} : Subject3 {2}".format(sub1,sub2,sub3))
print("Total {t} Avg {a}".format(t=sub1+sub2+sub3,a=(sub1+sub2+sub3)/2))
print("Result is {r}".format(r="Pass" if sub1>40 and sub2>40 and sub3>40 else
"Fail"))
===========================================================
==========

formatting characters
==================
d ==> decimal integer
o ==> octal integer
x ==> hexa decimal integer
f ==> float in fixed
e ==> float in expo
b ==> binary integer

{1 : 2 }

formatting field is divided into two parts. this is sep with :


1. field index/name
2. formatting type

=========================================================
a=65
print("{:d},{:o},{:x},{:b}".format(a,a,a,a))
print("{0:d},{1:o},{2:x},{3:b}".format(a,a,a,a))
print("{p:d},{q:o},{r:x},{s:b}".format(p=a,q=a,r=a,s=a))
==========================================================
a=10
b=100
c=1000
d=10000
e=100000
print(a)
print(b)
print(c)
print(d)
print(e)
print("{:8d}".format(a))
print("{:8d}".format(b))
print("{:8d}".format(c))
print("{:8d}".format(d))
print("{:8d}".format(e))
===========================================================
a=1.56
b=23.434
c=3.567
d=.78
print(a)
print(b)
print(c)
print(d)
print("{:8.2f}".format(a))
print("{:8.2f}".format(b))
print("{:8.2f}".format(c))
print("{:8.2f}".format(d))
===========================================================
==
Alignment characters

> ==> right align


< ==> left align
^ ==> center align

a,b,c=100,200,300
p,q,r=10,20,30
m,n,o=1,2,3
print(a,b,c)
print(p,q,r)
print(m,n,o)

print("{:5d}{:5d}{:5d}".format(a,b,c))
print("{:5d}{:5d}{:5d}".format(p,q,r))
print("{:5d}{:5d}{:5d}".format(m,n,o))

print("{:5d}{:5d}{:5d}".format(a,b,c))
print("{:5d}{:5d}{:5d}".format(p,q,r))
print("{:5d}{:5d}{:5d}".format(m,n,o))
9th September
f-string or string interpolation
========================

==> f-string is called formatted string.


==> this string is prefix with f.
==> inorder to created formatted string, we represent string prefix with 'f' followed
string which contain replacement fields/expression.
==> these fields are represented with name/variablename or expression.

1st approch ==> "%s,%s"%(value,value)


2nd approch ==> "{},{},{}".format(value,value)
3rd approch ==> f'{var},{var},{expression}'

#finding simple intrest

amt=float(input("Enter amount"))
t=int(input("Enter time"))
rate=float(input("Enter rate"))
si=amt*t*rate/100
print(f'Amt={amt},Time={t},Rate={rate},SimpleIntrest={si}')

output:
Enter amount5000
Enter time12
Enter rate2.5
Amt=5000.0,Time=12,Rate=2.5,SimpleIntrest=1500.0
===========================================================
========
n1=50
n2=7
print(f'sum of {n1} and {n2} is {n1+n2}')
print(f'diff of {n1} and {n2} is {n1-n2}')
print(f'prod of {n1} and {n2} is {n1*n2}')

output:

sum of 50 and 7 is 57
diff of 50 and 7 is 43
prod of 50 and 7 is 350
===========================================================
==========

n1=65
print(f'{n1:d},{n1:o},{n1:x},{n1:b}')
n2=4.5678
print(f'{n2:f},{n2:e}')
print(f'{n2:.2f}')
===========================================================
==========
a=100
b=1000
c=10000
d=100000
print(f'{a:8d}\n{b:8d}\n{c:8d}\n{d:8d}')
===========================================================
==========
# temperature conversion

celsius=float(input("enter temp in celsius"))


print(f'fahrenheit={(celsius*9/5)+32:.2f}')
===========================================================
==========

Control Statements
================

==> Control statements are used to control the flow of execution of program.
==> By default the program is executed in sequential order.
==> Inorder to change the flow of execution of program we use control statements.

1. Condtional Statements
2. Looping Statements or Iterational Statement

==> Python support only one conditional statement ==> if statement


==> Python support two looping statements ==> while,for

if statement
==========

==> if is conditional statements.


==> this statement is used to execute block of statement based on
given condition.

Types of if statements
==================
1. simple if
2. if..else
3. if..elif
4. nested if

simple if 
=======

==> if without else is called simple if.


==> this syntax is having only if block without else block.

syntax:

if <test/condition>:
     statement-1
     statement-2

statement-3

==> if test is True, it execute statement-1 and statement-2 and continue with
statement-3.
==> if test is False,it exeute statement-3.

Q: What is indent in python?

==> indentation is spaces that used at the begining of the statment.


==> every statement in python start without spaces.
==> indentation is given only for blocked statements (OR) define block.

==> we cannot define empty block. a block required at least one statement.
pass
====

pass is a null operation — when it is executed, nothing happens. It is useful as a


placeholder when a statement is required syntactically, but no code needs to be
executed.

10thSeptember
f-string or string interpolation
========================

==> f-string is called formatted string.


==> this string is prefix with f.
==> inorder to created formatted string, we represent string prefix with 'f' followed
string which contain replacement fields/expression.
==> these fields are represented with name/variablename or expression.

1st approch ==> "%s,%s"%(value,value)


2nd approch ==> "{},{},{}".format(value,value)
3rd approch ==> f'{var},{var},{expression}'

#finding simple intrest

amt=float(input("Enter amount"))
t=int(input("Enter time"))
rate=float(input("Enter rate"))
si=amt*t*rate/100
print(f'Amt={amt},Time={t},Rate={rate},SimpleIntrest={si}')

output:
Enter amount5000
Enter time12
Enter rate2.5
Amt=5000.0,Time=12,Rate=2.5,SimpleIntrest=1500.0
===========================================================
========
n1=50
n2=7
print(f'sum of {n1} and {n2} is {n1+n2}')
print(f'diff of {n1} and {n2} is {n1-n2}')
print(f'prod of {n1} and {n2} is {n1*n2}')

output:

sum of 50 and 7 is 57
diff of 50 and 7 is 43
prod of 50 and 7 is 350
===========================================================
==========

n1=65
print(f'{n1:d},{n1:o},{n1:x},{n1:b}')
n2=4.5678
print(f'{n2:f},{n2:e}')
print(f'{n2:.2f}')
===========================================================
==========
a=100
b=1000
c=10000
d=100000
print(f'{a:8d}\n{b:8d}\n{c:8d}\n{d:8d}')
===========================================================
==========
# temperature conversion

celsius=float(input("enter temp in celsius"))


print(f'fahrenheit={(celsius*9/5)+32:.2f}')
===========================================================
==========

Control Statements
================

==> Control statements are used to control the flow of execution of program.
==> By default the program is executed in sequential order.
==> Inorder to change the flow of execution of program we use control statements.
1. Condtional Statements
2. Looping Statements or Iterational Statement

==> Python support only one conditional statement ==> if statement


==> Python support two looping statements ==> while,for

if statement
==========

==> if is conditional statements.


==> this statement is used to execute block of statement based on
given condition.

Types of if statements
==================
1. simple if
2. if..else
3. if..elif
4. nested if

simple if
=======

==> if without else is called simple if.


==> this syntax is having only if block without else block.

syntax:

if <test/condition>:
statement-1
statement-2

statement-3

==> if test is True, it execute statement-1 and statement-2 and continue with
statement-3.
==> if test is False,it exeute statement-3.
Q: What is indent in python?

==> indentation is spaces that used at the begining of the statment.


==> every statement in python start without spaces.
==> indentation is given only for blocked statements (OR) define block.

==> we cannot define empty block. a block required at least one statement.

pass
====

pass is a null operation — when it is executed, nothing happens. It is useful as a


placeholder when a statement is required syntactically, but no code needs to be
executed.

11thSeptember
if..else
=====

 This syntax provide two blocks


o If block
o Else block
 If block is executed when condition is True
 Else block is executed when condition is False

Syntax:

if  <condition>:
statement-1
statement-2
else:
statement-3
statement-4

statement-5

 If condition is true it execute statement-1,statement-2 and statement5


 If condition is false it execute statement-3,statement-4 and statement5
# write a program to find given number is even or odd

num=int(input("Enter any number"))

if num%2==0:

    print(f'{num} is even')

else:

    print(f'{num} is odd')

# find output

if True:

    print("Hello")

    else:

    print("Bye")

The above script/code display syntax error because else: block is placed inside if, it
should use the same indent of if. The correct code is,

if True:
  print(“Hello”)
else:
  print(“Bye”)

The output is Hello because condition is True.

# write a program read name and age person and find elg to vote or not

name=input("Enter name")
age=int(input("Enter age"))

if age>=18:

    print(f'{name} is elg to vote')

else:

    print(f'{name} is not elg to vote')

# develop login script/program

user=input("Enter UserName")

pwd=input("Enter Password")

if user=='nit' and pwd=='nit123':

    print(f'{user},welcome to my application')

else:

    print(f'invalid username or password')

# write a program to find input character is vowel or not

ch=input("input any character")

if ch=='a' or ch=='e' or ch=='o' or ch=='i' or ch=='u':

    print(f'{ch} is vowel')

else:

    print(f'{ch} is not vowel')

if ch in "aeiouAEIOU":

    print(f'{ch} is vowel')

else:

    print(f'{ch} is not vowel')

# write a program to find max of two numbers


num1=int(input("Enter first number"))

num2=int(input("Enter seconding nuber"))

if num1>num2:

    print(f'{num1} is max')

else:

    print(f'{num2} is max')

# example to read more than value using input

a=input("input two values using sep , ")

x,y=a.split(",")

print(a)

print(x)

print(y)

print(type(a))

print(type(x))

print(type(y))

# write a program to print sum of three numbers

# input three value in one line using sep as space

a=input("Enter three numbers sep with space")

x,y,z=a.split(" ")

sum=int(x)+int(y)+int(z)

print(f'sum of {x},{y},{z} is {sum}')

x,y,z=input("Enter three numbers sep with space").split(" ")

s=int(x)+int(y)+int(z)
print(f'sum of {x},{y},{z} is {s}')

x,y,z=input("Enter three numbers sep with space").split(" ")

print(f'sum of {x},{y},{z} is {int(x)+int(y)+int(z)}')

# write a program to input rno,name 3 subject marks and find result

# input five value in line using sep ,

rno,name,s1,s2,s3=input("Enter rollno,name 3 subject marks sep with ,").split(",")

print(f'Rollno {rno}')

print(f'Name {name}')

if int(s1)<45 or int(s2)<45 or int(s3)<45:

    print(f'{s1},{s2},{s3} and Fail')

else:

    print(f'{s1},{s2},{s3} and Pass')

You might also like