Python 6
Python 6
==========================
note:
2.web application:
==============
3.enterprise application:
====================
application
communication
4.mobile application:
================
may be stand-alone,web,enterprise,...…..
python:
======
what is python?:
=============
python is a "high-level,programming,object-oriented,stongly-
1.high-level language:
=================
program/code/script"
example: c,c++,java,python,js,php,...………….
2.assembly language:
=================
translators
2.assembler
3.interpreter
complier:
=======
into binary
binary"
"compilation"
1.c
2.c++
3.java
4.python
5.typescript...……………….
for example:
==========
program/code
a time)
interpreter:
=========
"interpretation"
1.java
2.python
3.javascript
4.sql...……………….
for example:
==========
JVM(interpreter) ====>sample.exe
===>PVM (interpreter)===>sample.exe
object-oriented:
============
2.inheritance
4.polymorphism
c++,java,python,javascript,...………………….
note:
dynamic-typed:
============
note:
====
only
strongly-typed:
============
why python?
==========
the byte code,this byte code will run in the any platform(any
operating system))
sample.py ===>compiler ==>byte code ===>PVM
1.application development
2.testing
3.data science
4.data analytics
5.data engineering
7.Artificial intelligence(ML,DL,NLP)
8.gaming development
9.IOT
10.networking...……………..
1.alphabets ===>
upper-case(capital) ==>A,B,C,D,...Z
lower-case(Small)===>a,b,c,d...z
2.digits ===>0,1,2,3,4,5,6,7,8,9
3.special symbols/chracters===>~,!,@,#,$,%,^,&,*,(,),[,],|,{,},:,,",',...
action.
for,while
break,continue
if,else,elif,match
case,True,False,
or,and,not
in,is,class,pass,return,def
try,except,finally,raise,import,from,as,with,assert,yield……………..
3.comments:
==========
comments are used in the code "to describe the line of the code
comments will write only the for sake "readability" of the code
syntax:
======
example:
=======
#program for adding two numbers
2.multi-line comments:
==================
syntax:
======
"""
"""
example:
=======
"""
program for adding two numbers
"""
4.identifiers:
==========
underscore"
1.all digits(0,1,2,3,4,5,6,7,8,9)
2.all alphabets(a-z/A-Z)
example:
========
abc_(valid)
_abc(valid)
_123(valid)
_12abc$(invalid)
1abc(invalid)
#abc(valid)
5.variables:
=========
program
program
following syntax:
===============================================
variable_name=value
example:
=======
a=10
python litreals:
============
1.numeric litreals:
===============
integer(1,1234,-45,-6789,....)
complex number(10+5j,10-6j,-9j,...…..)
2.boolean litreals:
==============
"single/double/triple quotes"
example:
"hello"
'hello'
type
example: [1,2,3,4,"hello",.....]
type
example: (1,2,3,4,5...….)
example: {key1:val1,key2:val2,...…...}
7.set literal ===>it will store the data and all data in the set are
unique.
example: {1,2,3,4,5,.........}
9.range() as literal,
10.binary literal
11.octal literal
12.hexadecimal litreal...………………
syntax:
======
another type"
developer
developer
we can not convert the complex number into integer and float/
real number
from to function
===== === =======
note:
====
in genereal,
in numeric,
True ===>1
False ===>0
for example:
10+True ==>11
10-False ===>10
syntax:
======
code:
====
"""
author:Ram
program for display the message called "hello world"
"""
print('hello world')
print("hello world")
print('''hello world''')
note:
====
code:
====
"""
author:Ram
program for display the message called "hello world"
"""
print('hello world',end=",")
print("hello world",end=",")
print('''hello world''',end=",")
note:
=====
class
of the variable:
===============================================
note:
====
when we want to find the type of the object of the any variable
example:
=======
"""
author:Ram
program to find the type of object of the variable
"""
a=10#integer object
print(type(a))
b=1.234#float object
print(type(b))
c=True#boolean object
print(type(c))
d=10+5j#complex object
print(type(d))
e="hello" #string object
print(type(e))
code:
====
"""
author:Ram
program for demonstrate airthmetic operators
"""
#taking the values from the user
a=int(input("enter the value for a:"))#10
b=int(input("enter the value for b:"))#20
print("the sum of a and b is:",a+b)#30
print("the subtraction of a and b is:",a-b)#-10
print("the product of a and b is:",a*b)#200
print("the division of a and b is:",a/b)#0.5
print("the floor division of a and b is:",a//b)#0
print("the modulo diviison of a and b is:",a%b)#10
print("the power/exponenent of a and b is",a**b)#
code:
=====
"""
author:ram
python program to demonstrate relational operators
"""
a=int(input("enter the number for a:"))#10
b=int(input("enter the number for b:"))#20
print("a>b:",a>b)#False
print("a<b:",a<b)#True
print("a>=b:",a>=b)#False
print("a<=b:",a<=b)#True
print("a==b:",a==b)#False
print("a!=b:",a!=b)#True
code:
====
"""
author:ram
python program to demonstrate logical operators
"""
a=int(input("enter the number for a:"))#10
b=int(input("enter the number for b:"))#20
#or
print("(a>b) or (a<b):",(a>b) or (a<b))#True
#and
print("(a>b) and (a<b):",(a>b) and (a<b))#False
#not
print("not(a>b):",not(a>b))#True
operators"
=================================================
"""
author:Ram
python program to demonstrate the "Assignment
operators"
"""
a=10
print("the value of a is:",a)#10
a+=10#a=a+10
print("the value of a is:",a)#20
a-=10
print("the value of a is:",a)#10
a*=10
print("the value of a is:",a)#100
a/=10
print("the value of a is:",a)#10.0
code:
====
"""author:Ram
python program to demonstrate bitwise operators
"""
a=10
b=20
print("bitwise or(a|b):",a|b)#30
print("bitwise and(a&b):",a&b)#0
print("bitwise exclusive or(a^b):",a^b)#30
print("bitwise left shift(a<<2):",a<<2)#40
print("bitwise right shift(a>>2):",a>>2)#2
print("bitwise one's complement':",~a)#-11
note:
====
object at memory"
code:
====
"""
author:Ram
a python to find the memory address
of the given object
"""
a=10
print(id(a))
print(id(10))
b=1.234
print(id(b))
print(id(1.234))
c=a
print(id(c))
h="hello"
print(id(h))
code:
=====
"""
author:Ram
program to demonstrate identity operators in python
"""
a=10
b=20
c=10
print(a is b)
print(a is c)
print(a is not b)
print(c is not b)
print(a is c or a is b)
print(a is c and a is b)
python:
=================================================
code:
====
"""
author:Ram
program to demonstarate warlus operator
"""
a=10
if (a:=a+10)>=20:
print("a value more than 20 or equal to 20")
else:
print("a value less than 20")
print("the value of a is:",a)
6.operators:
==========
operator"
for example,
a=10,b=20
1.airthmetic operator(+,-,*,/,//,%,**):
============================
+ (meaning: addtion):
================
a=10,b=20 ===>a+b===>30
-(meaning: subtraction):
==================
*(meaning: multiplication)
====================
note:
====
real number
a=36,b=5 ==>a//b===>7
a=25,b=1000===>a%b ==>25
note:
====
**(exponent):
==========
10**3 ===>1000
34**3 ===>39304
1**3===>1
a=10,b=20
a>b ===>False
a<b ===>True
3.logical operator(or,and,not):
=======================
logical or:
========
rule:
====
example:
=======
a=10,b=20
note:
====
2.logical and:
==========
rule:
====
example:
=======
a=10,b=20
note:
====
behaviour
3.logical not:
==========
this operator will change the result which is True into False
example:
=======
a=10,b=20
not(a>b) ===>True
not(a<b) ===>False
4.assignment operator(=):
====================
example:
a+=10===>a=a+10 ==>a=20
5.bitwise operator(|,&,^,<<,>>,~):
==========================
number(Decimal number)
1.bitwise or:
==========
symbol: |(pipe)
rule: in the case of bitwise or,if any one input is "1",then the
example:
=======
a=10 ===>01010
b=20 ===>10100
==============
a|b ===> 11110<=== 30
a=45===>101101
b=60===>111100
===============
111101<===61
2.bitwise and:
===========
symbol: &(ampracend)
rule: in the case of bitwise and,if any one input is "0",then the
example:
=======
a=10 ===>01010
b=20 ===>10100
==============
a|b ===> 00000<=== 0
symbol: ^(caret)
rule: in the case of bitwise exclusive or,if both inputs are same
example:
=======
a=10 ===>01010
b=20 ===>10100
==============
a^b ===> 11110<=== 30
a=100 ===>01100100
b=200===>11001000
====================
a^b ====>10101100 <==172
symbol: <<
example:
========
a=20
symbol: >>
example:
========
a=20
symbol: ~(tlide)
formula: ~n=-(n+1)
example:
========
a=10
~a=-(a+1)=-11
a=-19
~a=-(-19+1)=18
these operators are used in python "to check the given value
example:
=======
2.is not(it will check both objects are not at same memory
location)
example:
=======
x=10
y=20
z=10
a=y
x is y ===>False
x is not y ==>True
x is not a ===>True
y is not z===>True
note:
====
when we create any two variables with same object, then the
location
syntax:
======
example:
=======
x=10,y=20
9.warlus operator(:=):
=================
symbol:
======
:=
example:
========
x=10
x=30
(x:=x+10)>50 ====>False
7.conditional statements:
====================
statement"
if condition:
note:
when we are working with if, the logic under the if will be
when we write the any statement under the if, each statement
indentation error
if condition:
#logic
example:
=======
"""
author:ram
if statement
"""
a=10
if a>10:
print("a value is more than 10")
if a<=10:print("a value is less than 10 or equal to 10")
example:
=======
if True:
#logic
"pass"
in python,"pass" is a keyword
example:
=======
"""
author:ram
if statement
"""
a=10
if a>10:#empty if statement
pass
if a<=10:
print(a)
pass
2.if-else statement :
================
if-else statements
syntax:
======
if condition:
#logic
else:
#logic
note:
====
in python,
example:
=======
"""
author:ram
if-else statement
"""
a=10
b=20
if a>b:print("a>b")
else:print("b>a")
#or
print("a>b") if a>b else print("b>a")
3.else-if ladder:
============
syntax:
======
if condition:
#logic
elif condition:
#logic
elif condition
#logic
.
.
.
else:
#logic
note:
====
elif conditions"
or
example:
=======
"""
author:Ram
else-if ladder
"""
a=10
b=20
c=30
if a>b and a>c:
print(a)
elif b>c:
print(b)
else:
print(c)
statements"
example:
=======
if condition:
if condition:
if condition:
else:
else:
if condition:
#logic
else:
#logic
example:
=======
age=int(input("Enter the age:"))
if age>=21:
if age>=21 and age<=30:
print("enjoy the bachelor life")
elif age>=31 and age<=40:
print("good age,check with experts before marriage")
elif age>=41 and age<=60:
print("already you are in heaven,enjoy the remaing life")
elif age>=61:
print("RIP!")
else:
print("this is not a video game!")
these statements are used in python, "to execute any logic for
statements:
================================================
1.while loop:
==========
syntax:
======
while condition:
#logic
example:
=======
"""
author:Ram
while loop
"""
a=1# the value of the a is 1
times=0
while a<=10:
print(a)#1 2 3 4 5 6 7 8 9 10
a+=1 #a=11
times+=1
print("the value of a is:",a)
print("the number of times loop is executed:",times)
#output:
#1 2 3 4 5 6 7 8 9 10
#the value of a is:11
example-2:
=========
code:
=====
"""
author:Ram
while loop
"""
start=1
while start<=100:
if start%2==0:
print(start,end=" ")
start+=1
example-3:
=========
code:
====
"""
author:Ram
while loop
"""
start=100
while start>=1:
if start%2!=0:
print(start,end=" ")
start-=1
example-4:
========
find the given number factors and print them using python:
==============================================
"""
author:Ram
while loop
"""
num=int(input("enter the number:"))#6
fact=1
while fact<=num:
if num%fact==0:
print(fact,end=" ")#1,2,3,6
fact+=1
infinite loop:
==========
infinite loop means "the condition what we taken for the loop
example:
=======
a=1
while a<=10:
print(a)
2.for loop:
=========
(list/tuple/range()/set/string/dictionary....)
syntax:
======
note:
====
function in python:
example:
=======
range(1,10) ===>1,2,3,4,5,6,7,8,9
range(1,21) ====>from 1 to 20
note:
====
range() is "0"
range() is "1"
example:
=======
range(10) <=== here if we give only one value,then range() assumes it is only end
value
range(10) ===>range(0,10,1)
0,1,2,3,4,5,6,7,8,9
range(1,10,2) ===>1,3,5,7,9
range(1,10,4) ===>1,5,9
range(1,10,6) ===>1,7
example-1:
=========
"""
author:Ram
for loop
"""
for i in range(10):#it is always end value
print(i,end=" ")
print()
for i in range(1,10,6):
print(i,end=" ")
print()
for i in range(1,10,4):
print(i,end=" ")
print()
for i in range(1,10,-6):
print(i,end=" ")
print()
for i in range(10,10,6):
print(i,end=" ")
example-2:
=========
"""
author:Ram
for loop
"""
for i in range(1,10,0):
print(i,end=" ")
print()
"""
step value never be zero
"""
example-3:
=========
code:
====
"""
author:Ram
for loop
"""
for i in range(10,0,-3):
print(i,end=" ")
print()
for i in range(99,0,-2):
print(i,end=" ")
"""
when we take negative step in range()
example:
=======
"""
author:Ram
range type
"""
x=range(1,10)
print(x)
print(type(x))
print(help(range))
syntax:
======
range_object_name[index]
example:
=======
"""
author: ram
indexing on range()
"""
a=range(10,110,10)
print(a[0])
print(a[7])
print(a[8])
print(a[4])
print(a[2])
print(a[-1])
print(a[-5])
print(a[-10])
print(a[-4])
write a python program print the even index elements from the
range.
code:
====
"""
author: ram
indexing on range()
"""
x=range(10,100,10)
index=0
for i in x:
if index%2==0:
print(i)
index+=1
"""
in python,to find the length of the range,we will
use,len() function,
length of the range=number of numbers in range
"""
index=0
print("length of the x range is:",len(x))
while index<len(x):
if index%2==0:
print(x[index])
index+=1
slicing on range():
==============
syntax:
======
range_object_name[start:end:step]
"""
author: ram
indexing on range()
"""
x=range(10,110,10)
for i in x[1:4]:
print(i,end=" ")
print()
for i in x[1:]:
print(i,end=" ")
print()
for i in x[:6]:
print(i,end=" ")
print()
for i in x[:]:
print(i,end=" ")
print()
for i in x[1:10:4]:
print(i,end=" ")
example-2:
=========
x=range(10,110,10)
for i in x[-10:]:
print(i,end=" ")
print()
for i in x[-8:-1]:
print(i,end=" ")
print()
for i in x[::3]:
print(i,end=" ")
print()
for i in x[-10::3]:
print(i,end=" ")
print()
example-3:
=========
"""
author: ram
indexing on range()
"""
x=range(10,110,10)
for i in x[-4:-8]:
print(i,end=" ")
print()
for i in x[-4:-8:-1]:
print(i,end=" ")
print()
for i in x[-8:-4:1]:
print(i,end=" ")
print()
for i in x[-10:5]:
print(i,end=" ")
print()
example-4:
=========
"""
author: ram
indexing on range()
"""
x=range(10,110,10)
for i in x[::3]:
print(i,end=" ")
print()
x=range(10,110,10)
for i in x[::4]:
print(i,end=" ")
print()
x=range(10,110,10)
for i in x[::5]:
print(i,end=" ")
print()
x=range(10,110,10)
for i in x[::-1]:
print(i,end=" ")
print()
x=range(10,110,10)
for i in x[::-4]:
print(i,end=" ")
print()
or temporary variable:
==============================================
code-1:
======
"""
author:ram
swapping of the two numbers
"""
a=int(input("Enter the number for a:"))
b=int(input("Enter the number for b:"))
print("before swapping:")
print(a,b,sep=",")
a,b=b,a
print("after swapping:")
print(a,b,sep=",")
code-2:
======
"""
author:ram
swapping of the two numbers
"""
a=int(input("Enter the number for a:"))
b=int(input("Enter the number for b:"))
print("before swapping:")
print(a,b,sep=",")
#using airthmetic operators
a=a+b
b=a-b
a=a-b
print("after swapping:")
print(a,b,sep=",")
code-3:
======
"""
author:ram
swapping of the two numbers
"""
a=int(input("Enter the number for a:"))
b=int(input("Enter the number for b:"))
print("before swapping:")
print(a,b,sep=",")
#using airthmetic operators
a=a*b
b=a//b
a=a//b
print("after swapping:")
print(a,b,sep=",")
code-4:
======
"""
author:ram
swapping of the two numbers
"""
a=int(input("Enter the number for a:"))
b=int(input("Enter the number for b:"))
print("before swapping:")
print(a,b,sep=",")
#using bitwise operators
a=a^b
b=a^b
a=a^b
print("after swapping:")
print(a,b,sep=",")
code:
=====
"""
author:ram
area of the circle
"""
print("the area of the circle:",(3.14)*(int(input("enter the number:"))**2))
3.simple interest:
=============="""
author:ram
simple interest
"""
p=int(input("enter P:"))
t=int(input("enter T:"))
r=int(input("enter R:"))
print("simple interest:",(p*t*r)//100)
code:
====
"""
author:ram
maximum of the given three numbers
"""
num1=int(input("enter Num1:"))
num2=int(input("enter Num2:"))
num3=int(input("enter Num3:"))
if num1>num2 and num1>num3:print("maximum:",num1)
elif num2>num3:print("maximum",num2)
else:print("maximum:",num3)
code:
====
"""
author:ram
minimum of the given three numbers
"""
num1=int(input("enter Num1:"))
num2=int(input("enter Num2:"))
num3=int(input("enter Num3:"))
if num1<num2 and num1<num3:print("minimum:",num1)
elif num2<num3:print("minimum",num2)
else:print("minimum:",num3)
code:
====
"""
author:ram
average of the six subjects
"""
sub1=int(input("enter sub1:"))
sub2=int(input("enter sub2:"))
sub3=int(input("enter sub3:"))
sub4=int(input("enter sub4:"))
sub5=int(input("enter sub5:"))
sub6=int(input("enter sub6:"))
print("Average of the six subjects:",(sub1+sub2+sub3+sub4+sub5+sub6)//6)
7.check given number is positive or not
================================
code:
====
"""
author:ram
check given number is positive or not
"""
num=int(input("enter the number:"))
if num>0:print("positive")
elif num<0:print("negative")
else:print("neither positive nor negative")
"""
author:ram
check given number is even or odd
"""
print("Even") if int(input("enter the number:"))%2==0 else print("odd")
"""
author:ram
check given divisible by both 8 and 16 or not
"""
num=int(input("enter the number:"))
print("yes") if num%8==0 and num%16==0 else print("no")
code:
====
"""
author:Ram
print the digits of the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
#convert the given number into string,using str()
num=str(num)#123
#print the each digit using for loop
for i in num:
print(i)#1 2 3
or
"""
author:Ram
print the digits of the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
#using while loop
while num!=0:
digit=num%10
print(digit)
num//=10
code:
====
"""
author:Ram
print the number of digits in the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
count=0
#using while loop
while num!=0:
count+=1#count=count+1
num//=10
print("the number digits in the given number is:",count)
example:
=======
123 ===>1+2+3=6
code:
====
"""
author:Ram
print the sum of digits in the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
sum=0
#using while loop
while num!=0:
digit=num%10
sum+=digit#sum=sum+digit
num//=10
print("the sum of digits in the given number is:",sum)
"""
author:Ram
maximum digit in the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
max=0
#using while loop
while num!=0:
digit=num%10#3,2,1
if max<digit:
max=digit#max=3
num//=10
print("the maximum digit in the given number is:",max)
code:
====
"""
author:Ram
maximum digit in the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
min=9
#using while loop
while num!=0:
digit=num%10#3,2,1
if min>digit:
min=digit#min=1
num//=10
print("the minimum digit in the given number is:",min)
code:
====
"""
author:Ram
product of the digits of the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
product=1
#using while loop
while num!=0:
digit=num%10#3,2,1
product*=digit
num//=10
print("the product of the digits in the given number is:",product)
code:
====
"""
author:Ram
factors of the given number
"""
#take the number from the user
num=int(input("enter the number:"))#123
fact=1
#using while loop
while fact<num:
if num%fact==0:
print(fact,end=" ")
fact+=1
example:
=======
24 ====>1,2,3,4,6,8,12
code:
====
"""
author:Ram
maximum factor of the given number
"""
#take the number from the user
num=int(input("enter the number:"))
fact=1
max=1
#using while loop
while fact<num:
if num%fact==0:
if max<fact:
max=fact
fact+=1
print("the Maximum factor of the given number:",max)
code:
=====
"""
author:Ram
minimum factor of the given number
"""
#take the number from the user
num=int(input("enter the number:"))
fact=2
min=num
#using while loop
while fact<num:
if num%fact==0:
if min>fact:
min=fact
fact+=1
print("the minimum factor of the given number:",min)
code:
====
"""
author:Ram
sum of the factors of the given number
"""
#take the number from the user
num=int(input("enter the number:"))
fact=1
sum=0
#using while loop
while fact<num:
if num%fact==0:
sum+=fact
fact+=1
print("the sum of the factors of the given number:",sum)
example:
=======
10 ===>1,2,5,10
20===>1,2,4,5,10,20
ouput: 1,2,5,10
code:
=====
"""
author:Ram
common factors of the given two numbers
"""
#take the numbers from the user
num1=int(input("enter the number 1:"))#70
num2=int(input("enter the number 2:"))#50
fact=1
#take the smallest number
num=num1 if num1<num2 else num2
#using while loop
while fact<=num:
if num1%fact==0 and num2%fact==0:
print(fact)
fact+=1
code:
====
"""
author:Ram
common factors of the given two numbers
"""
#take the numbers from the user
num1=int(input("enter the number 1:"))#70
num2=int(input("enter the number 2:"))#50
fact=1
#take the smallest number
num=num1 if num1<num2 else num2
max=0
#using while loop
while fact<=num:
if num1%fact==0 and num2%fact==0:
if max<fact:max=fact
fact+=1
print("HCF/GCD of the given two numbers is:",max)
code:
====
"""
author:Ram
LCM of the given two numbers
"""
#take the numbers from the user
num1=int(input("enter the number 1:"))#70
num2=int(input("enter the number 2:"))#50
fact=1
#take the smallest number
num=num1 if num1<num2 else num2
max=0
#using while loop
while fact<=num:
if num1%fact==0 and num2%fact==0:
if max<fact:max=fact
fact+=1
print("LCM of the given two numbers is:",(num1*num2)//max)
code:
====
"""
author:Ram
square root of the given number
"""
#take the numbers from the user
num1=int(input("enter the number 1:"))
print("square root of the number:",(num1)**(0.5))
code:
====
"""
author:Ram
factorial of the given number
"""
#take the numbers from the user
num=int(input("enter the number:"))
fact=1
if num==0 or num==1:
print("Factorial of the given number:1")
elif num>1:
while num!=0:
fact*=num
num-=1
print("Factorial of the given number:",fact)
elif num<0:
print("given number is invalid")
code:
====
"""
author:Ram
fibnocii series
"""
a=0
b=1
count=int(input("how many numbers you want in the fibnocii series:"))
print(a,b,end=" ")
while count!=0:
print(a+b,end=" ")
a,b=b,a+b
count-=1
9.un-conditional statements:
======================
these statemens are used in python to change the "control
with no conditions"
3.return
example-1:
=========
"""
author: ram A
break statement
"""
for i in range(1,11):
print(i)
if i==5:
break
a=1
while a<=10:
print(a)
if a==5:
break
a+=1
example-2:
=========
"""
author:ram
continue statement
"""
a=0
while a<=10:
a+=1
if a==5:
continue
print(a)
nested loops:
===========
nested loops means create the one loop inside another loop
exampel-1:
=========
"""
author: Ram
"""
a=1
b=1
times=0
while a<=10:
while b<=5:
#print(a)#1 1 1 1 1
b+=1
times+=1
a+=1
b=1
print(times)
example-2:
=========
"""
author: Ram
"""
a=1
b=1
k=1
times=0
while a<=10:
while b<=5:
while k<=5:
if k==4:
break
k+=1
times+=1
b+=1
k=1
times+=1
a+=1
b=1
k=1
print(times)
example-3:
=========
"""
author:ram
continue statement
"""
a=1
b=1
c=1
d=1
times=0
while a<=10:
while b<=5:
while c<=10:
while d<=5:
if d==2:
break
times+=1
d+=1
d=1
times+=1
c+=1
b+=1
times+=1
a+=1
times+=1
print(times)#
example-4:
=========
"""
author:ram
continue statement
"""
a=1
b=1
c=1
d=1
times=0
while a<=10:
while b<=5:
while c<=10:
while d<=5:
if d==2:
break
times+=1
d+=1
d=1
times+=1
c+=1
b+=1
times+=1
a+=1
times+=1
b=1
c=1
b=1
print(times)#
example-5:
=========
"""
author:ram
continue statement
"""
a=1
b=1
c=1
d=1
times=0
while a<=10:
while b<=5:
while c<=10:
while d<=5:
if d==2:
break
times+=1
d+=1
d=1
times+=1
c+=1
b+=1
times+=1
c=1
a+=1
times+=1
b=1
c=1
b=1
print(times)#
eample-6:
========
"""
author:ram
continue statement
"""
a=b=c=d=1
times=1
while a<=5:
while b<=10:
while c<=5:
while d<=6:
if d==1:
break
times+=1
if c==2:
break
c+=1
times+=1
if b==3:
break
b+=1
times+=1
a+=1
times+=1
b=c=d=1
print(times)
example-7:
=========
"""
author:ram
continue statement
"""
a=b=c=d=1
times=0
while a<=5:
a+=1
if a==5:
break
else:
while b<=5:
b+=1
if b==3:
break
else:
times+=1
times+=1
print(times)
b=1
#print(times)
code:
====
for i in range(1,101):
if "1" not in str(i) and "2" not in str(i) and "3" not in str(i):
print(i,end=" ")
example:
========
i,j,k,count=1,1,1,0
while i<=10:
while j<=10:
while k<=5:
count+=1
k+=1
k=1
j+=1
j,k=1,1
i+=1
print(count)
10 and 20 ===>01,11,21,31,41,51,61,71,81,91,02
code:
====
code:
====
start=0
count=int(input("enter the number values you want in series:"))
while count!=0:
print(start,end=",")#0,2
start+=2#4
count-=1#8
print()
#using for loop
for i in range(0,20,2):
print(i,end=",")
2.10,120,1440,...…………
================================================
code:
====
start=10
count=int(input("enter the number values you want in series:"))
while count!=0:
print(start,end=",")#10,120
start*=12#1440
count-=1#8
print()
#using for loop
start=10
for i in range(1,11):
print(start,end=",")
start*=12
3.1000,100,10,...……………..
=================================================
code:
====
start=1000
count=int(input("enter the number values you want in series:"))#10
while count!=0:
print(start,end=",")#1000,100,10
start//=10#1
count-=1#7
print()
code:
=====
a,b=0,1
count=int(input("enter the number values you want in series:"))#10
count-=2
print(a,b,end=",")
while count!=0:
print(a+b,end=",")#1,
a,b=b,a+b
count-=1
print()
any * or ** operator:
=================================================
code:
=====
start=1
count=int(input("enter the number values in the series:"))
sum=0
num=1
while count!=0:
while num<=start:
sum+=start
num+=1
print(sum,end=",")
num=1
sum=0
count-=1
start+=1
problems on patterens:
==================
patteren-1:
=========
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
code:
====
"""
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
"""
rows=int(input("enter the number of rows:"))
for i in range(1,rows+1):
for col in range(1,i+1):
print(col,end=" ")
print()
patteren-2:
=========
0
2 4
6 8 10
12 14 16 18
20 22 24 26 28
code:
====
"""
0
2 4
6 8 10
12 14 16 18
20 22 24 26 28
"""
rows=int(input("enter the number of rows:"))
data=0
for i in range(1,rows+1):
for col in range(1,i+1):
print(data,end=" ")
data+=2
print()
patteren-3:
=========
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
code:
====
"""
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
"""
rows=int(input("enter the number of rows:"))
col=rows
for i in range(1,rows+1):
for data in range(1,col+1):
print(data,end=" ")
col-=1
print()
patteren-4:
=========
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
code:
=====
"""
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
"""
rows=int(input("enter the number of rows:"))#5
col=rows#5
data=rows#5
for i in range(1,rows+1):#(1,6)
for j in range(1,col+1):#(1,6)
print(data,end=" ")#5 4 3 2 1
data-=1
col-=1#4
data=col#4
print()
patteren-6:
=========
*
* *
* * *
* * * *
* * * * *
code:
====
"""
*
* *
* * *
* * * *
* * * * *
"""
rows=int(input("enter the number of rows:"))#5
for i in range(1,rows+1):print("* "*i)
patteren-7:
=========
* * * * *
* * * *
* * *
* *
*
code:
====
"""
*
* *
* * *
* * * *
* * * * *
"""
rows=int(input("enter the number of rows:"))#5
col=rows
for i in range(1,rows+1):
print("* "*col)
col-=1
patteren-8:
=========
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
code:
====
"""
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
"""
rows=int(input("enter the number of rows:"))#5
for i in range(1,rows+1):
if i<=(rows//2):
print("* "*i)
elif i>(rows//2):
print("* "*(rows-i))
patteren-9:
=========
*
*
*
*
*
code:
====
"""
*
*
*
*
*
"""
rows=int(input("enter the number of rows:"))#5
for i in range(rows):
print(" "*i+"*")
patteren-10:
==========
*
*
*
*
*
code:
====
"""
*
*
*
*
*
"""
rows=int(input("enter the number of rows:"))#5
spaces=rows
for i in range(rows):
print(" "*spaces+"*")
spaces-=1
10.functions:
===========
with help of the functions,we can write the code once, we can
in the program
called "modules"
arguments/parameters
execution
syntax:
def function_name(arg1,arg2,arg3,....argn):
#logic
note:
====
"def"
syntax:
name_of_the_function(arg1,arg2,arg3,....argn)
example:
=======
"""
create the function in python
"""
def display():
print("this is my first function in python")
#call the function display
display()
example-2:
=========
"""
create the function in python
"""
def factorial():
#take the number
num=int(input("enter the number:"))#5
fact=1
for i in range(1,num+1):
fact*=i
print("the factorial of the given number is:",fact)
factorial()
in this model,
example:
=======
"""
function without arguemnts and without return type
"""
def sum_of_the_two_numbers():
#take the numbers
num1=int(input("enter the number1:"))
num2=int(input("enter the number2:"))
print("the sum of the two numbers is:",num1+num2)
sum_of_the_two_numbers()
in this model,
example:
=======
"""
function with arguemnts and without return type
"""
def sum_of_the_two_numbers(x,y):
print("the sum of the two numbers is:",x+y)
#take the numbers
num1=int(input("enter the number1:"))
num2=int(input("enter the number2:"))
#calling function
sum_of_the_two_numbers(num1,num2)
in this model,
note:
====
function can return any value as result after its execution, with
function
variable, then the variable will hold the function returned value
syntax:
======
variable_name=fucntion_name(arg1,arg2,...….)
or
print(function_name(atg1,arg2,arg3,arg4,...…..))
example:
=======
"""
function with arguemnts and with return type
"""
def sum_of_the_two_numbers(x,y):
return x+y
#take the numbers
num1=int(input("enter the number1:"))
num2=int(input("enter the number2:"))
#calling function
result=sum_of_the_two_numbers(num1,num2)
print(result)
in this model,
execution"""
example:
=======
"""
function without arguemnts and with return type
"""
def sum_of_the_two_numbers():
#take the numbers
num1=int(input("enter the number1:"))
num2=int(input("enter the number2:"))
return num1+num2
#calling function
result=sum_of_the_two_numbers()
print(result)
1.positional arguments:
==================
arguments
example:
=======
"""
positional arguments
"""
def my_positional_arguments(x,y,z):
print(x,y,z)
my_positional_arguments(10,20,30)#x=10,y=20,z=30
note:
====
2.default arguments:
=================
example:
=======
"""
default arguments
"""
def my_default_arguments(x=100,y=200,z=300):
print(x,y,z)
my_default_arguments()
my_default_arguments(10)
my_default_arguments(10,20)
my_default_arguments(10,20,30)
3.keyword arguments:
==================
or
keyword arguments
example:
=======
"""
keyword arguments
"""
def my_keyword_arguments(x,y,z):
print(x,y,z)
my_keyword_arguments(x=10,y=20,z=30)
my_keyword_arguments(z=10,y=20,x=30)
my_keyword_arguments(y=10,z=20,x=30)
my_keyword_arguments(z=10,x=20,y=300)
4.arbitary arguments:
=================
name
*argument_name
these arguments will take any number of values and all values
example:
=======
"""
arbitary arguments
"""
def my_arbitary_arguments(*x):
print(x)
print(type(x))
my_arbitary_arguments()
my_arbitary_arguments(1,2,3,4,5,6)
my_arbitary_arguments(10,20,30)
my_arbitary_arguments(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
calling
arguments
name
as "dictionary"
example:
=======
"""
keyword_arbitary arguments
"""
def my_keyword_arbitary_arguments(**x):
print(x)
print(type(x))
my_keyword_arbitary_arguments()
my_keyword_arbitary_arguments(a=100,b=200,c=400)
my_keyword_arbitary_arguments(name="ram",location="hyderabad")
example-1:
=========
def display(x,y,z=100):
print(x)#300
print(y)#100
print(z)#450
display(10,20)
display(10,20,30)
display(y=100,x=300,z=450)
example-2:
=========
def display(x,y,z=100,*arg):
print(x)#300
print(y)#100
print(z)#450
print(*arg)
display(10,20)
display(10,20,30)
display(y=100,x=300,z=450)
display(1,2,3,50,60,70,80,90,100)
display(x=100,z=200,y=300)
example-3:
=========
def display(*arg,**kwarg):
print(arg)
print(kwarg)
display()
display(1,2,3,4)
display(1,2,a=10,b=20,c=30)
display(a=10,b=20,c=30)
display(1,10,20,40,a=10,b=20)
exmaple-4:
=========
def display(x,y,z=100,*arg,**kwarg):
print(x,y)
print(z)
print(arg)
print(kwarg)
display(10,20)
display(20,y=100,z=300)
display(30,20,50,1,2,3,a=10,b=20,c=40)
recursive functions:
===============
example:
=======
def display(x):
if x<=10:
print("this is recursive function")
x+=1
display(x)
display(1)
example:
=======
code:
====
def factorial(num):
if num==0 or num==1:
return 1
elif num>1:
return num*factorial(num-1)
print(factorial(int(input("enter the nummber:"))))
example:
=======
print the factors of the given number using recursion
==========================================
def factor(num,fact):
if fact<=num:
if num%fact==0:
print(fact)
factor(num,fact+1)
fact=1
factor(int(input("enter the nummber:")),fact)
lambda functions:
==============
lambda function is a function and which can have only one line
as code
"lambda"
syntax:
======
example:
=======
x=lambda:print("this is my first lambda function")
print(type(x))
x()
x=lambda a,b:a+b
print(x(10,20))
x=lambda a,b,c:a+b+c
print(x(1,2,3))
example:
=======
example:
=======
def result(num):
return lambda x:num*x
res=result(int(input("number:")))
print(type(res))
print(res(10))
python scope:
===========
scope refers "at what extent we can able to access the data
in the program"
1.local scope:
============
when we define the any data inside the function, then that data
can able to access only in the function, but not outside the
2.global scope:
============
when we define any data outside the function, then the data
global data,we can access inside the function, but we can not
example:
=======
x=10#global data
def display():
y=20
print(x)#global data
print(y)#local data
print(x)#10
display()
example:
=======
x=1000
def display1():
print(x)
x=100
def display2():
print(x)#100
display2()#100
display1()#100
L ===>Local
E===>Enclosed
G===>Global
B===>Built-in
example-3:
=========
x=10
def display():
x=100
print(x)
x+=100
print(x)
display()
function"
closures"
the following:
=================================================
inner function can able to access the outer function data ,but
not vice versa(outer function can not access the inner function
data)
inner function can able to access the global data (the data
functions
example:
=======
def outer():
def inner1():
print("This is inner 1")
def inner2():
print("This is inner 2")
def inner3():
print("This is inner 3")
inner1()
inner2()
inner3()
outer()
example-2:
=========
p,q,r=1,2,3#global data
def outer():
x,y,z=100,200,300
print(p,q,r)
def inner1():
a=10
print(p,a,x)
def inner2():
b=20
print(q,b,y)
def inner3():
c=30
print(r,c,z)
inner1()
inner2()
inner3()
outer()
example-3:
=========
global keyword :
syntax:
======
global variable_name
example:
=======
def display():
global x
x=100
print(x)
display()
print(x)
def display2():
print(x)
display2()
example-2:
=========
def display():
global x
def inner1():
global x
x=1000
def inner2():
global x
print(x)
inner1()
inner2()
print(x)
display()
print(x)
nonlocal:
=======
example:
=======
x=10
def display():
def inner1():
x=100
print(x)#100
inner1()
print(x)#10
display()
example:
========
x=10
def display():
def inner1():
x=1000
print(x)
def inner2():
def inner3():
print(x)
global x
x=1000
inner3()
inner1()
inner2()
print(x)
display()
print(x)
example:
=======
x=10
def display():
x=10
print(x)#10
def inner1():
nonlocal x
x=100
print(x)#100
inner1()
print(x)#100
display()
print(x)
example:
=======
x=10
def display():
x=100
def inner1():
nonlocal x
print(x)
inner1()
print(x)
display()
print(x)
example:
=======
x=10
def display():
x=100
def inner1():
nonlocal x
x=1000
def inner2():
nonlocal x
print(x)
print(x)
inner2()
inner1()
print(x)
display()
print(x)#10
example:
=======
def display():
global x
x=100
display()
print(x)
def display2():
global x
x=1000
print(x)
display2()
print(x)#1000
example:
=======
x=10
def display():
global x
x+=10
print(x)#20
def inner():
global x
x+=100
print(x)#120
inner()
display()
print(x)#120
def display2():
global x
x+=300
print(x)#420
display2()
example:
=======
x=10
def display():
x=100
def inner():
nonlocal x
x+=100
print(x)#200
inner()
print(x)#200
def inner2():
nonlocal x
x+=300
print(x)#500
inner2()
print(x)#500
display()
print(x)
modules:
=======
using modules, any python file can use another python file
data/functions
using python module, we can reuse the any other python file
data or functions
steps:
=================================================
import module_name
module_name.data_name/method_name()
example:
=======
sample.py:
=========
x=100
y=200
z=300
def display():
print("this is sample module")
def add(a,b):
print(a+b)
def sub(a,b):
print(a-b)
def mul(a,b):
print(a*b)
test.py:
======
import sample
print(sample.x)
print(sample.y)
print(sample.z)
sample.display()
sample.add(10,20)
sample.sub(10,20)
sample.mul(10,20)
called "as"
example:
========
import sample as s
print(s.x)
print(s.y)
print(s.z)
s.display()
s.add(10,20)
s.sub(10,20)
s.mul(10,20)
note:
====
example:
=======
folder
sample.py:
=========
x=100
y=200
z=300
sample2.py:
=========
def add(a,b):print(a+b)
def sub(a,b):print(a-b)
def mul(a,b):print(a*b)
mypackage
test.py:
======
example:
=======
test.py
======
mypackage2
|
|
__init__.py(create, save and run the file)
mysubpackage1
|
__init__.py(create, save and run the file)
sample.py
mysubpackage2
|
__init__.py(create, save and run the file)
sample2.py
sample.py:
=========
x=100
y=200
z=300
sample2.py:
=========
def add(a,b):print(a+b)
def sub(a,b):print(a-b)
def mul(a,b):print(a*b)
test.py:
======
example:
=======
test.py:
======
from mypackage2.mysubpackage1.sample import x,y
from mypackage2.mysubpackage2.sample2 import add,sub
print(x,y)
add(10,20)
sub(10,20)
13.built-in functions:
=================
1.user-defined function:
===================
2.built-in function:
===============
functions:
================================================
example:
=======
x,y,z=10,20,30
print(x)
print(y)
print(z)
print(x,y,z,sep=",")
print(x,y,z,end=",")
example-2:
=========
code-1:
======
x,y,z=10,20,30
print(f"the value of x is:{x}")
print(f'the value of y is:{y}')
print(f"the sum of the x and y is:{x+y}")
code-3:
======
x,y,z=10,20,30
print("the value of x is:{0}".format(x))
print("the value of y is {1} and x is :{0}".format(x,y))
print("the value of y is {value2}".format(value2=y))
print("the value of z is {z1} and x is {x1}".format(z1=z,x1=x))
note:
====
will not able to give the input/user will not understand where
example:
=======
x=input("X value:")
example:
=======
x=int()
print(x)
x=int(12_34)
print(x)
x=int(1_2_3_4)
print(x)
x=int("123")
print(x)
x=int(12.34)
print(x)
x=int("+123")
print(x)
x=int("-123")
print(x)
print(float(10))
print(float())
print(float("+123"))
print(float("-67"))
print(float("1_2.3_4"))
print(float(+12_34.5_6_7))
print(float("12.34"))
example:
=======
print(str())
example:
=======
print(bool())#default is False
print(bool(1))
print(bool(0))
print(bool(None))
print(bool("abc"))
print(bool(''))
print(bool(10+5j))
print(bool(-123))
example:
=======
print(complex(10))
print(complex(1.2))
#print(complex("abc"))
print(complex(True))
print(complex(False))
#print(complex(None))
print(complex(10+5j))
print(complex())
example:
=======
print(list(range(1,10)))
print(list((1,2,3,4,5)))
print(list("hello"))
print(list({1,2,3,4,5,6,7,8,9,10}))
print(list({1:2,3:4,5:6}))
10.tuple()<== it used to convert the given iterable data into
tuple
example:
=======
print(tuple([1,2,3,4,5]))
print(tuple("12345"))
print(tuple("hello"))
print(tuple({1,2,3,4,5,6,7,8,9,10}))
print(tuple({1:2,3:4,5:6}))
print(tuple("h"))
example:
=======
print(set([1,2,3,4,5]))
print(set("12345"))
print(set("hello"))
print(set({1,2,3,4,5,6,7,8,9,10}))
print(set({1:2,3:4,5:6}))
print(set("h"))
print(set([1,2,3,12,3,1,23,4,5]))
12.range():
=========
example:
========
print(range(1,10))
print(type(range(1,10)))
print(list(range(1,10,4)))
a=range(10,1,-1)
print(a)
print(tuple(a))
example:
========
print(dict(a=10,b=20,c=30,d=40))
print(dict())
print(set())
a={}
print(type(a))
example: 0b101010,0B1010101
example:
=======
print(bin(10))#10 is decimal number(0-9 only digits)
print(bin(0o127))#here is octal number(0-7 only digits)
print(bin(0xabc))#here is hexadecimal number(0-9 and a-f)
a=bin(27)#110011
print(type(a))#<class 'str'>
example:
=======
print(oct(100))
print(oct(0b10101))
print(bin(0xabc))
a=oct(27)
print(type(a))
16.hex():
=======
print(hex(100))
print(hex(0b10101))
print(hex(0xabc))
a=hex(27)
print(type(a))
note:
=====
a=0b10101#binary literal
b=0x121#hexa decimal litreal
c=0o127#octal litreal
print(a)
print(b)
print(c)
a=bin(a)
print(a)
17.split():
========
this function will used to split the given string into "n"
example:
=======
print("hello world".split())
print("abcdefghij".split("e"))
print("hello world".split("l"))
print("hellllworlld".split("l"))
print("hellllllld".split("l"))
print("delloh".split("h"))
a,b,c=input("enter:").split(",")
print(a,b,c)
18.map()
19.filter()
20.eval():
=======
form
example:
=======
a=10
b=20
print(eval("10+20"))
print(eval("a+b"))
print(eval("a>10"))
print(eval("a<20"))
print(eval("a**2"))
21.exec():
========
this function will execute the given code which is in the form
of string
example:
=======
exec("a=10")
exec("b=20")
print(a)
print(b)
exec("result=a+b")
print(result)
print(eval("a+b"))
22.min():
=======
iterable data"
example:
=======
print(min(range(1,10)))
print(min([10,2,3,4,0,-12,100]))
print(min(range(10,-3,-4)))
print(min({1,2,3,10,90,100,345,789}))
print(min(1,2,3,4,5,6,7,8,9,10))
print(min(10,20,30,40,50,60,70,80,90,100))
a=1,2,3,4,5,6,7,8,9,10
print(type(a))
23.max():
========
iterable" in python
example:
=======
print(max(range(1,10)))
print(max([10,2,3,4,0,-12,100]))
print(max(range(10,-3,-4)))
print(max({1,2,3,10,90,100,345,789}))
print(max(1,2,3,4,5,6,7,8,9,10))
print(max(10,20,30,40,50,60,70,80,90,100))
a=1,2,3,4,5,6,7,8,9,10
print(type(a))
24.len():
=======
example:
========
print(len(range(1,10)))#9
print(len([10,2,3,4,0,-12,100]))#7
print(len(range(10,-3,-4)))#4
print(len({1,2,3,10,90,100,345,789}))#8
print(len([1,2,3,4,5]))#10
25.ord():
=======
this function will return the "Unicode number of the given character"
example:
=======
print(ord("A"))
print(ord('C'))
print(ord('a'))
print(ord('b'))
print(ord('1'))
print(ord(";"))
26.chr():
=======
this function will return "the given number equitant character
in the Unicode"
example:
=======
print(chr(96))
print(chr(99))
print(chr(100))
print(chr(120))
print(chr(90))
27.any():
=======
this function will check "in the given iterable any one is
example:
=======
print(any((1,2,3,4,5,6,7)))
print(any(''))
print(any({0}))
print(any([1,2,3,0,0,0]))
28.all():
======
this function will check "in the given iterable, all values are
example:
========
print(all((1,2,3,4,5,6,7)))
print(all(''))
print(all({0}))
print(all([1,2,3,0,0,0]))
29.zip():
=======
example:
=======
l1=[1,2,3,4,5]
l2=[4,5,6,7,8]
result=list(zip(l1,l2))
print(result)
l1=[1,2,3,4,5]
l2=[4,5,6,7]
result=list(zip(l1,l2))
print(result)
l1=[1,2,3]
l2=[4,5,6,7,8]
result=list(zip(l1,l2))
print(result)
30.enemurate():
=============
l1=[1,2,3,4,5]
l2=[4,5,6,7,8]
result=list(enumerate(l1))
print(result)
result=list(enumerate([10,20,30,40,50,60,70,80,90,100]))
print(result)
result=list(enumerate("hello world"))
print(result)
31.sorted():
=========
this function will used "to sort the given iterable data and it
will give the result any iterable data in list form only"
descending order"
example:
=======
l1=[1,2,3,4,5]
l2=[4,5,100,-12,34,56]
print(sorted(l1))
print(sorted(l2))
print(sorted(l2,reverse=True))
print(sorted("hello"))
print(sorted({1,2,3,100,567,890,-45,-78}))
32.round():
=========
syntax:
======
round(float/real number)
example:
=======
print(round(1.234,0))
print(round(1.456,3))
print(round(1.5))
print(round(1.49999))
print(round(1.4567,2))
print(round(1.456789,3))
print(round(1.4))
14.built-in modules(math,random,time,datetime,os,sys):
============================================
operations"
1.ceil():
======
syntax:
======
import math as mt
mt.ceil(real/floating-point number)
example:
=======
import math as mt
print(mt.ceil(1.234))
print(mt.ceil(9.01))
print(mt.ceil(5.0))
print(mt.ceil(-4.56))
2.floor():
=======
syntax:
======
import math as mt
print(mt.floor(realnumber))
example:
=======
import math as mt
print(mt.floor(1.234))
print(mt.floor(9.01))
print(mt.floor(5.0))
print(mt.floor(-4.56))
syntax:
======
import math as mt
print(mt.trunc())
example:
=======
import math as mt
print(mt.trunc(1.234))
print(mt.trunc(9.01))
print(mt.trunc(5.0))
print(mt.trunc(-4.56))
iterable
syntax:
======
import math as mt
print(mt.fsum())
example:
=======
import math as mt
print(mt.fsum([1,2,3,4,5,6,7,8,9,10]))
print(mt.fsum(range(1,10)))
print(mt.fsum((1,2,3,4,5,6,7,8,9,10)))
print(mt.fsum({1,2,3,4,5,6,7,8,9,10}))
5.prod():
=======
this function is used to find the "product of the values of the
given iterable"
syntax:
======
import math as mt
print(mt.prod(sequence_name))
example:
=======
import math as mt
print(mt.prod([1,2,3,4,5,6,7,8,9,10]))
print(mt.prod(range(1,10)))
print(mt.prod((1,2,3,4,5,6,7,8,9,10)))
print(mt.prod({1,2,3,4,5,6,7,8,9,10}))
6.factorial():
==========
syntax:
======
import math as mt
print(mt.factorial(number))
example:
=======
import math as mt
print(mt.factorial(10))
print(mt.factorial(0))
#print(mt.factorial(1.2))
#print(mt.factorial(-10))
write a python program,find the given number is which number factorial or not?
==============================================
code:
=====
import math as mt
number=int(input("enter the number:"))
fact=1
while True:
if mt.factorial(fact)==number:
print(f"the given {number} is factorial of {fact}")
break
elif mt.factorial(fact)>number:
print(f"given {number} not any factorial of the number")
break
else:
fact+=1
7.perm():
========
syntax:
======
import math as mt
print(mt.perm(n,k))
example:
========
import math as mt
print(mt.perm(4,4))
print(mt.perm(5,4))
print(mt.perm(10,2))
print(mt.perm(10,20))
8.comb():
=======
example:
=======
import math as mt
print(mt.comb(4,4))
print(mt.comb(5,4))
print(mt.comb(10,2))
print(mt.comb(10,20))
9.sqrt():
======
example:
=======
import math as mt
print(mt.sqrt(100))
print(mt.sqrt(196))
print(mt.sqrt(80))
print((int(input("number:")))**(0.5))
10.cbrt():
=======
syntax:
======
import math as mt
print(mt.cbrt(number))
example:
=======
import math as mt
print(mt.cbrt(100))
print(mt.cbrt(196))
print(mt.cbrt(80))
print((int(input("number:")))**(1/3))
print(mt.cbrt(27))
11.exp():
=======
syntax:
======
import math as mt
print(mt.exp(number))
example:
=======
import math as mt
print(mt.exp(1))
print(mt.exp(19))
print(mt.exp(-4))
12.pow():
========
syntax:
======
import math as mt
print(mt.pow(number))
example:
=======
import math as mt
print(mt.pow(3,3))
print(mt.pow(2,10))
print(mt.pow(2,-10))
13.fabs():
========
it is used to "get the given number as always positive number"
syntax:
======
import math as mt
print(mt.fabs(number))
example:
=======
import math as mt
print(mt.fabs(-10))
print(mt.fabs(10))
print(abs(10))
print(abs(-10))
14.fmod():
========
syntax:
======
import math as mt
result=mt.fmod(num1,num2)
example:
=======
import math as mt
print(mt.fmod(10,20))#10.0
print(mt.fmod(3,4))#3.0
print(mt.fmod(10,5))#0.0
#divmod()<=== it will return both quotient and remainder
print(divmod(10,20))#(10//20,10%20)==>(0,10)
print(divmod(20,4))#(5,0)
note:
=====
15.gcd():
=======
numbers"
syntax:
======
import math as mt
print(mt.gcd(num1,num2))
example:
========
import math as mt
print(mt.gcd(10,20))#10
print(mt.gcd(16,64))#16
print(mt.gcd(100,200))#100
print(mt.gcd(100,0))#100
print(mt.gcd(12,56))#4
sin()
cos()
tan()
asin()
acos()
atan()
sinh()
cosh()
tanh()
log()
log10()
log2()
math.pi
math.tau
math.inf
math.nan
example:
=======
import math as mt
print(mt.log(1))
print(mt.log(12))
print(mt.log10(1234))
print(mt.log2(123))
#shoutcut using log10(),we can find number length
print(int(mt.log10(123456789))+1)
print(mt.log10(123456789))
print(int(mt.log10(int(input("Enter:"))))+1)
example:
=======
import math as mt
print(mt.e)#2.7
print(mt.tau)
print(mt.inf)
print(mt.nan)
print(10+mt.inf)
print(10-mt.inf)
print(mt.pi)
2.random module:
==============
1.random():
==========
syntax:
======
import random as rd
print(rd.random())
example:
=======
import random as rd
print(rd.random())
print(int(rd.random()*10))
print(int(rd.random()*100))
print(int(rd.random()*1000))
print(int(rd.random()*10000))
print(int(rd.random()*100000))
2.randint():
=========
syntax:
======
import random as rd
print(rd.randrange(start,end))
example:
=======
import random as rd
print(rd.randint(1,10))
print(rd.randint(1,1000))
print(rd.randint(1,10000))
print(rd.randint(1,100000))
print(rd.randint(1,1000000))
3.randrange():
===========
this function will give the "integer random number" and this
syntax:
======
import random as rd
print(rd.randrange(start,end,step))
example:
========
import random as rd
print(rd.randrange(1,10,2))
print(rd.randrange(10,100,10))
print(rd.randrange(1,100,23))
4.choice():
========
import random as rd
print(rd.choice(range(1,10)))
print(rd.choice([1,2,3,4,5,6,7,8,9,10]))
print(rd.choice((10,20,30,40,50,60,70,80,90,100)))
print(rd.choice("helloworld"))
5.shuffle()
========
this function will shuffle the data "elements which are available
example:
=======
import random as rd
l1=[1,2,3,4,5,6,7,8,9,10]
rd.shuffle(l1)
print(l1)
rd.shuffle(l1)
print(l1)
rd.shuffle(l1)
print(l1)
6.uniform():
=========
example:
=======
import random as rd
print(rd.uniform(10.0,20.0))
print(rd.uniform(10,20))
print(rd.uniform(0.5,0.8))
7.sample():
=========
example:
=======
import random as rd
l1=[10,20,30,40,50,60,70,80,90,100]
print(rd.sample(l1,3))
print(rd.sample(l1,4))
print(rd.sample(l1,8))
print(rd.sample(l1,1))
having length as "10" digits and number must start with even
code:
====
import random as rd
res=''
res+=str(rd.randrange(0,10,2))
while len(res)!=9:
res+=str(rd.randrange(1,10))
res+=str(rd.randrange(1,10,2))
print(res)
which is does not have digits like 2,3,4,5 and length of the
code:
====
import random as rd
res=''
while len(res)!=12:
i=str(rd.randrange(1,10))
if i not in "2345":
res+=i
print(res)
code:
====
import random as rd
res=''
while len(res)!=10:
i=chr(rd.randrange(97,123))
if i in "aeiou":
res+=i
else:
res+=str(rd.randrange(1,10))
print(res)
write a python program to generate a random number which
code:
====
import random as rd
res=''
while len(res)<=15:
if len(res)<15:res+=chr(rd.randrange(97,123))
else:break
if len(res)<15:res+=str(rd.randrange(1,10))
else:break
print(res)
3.os module:
===========
files"
example-1:
=========
import os
#get the current working directory path
print(os.getcwd())
example-2:
=========
import os
#get the current working directory path
print(os.getcwd())
#create the directory
os.mkdir('sample4')
os.mkdir("sample5")
#list the all directories
print(os.listdir())
example-3:
=========
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical/sample")
#get the current working directory path
print(os.getcwd())
#create the directory
os.mkdir("mysample")
example-4:
=========
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical/sample")
#get the current working directory path
print(os.getcwd())
#remove the directory
os.rmdir("mysample")
example-5:
=========
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical")
path="D:/training/360digrii/python FP4_2024/python_practical"
#get the current working directory path
print(os.getcwd())
#list the directories in the current path
res=os.listdir()
for i in res:
if os.path.isdir(i):
print(i)
#listn the directories in the current path
res=list(os.scandir())
for i in res:
if os.path.isdir(i):
print(i)
example-6:
=========
given path:
=================================================import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical")
path="D:/training/360digrii/python FP4_2024/python_practical"
#get the current working directory path
print(os.getcwd())
#list the directories in the current path
res=os.listdir()
for i in res:
if os.path.isdir(i):
os.rmdir(i)
example-7:
=========
code:
====
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical")
path="D:/training/360digrii/python FP4_2024/python_practical"
#get the current working directory path
print(os.getcwd())
#create the 20 directories in the given path
for i in range(1,21):
os.mkdir("sample"+str(i))
example-8:
=========
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical")
path="D:/training/360digrii/python FP4_2024/python_practical"
#get the current working directory path
print(os.getcwd())
#rename the directory
os.rename("sample1","mysample1")
example-9:
=========
rename the all directories which are existing in the given path,
import os
#get the current working directory path
print(os.getcwd())
#change the directory path
os.chdir("D:/training/360digrii/python FP4_2024/python_practical")
path="D:/training/360digrii/python FP4_2024/python_practical"
#get the current working directory path
print(os.getcwd())
#rename the directory
for i in os.listdir():
if os.path.isdir(i):
os.rename(i,"my"+i)
memory"
function
function
python
note:
====
when we are open any file in python, while opening the file we
mode of the file will represents "what type of operation ,we are
file:
=================================================
in python, when we writing the any data into the file, first the
entire file content will be removed and place the content what
in the case of both append and write operation, file need not be
there inside the folder, if the file is not there inside the working
example-1:
=========
import os
#get the current working directory path
print(os.getcwd())
#open a file
fp=open("sample.txt","w")
#here we are write the content in the file using write() fucntion
fp.write("this is data is added")
#after performing the operations,we need to close the file
fp.close()
example-2:
========
import os
#get the current working directory path
print(os.getcwd())
#open a file
fp=open("sample.txt","a")
#here we are append the content in the file using write() fucntion
fp.write("this is data is added")
#after performing the operations,we need to close the file
fp.close()
read() function will read the data from the file, character by
character
code:
====
import os
#get the current working directory path
print(os.getcwd())
#open a file
fp=open("sample.txt","r")
#here we are read the content in the file using read() fucntion
data=fp.read(10)
print(data)
data=fp.read(10)
print(data)
data=fp.read()
print(data)
#after performing the operations,we need to close the file
fp.close()
when we are read the file content using readline(), this function
code:
====
import os
#get the current working directory path
print(os.getcwd())
#open a file
fp=open("sample.txt","r")
#here we are read the content in the file using read() fucntion
data=fp.readline()
print(data)
data=fp.readline(12)
print(data)
#after performing the operations,we need to close the file
fp.close()
example:
========
import os
#get the current working directory path
print(os.getcwd())
#open a file
fp=open("sample.txt","r")
#here we are read the content in the file using read() fucntion
data=fp.readlines()
print(data)
#after performing the operations,we need to close the file
fp.close()
example:
=======
any file using "with" ,it can be close automatically when we use
with
example-1:
=========
import os
#get the current working directory path
print(os.getcwd())
with open("sample.txt") as fp:
#here we are read the content in the file using read() fucntion
data=fp.readlines()
print(data)
print(fp.readlines())
example:
=======
import os
#get the current working directory path
print(os.getcwd())
with open("sample.txt") as fp:
#here we are read the content in the file using read() fucntion
print(fp.seek(5))
data=fp.read(5)
print(data)
to get the file pointer position in python ,we will use a function
called "tell()"
example:
=======
import os
#get the current working directory path
print(os.getcwd())
with open("sample.txt") as fp:
#get the file pointer position
print(fp.tell())
#set the file pointer position
fp.seek(5)
#get the file pointer position
print(fp.tell())
#set the file pointer position
fp.seek(15)
#get the file pointer position
print(fp.tell())#15
#set the file pointer position
fp.seek(5+fp.tell())
#get the file pointer position
print(fp.tell())
example:
=======
import os
#get the current working directory path
print(os.getcwd())
with open("sample.txt") as fp:
#to rename the file,we will rename()
os.rename("sample2.txt","sample_new.txt")
example:
=======
import os
#get the current working directory path
print(os.getcwd())
with open("sample.txt") as fp:
#to remove the file,we will remove()
os.remove("sample_new.txt")
following functions:
================================================
example-1:
=========
example-2:
=========
time module:
==========
program
import time as t
this module will have the following important functions:
============================================
function
import time as t
t.sleep(time_period)
example:
=======
import time as t
for i in range(1,11):
t.sleep(3)
print(i)
time() function will give the current time an date in the seconds
example:
=======
import time as t
#get the time in seconds
seconds=t.time()
print(seconds)
#convert the seconds into time
result=t.localtime(seconds)
print(result)
print(result.tm_year)
print(result.tm_mon)
print(result.tm_mday)
print(result.tm_hour)
print(result.tm_min)
print(result.tm_sec)
print(f"{result.tm_year}/{result.tm_mon}/{result.tm_mday}")
4.ctime():
========
using this ,we can able to get convert the given seconds time
example:
========
import time as t
#get the time in seconds
seconds=t.time()
print(seconds)
#convert the seconds into time usinf ctime()
result=t.ctime(seconds)
print(result)
5.process_time():
=============
execute a process"
example:
=======
import time as t
#start time
start=t.process_time()
#logic
for i in range(1,10):
t.sleep(0.3)
print(i)
#end-time
end=t.process_time()
print(f"result:{start-end}")
6.perf_counter():
=============
example:
=======
import time as t
#start time
start=t.perf_counter()
#logic
for i in range(1,10):
t.sleep(0.3)
print(i)
#end-time
end=t.perf_counter()
print(f"result:{end-start}")
7.strf_time():
==========
it is used to format the given time and date in the user defined
way
example:
=======
import time as t
#get the time in seconds
seconds=t.time()
#convert the seconds into human readble form
result=t.ctime(seconds)
print(result)
print(t.strftime("%d"))
print(t.strftime("%m"))
print(t.strftime("%Y"))
print(t.strftime("%H"))
print(t.strftime("%M"))
print(t.strftime("%S"))
print(t.strftime("%d/%m/%Y"))
print(t.strftime("%H:%M:%S"))
datetime module:
==============
using this module we can able manipulate the both date and
time
1.date class
2.time class
3.timedelta class
example-1:
=========
example-3:
=========
from datetime import datetime,date,time
t=datetime.now()
print(t)
datetime2=datetime.combine(date(2024,7,9),time(15,20,20))
print(datetime2)
print(t.strftime("%H"))
print(t.strftime("%d"))
print(t.strftime("%m"))
print(t.strftime("%Y"))
print(t.strftime("%H"))
print(t.strftime("%M"))
print(t.strftime("%S"))
print(t.strftime("%d/%m/%Y"))
print(t.strftime("%H:%M:%S"))
example-4:
=========
example-5:
=========
example-6:
=========
from datetime import date,timedelta
t=date(2024,7,9)
t1=date(2024,3,9)
print(t)
print((((t-t1).days)//30)+1)
example-7:
=========
calculate the age of the person by taking year, month and day
code:
====
from datetime import date
year=int(input("enter the year"))
month=int(input("enter the month:"))
day=int(input("enter the day:"))
t=date(year,month,day)
t1=date.today()
if year>1900 and year<=2100:
if month>=1 and month<=12:
if day>=1 and day<=31:
print(f"{((t1-t).days//365)}years")
else:
print("please give the valid day number")
else:
print("please give valid month number")
else:
print("please give the valid year number")
sys module:
=========
import sys
object
example:
========
import sys
a=10
b=1.234
c="hello"
print(sys.getsizeof(a))
print(sys.getsizeof(b))
print(sys.getsizeof(c))
2.sys.exit():
=========
example:
=======
import sys
for i in range(1,10):
if i>5:
sys.exit()
else:
print(i)
print("the value of i is:",i)
note:
====
in python ,we will also have built-in functions called "exit() and
example:
=======
import sys
print(sys.platform)#platform name
print(sys.version)#python inpreter version
4.sys.argv:
=========
1.interactive mode
2.shell mode
example-1:
=========
import sys
a=int(input("enter the value for a:"))
b=int(input("enter the value for b:"))
print(a+b)
print(sys.argv)#argv talks about command line arguments
print(type(sys.argv))
print(sys.argv[0])
example-2:
=========
import sys
print(sys.argv)#argv talks about command line arguments
for i in sys.argv[1:]:
print(i)
deallocation"
objects"
example-1:
=========
import sys
a=10
b=20
c=a
print(a,id(a))
print(b,id(b))
print(c,id(c))
print(sys.getrefcount(a))
print(sys.getrefcount(b))
print(sys.getrefcount(c))
d=a+b
print(sys.getrefcount(a))
e=a
print(sys.getrefcount(a))
f=b
print(sys.getrefcount(b))
print(sys.getrefcount(c))
example-2
========
import sys
a=10
print(sys.getrefcount(a))
b=20
c=a
d=1.234
print(sys.getrefcount(d))
g=a
b=c
print(sys.getrefcount(a))
print(sys.getrefcount(c))
print(sys.getrefcount(b))
print(sys.getrefcount(d))
result=d
print(sys.getrefcount(d))
#removing object using del
del b
print(sys.getrefcount(a))
del a
print(sys.getrefcount(c))
how to set the recursion limit for any program using python:
===============================================
limit or depth
limit or depth(minimum:32)
example-1:
=========
import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(50)
print(sys.getrecursionlimit())
example-2:
=========
import sys
num=1
print(sys.getrecursionlimit())
sys.setrecursionlimit(100)
print(sys.getrecursionlimit())
1.list:
=====
when we are working with variable, variable can hold only one
and delete)
example-1:
=========
l1=[1,2,3,4,5,6,7]
print(l1)
l2=[]
print(l2)
l3=[1,2,1,2,1,2]
print(l3)
l4=[1,2,3,4,1.2,3.4,5.6,7.8,"hello"]
print(l4)
l5=list(range(10,110,10))
print(l5)
l6=list("hello")
print(l6)
l7=list((1,))
print(l7)
example-2:
=========
l1=[1,2,3,4,5,6,7]
for i in l1:
print(i,end=" ")
print()
l4=[1,2,3,4,1.2,3.4,5.6,7.8,"hello"]
for i in l4:
print(i,end=" ")
example-3:
=========
"""indexing with lists in python"""
l1=[1,2,3,4,5,6,7]
print(l1[0])
print(l1[-1])
print(l1[4])
l7=[10,20,30,40,50,60,70,80,90,100]
print(l7[-1])
print(l7[4])
print(l7[3])
print(l7[9])
example-4:
=========
"""slicing with lists in python"""
l1=[1,2,3,4,5,6,7]
print(l1[0:])
print(l1[3:])
print(l1[:9])
print(l1[3:8])
l7=[10,20,30,40,50,60,70,80,90,100]
print(l7[-5:-1])
print(l7[4:-3])
print(l1[::-1])
print(l7[::-2])
1.insertion
2.updation
3.deletion
following functions:
=================================================
l1=[1,2,3,4,5,6,7]
l1.append(100)
print(l1)
l1.append(200)
print(l1)
l1.append(300)
print(l1)
l1.insert(2,2000)
print(l1)
l1.insert(1000,4000)
print(l1)
l1.insert(-10000,10000)
print(l1)
2.updation:
=========
example:
=======
l1=[1,2,3,4,5,6,7]
l1[3]=300
print(l1)
l1[-1]=1000
print(l1)
l1[4]=600
print(l1)
3.deletion:
========
to delete the elements from the list, in python we will use the
following functions:
=================================================
list
index
example:
=======
l1=[1,2,3,4,5,6,7,8,9,10,100]
l1.pop()
print(l1)
l1.pop()
print(l1)
l1.pop(3)#it remove element at index 3
print(l1)
l1.remove(9)#it remove element 9
print(l1)
del l1[0]
del l1[2]
print(l1)
l1.clear()
print(l1)
example:
=======
l1=[1,2,3,4,5,6,7,8,9,10,100]
print(l1.index(1))
print(l1.index(100))
print(l1.index(5))
example:
=======
l1=[1,2,3,4,5,6,7,8,9,10,100,1,5,100]
print(l1.count(1))
print(l1.count(100))
print(l1.count(5))
print(l1.count(1000))
descending order
example:
========
l1=[1,2,3,4,5,-6,-7,-8,9,10,100,1,5,100]
l1.sort()#ascending order
print(l1)
l1.sort(reverse=True)#descending order
print(l1)
example:
========
l1=[1,2,3,4,5,-6,-7,-8,9,10,100,1,5,100]
l1.reverse()
print(l1)
sorted():
=======
using this we can able to sort the given list data "either in
example:
=======
l1=[1,2,3,900600,-10,-23,90,87,654]
print(l1)
print(sorted(l1))
print(sorted(l1,reverse=True))
reversed():
========
example:
=======
l1=[1,2,3,900600,-10,-23,90,87,654]
print(l1)
print(list(reversed(l1)))
example:
=======
l1=[[1,2,3,4],[5,6,7,8],[10,11,12,13]]
print(l1)
print(len(l1))#3
for i in l1:
print(i)
print(l1[0])#[1,2,3,4]
print(l1[1])#[5,6,7,8]
print(l1[2])#[10,11,12,13]
for i in l1:
for j in i:
print(j,end=" ")
print()
print(l1[2][3])
print(l1[1][3])
syntax:
=====
example:
=======
l1=[i for i in range(10,110,10)]
print(l1)
l1=[i for i in "hello world"]
print(l1)
l1=[i for i in (1,2,3,4,5,6,7,8,9,10)]
print(l1)
l1=[i for i in {1,2,3,4,5,6,7,8,9,10}]
print(l1)
l1=[i for i in [1,2,3,4,5,6,7,8,9,10]]
print(l1)
l1=[i for i in range(1,11) if i%2==0]
print(l1)
l1=[[j for j in range(1,i+1)] for i in range(2,11,2)]
print(l1)
extend():
=======
example:
=======
l1=[i for i in range(10,110,10)]
l2=[i for i in "hello world"]
print(l1+l2)
l1.extend(l2)
print(l1)
2.tuple:
======
when we want to store "multiple values under single name", in
example:
=======
t1=()
print(t1)#empty tuple
t1=(1,2,3,4,5,6,7,8,9,10)
print(t1)
t2=(1,2,3,4,12.34,5.678,9.0123)
print(t2)
t3=(1,2,3,1.234,"hello","hai")
print(t3)
t4=tuple(range(1,10,3))
print(t4)
t5=1,2,3,4,5,6,7,8,9,10,11
print(t5)
t6=1,2,3,1.345,"abc","hello"
print(t6)
print(t2[0])
print(t2[-1])
print(t3[1:5])
times repeated
4.sorted()<=== it used to tuple elements in the form of list
of list
example:
=======
t1=(10,20,30,40,50,60,70,80,90,100)
print(t1)
print(t1.index(100))
print(t1.index(30))
print(t1.index(60))
print(t1.count(10))
print(t1.count(100))
print(t1.count(1000))
print(sorted(t1,reverse=True))
print(tuple(reversed(t1)))
print(len(t1))
t2=list(t1).copy()
print(t2)
nested tuples:
===========
example:
=======
t1=((10,20,30,40),(50,60,70),(80,90,100))
print(t1)
print(t1[0])
print(t1[1])
print(t1[2])
for i in t1:
print(i)
print(len(t1))#3
print(t1[2][2])
example:
=======
t1=((10,20,30,40),(50,60,70),(80,90,100))
print(list(t1))
print(set(t1))
print(tuple([1,2,3,4,5]))
print(tuple({1,2,3,4,5}))
3.string:
=======
list or tuple)
as "doc string"
example-1:
=========
s1=""
s2='hello'
s3="hello world"
s4='''hello'''
s5="""hello"""
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)
for i in s3:
print(i)
example-2:
=========
s3="hello world"
print(s3[0])
print(s3[-1])
print(s3[5])
print(s3[-5])
print(s3[4:])
print(s3[:6])
print(s3[-5:-2])
print(s3[::-1])
string"
of the string
of the string
strip()=lstrip()+rstrip()
True
True
character data
"True"
"True"
example-3:
=========
s3="hello world hello hello"
print(len(s3))
print(len(''))
print(len(" "))
print(s3.lower())
print(s3.upper())
print(s3.swapcase())
print(s3.count("hello"))
print(s3.index("hello"))
print(s3[0].upper()+s3[1:])
example-4:
=========
s3="hello world hello hello"
s4=s3[::-1]
print(s3)
print(s4)
s2="hello world hello world"
print(s2.replace("hello","Hello",1))
s1=" hellp"
print(s1)
print(len(s1))
s1=s1.lstrip()
print(s1)
print(len(s1))
s1="hello "
print(s1)
print(len(s1))
s1=s1.rstrip()
print(s1)
print(len(s1))
s1=" hello "
print(len(s1))
s1=s1.strip()#lstrip()+rstrip()
print(s1)
print(len(s1))
example-5:
=========
example-6:
=========
example-7:
=========
example-8:
=========
example-9:
=========
example-10:
==========
given number
example:
=======
11223344678====>1234678
code:
====
num=int(input("number:"))
res=''
for i in str(num):
if i not in res:
res+=i
print(res)
example:
=======
[1,2,3,4,5,6,10,2,3,4,5,6,7,8]
ouput:
======
[1,2,3,4,5,6,10,7,8]
code:
====
l1=[1,2,3,4,5,6,10,2,3,4,5,6,7,8]
res=[]
for i in l1:
if i not in res:
res+=[i]
print(res)
code:
====
l1=[10,20,30,40,50,60,70,80]
pos=int(input("enter the position:"))
ele=int(input("enter the element:"))
if pos<=len(l1):
l1=l1[:pos-1]+[ele]+l1[pos-1:]
print(l1)
else:
print("given position is out of the range")
4.set:
=========
set is a "collection of unique or distinct elements"
set can not follow indexing and slicing like list or tuple
set is a un-orderd collection(it will not store the element in
1.union
2.intersection
3.difference(complement)
4.symmetric difference
example-1:
=========
s1={1,2,3,4,5,6,7,8,9,10}
s2={}
s3={1,2,3,1.2,3.56,"hello","hai"}
s4={1,2,1,2,1,2}
s5=set()
print(s1)
print(s2)
print(s3)
print(s4)
print(len(s1))
print(len(s3))
print(type(s2))
print(type(s5))
print(type(s1))
print(len(s5))
example-2:
=========
example-4:
=========
example-5:
========
example-6:
=========
example-7:
=========
example-8:
=========
example-9:
=========
print({1,2,3,4,5}>{6,7,8,9})
print({1,2,3,4,5,6,7,8,9}>{6,7,8,9})
print({6,7,8,9}<{1,2,3,4,5,6,7,8,9,10,11,12})
print({6,7,8,9}<{1,2,3,4,5,6,10})
print({1,2,3,4}=={4,3,2,1})
example-10:
==========
print({1,2,3,4,5}.issuperset({6,7,8,9}))
print({1,2,3,4,5,6,7,8,9}.issubset({6,7,8,9}))
print({6,7,8,9}.issubset({1,2,3,4,5,6,7,8,9,10,11,12}))
print({6,7,8,9}.isdisjoint({1,2,3,4,5,6,10}))
print({1,2,3,4}.isdisjoint({14,13,21,11}))
5.dictionary
16.exception handling