0% found this document useful (0 votes)
64 views32 pages

Sum of Two Numbers: "Welcome"

The document contains Python code snippets demonstrating various programming concepts like input/output, arithmetic operations, conditional statements, functions etc. Some examples include calculating sum, area, average; checking odd/even, divisibility; income tax calculation; greeting messages; distance between points; type conversions between integer and float. The code snippets are used to teach Python programming concepts through interactive examples.

Uploaded by

mani maran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views32 pages

Sum of Two Numbers: "Welcome"

The document contains Python code snippets demonstrating various programming concepts like input/output, arithmetic operations, conditional statements, functions etc. Some examples include calculating sum, area, average; checking odd/even, divisibility; income tax calculation; greeting messages; distance between points; type conversions between integer and float. The code snippets are used to teach Python programming concepts through interactive examples.

Uploaded by

mani maran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

2/17/2020 2019111048

In [97]:

print("welcome")

welcome

sum of two numbers


In [3]:

a=int(input("enter the number:"))


b=int(input("enter the number:"))
c=a+b
print("sum=",c)

sum= 12

To find the area and perimeter of a circle


In [4]:

a=int(input("enter the length:"))


b=int(input("enter the width:"))
c=a*b
d=2*(a+b)
print("Area=",c)
print("perimeter=",d)

Area= 63
perimeter= 32

To find the average of three numbers


In [8]:

A=int(input("Enter the first no:"))


B=int(input("Enter the second no:"))
C=int(input("Enter the third no:"))
D=(A+B+C)/3
print("Average=",D)

Average= 7.0

Evaluating the expression by getting the values for each


variable

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 1/32
2/17/2020 2019111048

In [10]:

a=int(input("Enter the value:"))


b=int(input("Enter the value:"))
c=int(input("Enter the value:"))
d=int(input("Enter the value:"))
f=int(input("Enter the value:"))
E={(a*b)+(c/d)-f}
print("Answer=",E)

Answer= {10.0}

To find the distance walked by a person around a circular


ground
In [15]:

d=int(input("enter the diameter:"))


pi=3.14
C=pi*d
print("distance=",C)

distance= 31.400000000000002

To calculate the area of the rectangular plot of land in acres


In [16]:

l=int(input("length:"))
b=int(input("breadth:"))
A=l*b*0.000247105
print("Area=",A,"acres")

Area= 24.7105 acres

To demonstrate the arithmetic operations

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 2/32
2/17/2020 2019111048

In [33]:

a=int(input("enter the number"))


b=int(input("enter the number"))
A=a+b
B=a-b
C=a*b
D=a/b
E=a%b
print("The addition value is:",A)
print("The subraction value is:",B)
print("The multplication value is:",c)
print("The division value is:",D)
print("The modulus value is:",E)

The addition value is: 135


The subraction value is: 45
The multplication value is: 15625
The division value is: 2.0
The modulus value is: 0

To convert celsius to fahrenheit


In [18]:

t=int(input("Enter temp in celsius:"))


T=t*1.8+32
print("Temp in fahrenheit=",T,"F")

Temp in fahrenheit= 41.0 F

To find the area of a circle


In [21]:

r=int(input("enter the radius:"))


pi=3.14
A=pi*r**2
print("Area=",A)

Area= 314.0

To find the simple and compound interest

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 3/32
2/17/2020 2019111048

In [32]:

p=int(input("enter the value:"))


n=int(input("enter the value:"))
r=float(input("enter the value:"))
Si=p*n*r/100
amt=p*pow((1+r/100),n)
Ci=amt-p
print("simple interest=",Si)
print("Compound interest=",Ci)

simple interest= 1500.0


Compound interest= 1576.2500000000018

To calculate the power of the given value


In [34]:

a=int(input("enter the first no"))


b=int(input("enter the second no"))
c=a**b
print("Answer=",c)

Answer= 3125

The biggest no among a,b,c


In [40]:

a=int(input("enter the number"))


b=int(input("enter the number"))
c=int(input("enter the number"))
big=a
if(a>big):
big=a
if(b>big):
big=b
if(c>big):
big=c
print("The greatest no is:",big)

The greatest no is: 5555555

Odd or Even

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 4/32
2/17/2020 2019111048

In [6]:

a=int(input("enter the number"))


if(a%2==0):
print("a is even")
else:
print("a is odd")

a is even

Find the given no is divisible by 7 or not


In [11]:

a=int(input("enter the number"))


if(a%7==0):
print("a is divisible by 7")
else:
print("a is not divisible by 7")

a is divisible by 7

Income tax
In [14]:

i=int(input("enter the income"))


if(i<500000):
print("Nil tax")
if(500000<i<1000000):
print("10% of the amount exceeding 500000")
if(1000000<i<2000000):
print("100000 plus 20% of the income exceeding 100000")
else:
print("400000 plus 20% of the amount exceeding 2000000")

400000 plus 20% of the amount exceeding 2000000

Print a message
In [15]:

print("Welcome to python programming lab")

Welcome to python programming lab

Print a message in single,double,triple quotes

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 5/32
2/17/2020 2019111048

In [32]:

print("\'Hi'")
print("'\'Hi'\'")
print("'\'\'Hi'\'\'")

'Hi'
''Hi''
'''Hi'''

To display the address of the user


In [34]:

a=int(input("enter the door no:"))


b=str(input("enter the street name:"))
c=str(input("enter the city:"))
d=str(input("enter the district:"))
e=int(input("enter the pincode:"))
print("",a,b,c,d,e)
print("",a)
print("",b)
print("",c)
print("",d)
print("",e)

65 Thiyagi venkatachalam street, cumbum, Theni, 625516


65
Thiyagi venkatachalam street,
cumbum,
Theni,
625516

Greeting message
In [36]:

a=str(input("Enter the first name"))


b=str(input("Enter the last name"))
print("Greetings!!!",a,b)

Greetings!!! Mani Maran

Name and Welcome message

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 6/32
2/17/2020 2019111048

In [37]:

a=str(input("Enter the first name"))


b=str(input("Enter the last name"))
print("Hello",a,b)
print("Welcome to python")

Hello Mani Maran


Welcome to python

Print name in reverse order


In [38]:

a=str(input("Enter the first name"))


b=str(input("Enter the last name"))
print("",b,a)

Maran Mani

Assignment statement
In [81]:

width=17
height=12.0
delimiter='.'
a=width/2
b=width/2.0
c=height/3
d=1+2+5
e=delimiter*5
print("a=",a,",It is a float no")
print("b=",b,",It is a float no")
print("c=",c,",It is a float no")
print("d=",d,",It is a integer no ")
print("e=",e)

a= 8.5 ,It is a float no


b= 8.5 ,It is a float no
c= 4.0 ,It is a float no
d= 8 ,It is a integer no
e= .....

Volume of the sphere

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 7/32
2/17/2020 2019111048

In [49]:

r=int(input("Enter the radius"))


pi=3.14
V=4/3*pi*(r**3)
print("Volume=",V)

Volume= 523.3333333333334

To find square root of a number


In [67]:

a=int(input("Enter the number"))


R=a**(1/2)
print("Root=",R)

Root= 2.8284271247461903

Wholesale copy for 60 books


In [91]:

a=24.95
b=3
c=0.75
d=a*40/100
e=a-d
n=int(input("enter the number"))
g=n*e
f=b+(n-1)*c
A=f+g
print("Total wholesale cost=",A)

Total wholesale cost= 945.4499999999999

Distance between two points


In [66]:

xi=int(input("Enter the first x coordinates="))


xf=int(input("Enter the 2nd x quardinates="))
yi=int(input("Enter the first y quardinates="))
yf=int(input("Enter the 2nd y quardinates="))
d=(xf-xi)**2+(yf-yi)**2
e=d**(1/2)
print("Distance=",e)

Distance= 2.8284271247461903

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 8/32
2/17/2020 2019111048

To convert integer into float


In [72]:

a=int(input("enter the number="))


b=float(a)
print("The corresponding floating point number is:",b)

The corresponding floating point number is: 8.0

To convert float into integer


In [73]:

a=float(input("enter the number:"))


b=int(a)
print("The corresponding integer is:",b)

The corresponding integer is: 7

The sum of two numbers without using integer


In [76]:

a=float(input("Enter the number="))


b=float(input("Enter the number="))
c=a+b
print("Sum=",c)

Sum= 2.68003675245

Assign the sum value to an integer


In [77]:

a=float(input("Enter the number"))


b=float(input("Enter the number"))
c=a+b
d=int(c)
print("a=",a)
print("b=",b)
print("Sum=",d)

a= 8.22154
b= 0.02222222
Sum= 8

Print the digit at one's place of a number

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 9/32
2/17/2020 2019111048

In [82]:

a=int(input("Enter the no"))


b=a%10
print("The one's place of the number is:",b)

The one's place of the number is: 7

To swap two number using a temporary variable


In [84]:

a=int(input("Enter the no="))


b=int(input("Enter the no="))
print("a=",b)
print("b=",a)

a= 4444
b= 66666

To find the total amount in a piggy bank


In [85]:

a=10
b=5
c=2
d=1
E=a+b+c+d
print("Total amount=",E)

Total amount= 18

Application of += operator on strings


In [86]:

a=str(input("Enter the 1st name"))


b=str(input("Enter the 2nd name"))
a+=b
print("Answer=",a)

Answer= Manimaran

Addition and Multiplication on string

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 10/32
2/17/2020 2019111048

In [88]:

a=str(input("Enter the name"))


b=str(input("Enter the name"))
c=int(input("Enter the no"))
d=a*c
e=a+b
print("Addition=",e)
print("Multiplication=",d)

Addition= Manimaran
Multiplication= ManiManiManiManiManiManiManiMani

Solution of the expressions


In [89]:

X=250+130-70
Y=(32+5.2-3)*10
Z=100%(45/2)
A=(40+20)*30/10
B=((40+20)*30)/10
C=(40+20)*(30/10)
D=40+(20*30)/10
E=40+((20*30)/10)
F=40+(20*30/10)
print("X=",X)
print("Y=",Y)
print("Z=",Z)
print("A=",A)
print("B=",B)
print("C=",C)
print("D=",D)
print("E=",E)
print("F=",F)

X= 310
Y= 342.0
Z= 10.0
A= 180.0
B= 180.0
C= 180.0
D= 100.0
E= 100.0
F= 100.0

Fahrenheit to celsius

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 11/32
2/17/2020 2019111048

In [92]:

T=int(input("enter the temp in fahrenheit"))


t=(T-32)*5/9
print("Temperature in celsius=",t)

Temperature in celsius= -15.0

Time to get breakfast


In [117]:

a=6.52
b=8*60+15
c=3*7*60+3*12
d=(b+c)/3600
e=d+a
print("Time of breakfast=",e,"AM")

Time of breakfast= 7.017499999999999 AM

GCD
In [44]:

a=int(input("enter no"))
b=int(input("enter no"))
if(a>b):
dividend=a
divisor=b
else:
dividend=b
divisor=a
while(divisor!=0):
rem=dividend%divisor
dividend=divisor
divisor=rem
print("GCD of",a,b,"is =",dividend)

GCD of 16 64 is = 16

Reassign values
In [107]:

a=int(input("enter the no"))


b=int(input("enter the no"))
print("a=",b)

a= 7

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 12/32
2/17/2020 2019111048

2ND EXERCISE

DECREMENT OF NUMBER

a=int(input("enter the number")) if(a<0): a=a-1 print("a=",a)

Divisible by 11
In [119]:

a=int(input("enter the number"))


if(a%11==0):
print("a is divisible by 11")
else:
print("a is not divisible by 11")

a is divisible by 11

Two numbers are equal or not


In [120]:

a=int(input("enter the no"))


b=int(input("enter the no"))
if(a==b):
print("a and b are equal")
else:
print("a and b are not equal")

a and b are not equal

Eligible for vote or not


In [125]:

a=int(input("enter the age"))


if(a<18):
a=18-a
print("The user is not eligible to vote")
print("",a,"years are left to be eligible")
else:
print("The user is eligible to vote")

The user is eligible to vote

Leap year or not


https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 13/32
2/17/2020 2019111048

In [126]:

a=int(input("enter the year"))


if(a%4==0):
print("The year is a leap year")
else:
print("The year is not a leap year")

The year is a leap year

Input is positive or else


In [133]:

a=int(input("enter the number"))


if(a>0):
print("The given no is positive")
elif(a==0):
print("The given no is zero")
else:
print("The given no is negative")

The given no is positive

Greatest of three numbers


In [135]:

a=int(input("enter the no"))


b=int(input("enter the no"))
c=int(input("enter the no"))
big=a
if(a>big):
big=a
if(b>big):
big=b
if(c>big):
big=c
print("",big,"is the biggest number")

333333 is the biggest number

Displaying corresponding day

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 14/32
2/17/2020 2019111048

In [136]:

a=int(input("enter the no"))


if(a==1):
print("Sunday")
elif(a==2):
print("Tuesday")
elif(a==3):
print("Wednesday")
elif(a==4):
print("Thursday")
elif(a==5):
print("Friday")
elif(a==7):
print("Saturday")
else:
print("Wrong entry")

Saturday

Vowel or not
In [139]:

d=str(input("enter the character"))


if(d=='a'or d=='e'or d=='i'or d=='o'or d=='u'):
print("It is a vowel")
else:
print("It is not a vowel")

It is a vowel

To calculate the roots of equation


In [150]:

a=int(input("enter the co-efficient of x^2"))


b=int(input("enter the co-efficient of x"))
c=int(input("enter the constant"))
d=(b**2)-4*a*c
g=d**0.5
e=(-b+g)/(2*a)
f=(-b-g)/(2*a)
print("Root 1=",e)
print("Root 2=",f)

Root 1= 2.0
Root 2= 2.0

Enter character and display message

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 15/32
2/17/2020 2019111048

In [156]:

d=str(input("enter the character"))


if(d=='O'):
print("Outstanding")
elif(d=='A'):
print("Very good")
elif(d=='B'):
print("Good")
elif(d=='C'):
print("Average")
elif(d=='F'):
print("Fail")
else:
print("Wrong entry")

Very good

No of days in a month
In [157]:

n=int(input("enter the year"))


a=str(input("enter the month"))
if(a=='january'or a=='march'or a=='july'or a=='august'or a=='october'or a=='december'):
print("Total day is 31")
elif(a=='april'or a=='june'or a=='september'or a=='novomber'):
print("Total day is 30")
elif(a=='february'):
if(n%4==0):
print("Total day is 29")
else:
print("Total day is 28")

Total day is 29

Alphabet, Number, Blank space

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 16/32
2/17/2020 2019111048

In [162]:

a=input("enter something")
b=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v'
,'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z']
for i in b:
if(a==i):
m=i
if(a==m):
print("It is an alphabet")
elif(a==' '):
print("It is a blank space")
else:
print("It is a number")

It is an alphabet

Total cost of videos


In [163]:

n=int(input("enter the no of new videos"))


m=int(input("enter the no of old videos"))
o=int(input("enter the days"))
c1=75*n*o
c2=50*m*o
TC=c1+c2
print("Total cost=",TC)

Total cost= 4050

Print numbers from 50 to 70

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 17/32
2/17/2020 2019111048

In [164]:

n=50
m=70
for i in range(n,m+1):
print(i)

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

print no form 75 to 15 with interval 5


In [14]:

n=75
m=15
for i in range(n,m-5,-5):
print(i)

75
70
65
60
55
50
45
40
35
30
25
20
15

Average of first n numbers


https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 18/32
2/17/2020 2019111048

In [18]:

n=int(input("enter the no"))


sum=0
for i in range(1,n+1):
sum=sum+i
avg=sum/n
print("Average=",avg)

Average= 2.5

To print the sum of all odd numbers 1-100


In [40]:

n=1
m=100
sum=0
for i in range(n,m+1):
if(i%2!=0):
sum=sum+i
print("Odd sum=",sum)

Odd sum= 2500

Square and cube in range(1,n+1)


In [27]:

n=int(input("enter the no"))


sum1=0
sum2=0
for i in range(1,n+1):
sum1=sum1+i**2
print("square=",sum1)
for j in range(1,n+1):
sum2=sum2+j**3
print("cube=",sum2)

square= 55
cube= 225

Armstrong or not

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 19/32
2/17/2020 2019111048

In [33]:

num=int(input("enter the no"))


sum=0
temp=num
while (temp>0):
digit=temp%10
sum+=digit**3
temp//=10
if (num==sum):
print(num,"is an armstrong number")
else:
print(num,"is not an armstrong number")

153 is an armstrong number

Palindrome or not
In [39]:

o=int(input("enter the no"))


m=int(input("enter the no"))
for n in range(o,m+1,1):
l=n
rev=0
while(n>0):
rem=n%10
rev=rev*10+rem
n=n//10
if(rev==l):
print(rev)

1
2
3
4
5
6
7
8
9
11
22
33
44
55
66
77
88
99
101

No of digits in a number
https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 20/32
2/17/2020 2019111048

In [41]:

num=int(input("enter the no"))


sum=0
temp=num
while temp>0:
digit=temp%10
sum=sum+1
temp//=10
print(sum,"digits are in the given no")

5 digits are in the given no

Sum the given series 1^2/1+2^2/2+...


In [46]:

n=int(input("enter the number"))


s=0.0
for i in range(1,n+1):
a=float(i**2)/i
s=s+a
print("The sum of the series is",s)

The sum of the series is 10.0

Sum the given series -x+x^2-x^3+...


In [51]:

x=int(input("enter the no"))


sum=0
for i in range(1,x+1):
sum=sum+(-x)**i
print(sum)

-21

Sum the series 1+(1+2)+(1+2+3)+...


In [18]:

n=int(input("enter the no"))


a=0
b=0
for i in range(1,n+1):
a=a+i
b=b+a
print(b)

10

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 21/32
2/17/2020 2019111048

Sum the given series 1-x+x^2/2!-x^3/3!


In [63]:

n=int(input("enter the no"))


m=int(input("enter the no"))
d=1
s=0
sum=0
for i in range(1,n+1):
s=(-m)**i
d=d*i
sum=sum+(s/d)
print("sum=",sum-1)

sum= -7.666666666666666

Sin cos tan

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 22/32
2/17/2020 2019111048

In [67]:

import math
for i in range(0,375,15):
x=(i*math.pi)/180
print(math.sin(x))
print(math.cos(x))
print(math.tan(x))

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 23/32
2/17/2020 2019111048

0.0
1.0
0.0
0.25881904510252074
0.9659258262890683
0.2679491924311227
0.49999999999999994
0.8660254037844387
0.5773502691896257
0.7071067811865475
0.7071067811865476
0.9999999999999999
0.8660254037844386
0.5000000000000001
1.7320508075688767
0.9659258262890683
0.25881904510252074
3.7320508075688776
1.0
6.123233995736766e-17
1.633123935319537e+16
0.9659258262890683
-0.25881904510252085
-3.7320508075688763
0.8660254037844387
-0.4999999999999998
-1.7320508075688783
0.7071067811865476
-0.7071067811865475
-1.0000000000000002
0.49999999999999994
-0.8660254037844387
-0.5773502691896257
0.258819045102521
-0.9659258262890682
-0.267949192431123
1.2246467991473532e-16
-1.0
-1.2246467991473532e-16
-0.25881904510252035
-0.9659258262890684
0.26794919243112225
-0.5000000000000001
-0.8660254037844386
0.577350269189626
-0.7071067811865475
-0.7071067811865477
0.9999999999999997
-0.8660254037844384
-0.5000000000000004
1.7320508075688754
-0.9659258262890683
-0.25881904510252063
3.732050807568879
-1.0
-1.8369701987210297e-16
5443746451065123.0
https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 24/32
2/17/2020 2019111048

-0.9659258262890682
0.25881904510252113
-3.7320508075688714
-0.8660254037844386
0.5000000000000001
-1.732050807568877
-0.7071067811865477
0.7071067811865474
-1.0000000000000004
-0.5000000000000004
0.8660254037844384
-0.5773502691896265
-0.2588190451025207
0.9659258262890683
-0.26794919243112264
-2.4492935982947064e-16
1.0
-2.4492935982947064e-16

EXCERCISE 3

Student details
In [68]:

student=['Manimaran.M','Mech','Anna university','Guindy','Chennai']
print(student)

['Manimaran.M', 'Mech', 'Anna university', 'Guindy', 'Chennai']

First middle and any particular index to end


In [2]:

A=['a','b','c','d','e','f','g','h','i','j']
print("First three letters=",A[0:4])
print("Three letters in middle=",A[4:7])
print("Index no 2 to end=",A[2:])

First three letters= ['a', 'b', 'c', 'd']


Three letters in middle= ['e', 'f', 'g']
Index no 2 to end= ['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Updating values in a list

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 25/32
2/17/2020 2019111048

In [3]:

numlist=[1,2,3,4,5,6,7,8,9,10]
print("List is:",numlist)
numlist[5]=100
print("List after updation is:",numlist)
numlist.append(200)
print("List after appending a value is:",numlist)
del numlist[3]
print("List after deleting a value is:",numlist)

List is: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


List after updation is: [1, 2, 3, 4, 5, 100, 7, 8, 9, 10]
List after appending a value is: [1, 2, 3, 4, 5, 100, 7, 8, 9, 10, 200]
List after deleting a value is: [1, 2, 3, 5, 100, 7, 8, 9, 10, 200]

Deletion of numbers using del statements


In [5]:

A=[1,2,3,4,5,6,7,8,9,10]
del A[2:4]
print(A)

[1, 2, 5, 6, 7, 8, 9, 10]

Program to illustrate nested list


In [11]:

list1=[1,'a',"abc",[2,3,4,5],8.9]
i=0
while i <(len(list1)):
print("list1[",i,"]=",list1[i])
i+=1

list1[ 0 ]= 1
list1[ 1 ]= a
list1[ 2 ]= abc
list1[ 3 ]= [2, 3, 4, 5]
list1[ 4 ]= 8.9

Insert a list in another list using slice operation

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 26/32
2/17/2020 2019111048

In [10]:

A=[1,9,11,13,15]
print("original list:",A)
A[2]=[3,4,7]
print("After inserting another list, the updated list is:",A)

original list: [1, 9, 11, 13, 15]


After inserting another list, the updated list is: [1, 9, [3, 4, 7], 13, 15]

To create a copy as well as the clone of the original list


In [13]:

A=[1,2,3,4,5]
B=A
print("A=",A)
print("B=",B)
C=A[2:6]
print("C=",C)

A= [1, 2, 3, 4, 5]
B= [1, 2, 3, 4, 5]
C= [3, 4, 5]

To delete items using empty list


In [15]:

A=['p','r','o','g','r','a','m']
A[2:5]=[]
print(A)

['p', 'r', 'a', 'm']

To find sum and mean of elements in a list


In [16]:

A=[1,2,3,4,5,6,7,8,9,10]
sum=0
for i in A:
sum+=i
print("sum=",sum)
avg=sum/float(len(A))
print("Avg=",avg)

sum= 55
Avg= 5.5

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 27/32
2/17/2020 2019111048

To find the maximum of a list of numbers

In [21]:

A=[4,1,8,2,9,3,0]
print("Largest value in the list is:",max(A))

Largest value in the list is: 9

To print the index values in a list


In [20]:

A=[1,2,3,4,5]
for i in range(len(A)):
print("Index=",i)

Index= 0
Index= 1
Index= 2
Index= 3
Index= 4

To remove duplicates from a list


In [24]:

A=[1,2,3,4,5,6,7,6,5,4]
print("Original list=",A)
i=0
while i<len(A):
num=A[i]
for j in range(i+1,len(A)):
val=A[j]
if val ==num:
A.pop(j)
i+=1
print("List after removing duplicates=",A)

Original list= [1, 2, 3, 4, 5, 6, 7, 6, 5, 4]


List after removing duplicates= [1, 2, 3, 4, 5, 6, 7]

Using reduce() function to calculate the sum

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 28/32
2/17/2020 2019111048

In [27]:

import functools
def add(x,y):
return x+y
A=[1,2,3,4,5,6,7,8,9,10]
print("Sum of values in list=",functools.reduce(add,A))

Sum of values in list= 55

Using filter() function to filter out even numbers


In [28]:

def check(x):
if(x%2==0):
return 1
A=list(filter(check,range(1,11)))
print("Even numbers=",A)

Even numbers= [2, 4, 6, 8, 10]

Map() function to print the double value of each element


In [29]:

def mul_2(x):
x*=2
return x
A=[1,2,3,4,4,5]
print("Original list=",A)
A1=list(map(mul_2,A))
print("Modified list=",A1)

Original list= [1, 2, 3, 4, 4, 5]


Modified list= [2, 4, 6, 8, 8, 10]

List to tuple and tuple to list

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 29/32
2/17/2020 2019111048

In [1]:

A=['x','y','z']
print("List A=",A)
B=tuple(A)
print("Tuple A=",B)
C=('x','y','z')
print("Tuple C=",C)
D=list(C)
print("List C=",D)

List A= ['x', 'y', 'z']


Tuple A= ('x', 'y', 'z')
Tuple C= ('x', 'y', 'z')
List C= ['x', 'y', 'z']

Student details with tuple


In [2]:

student=('Mani maran.M','Mech','Anna university','Guindy','Chennai')


print("Student=",student)

Student= ('Mani maran.M', 'Mech', 'Anna university', 'Guindy', 'Chennai')

Different ways of assigning tuple


In [3]:

(v1,v2,v3)=(1,2,3)
print(v1,v2,v3)
T1=(100,200,300)
(v1,v2,v3)=T1
print(v1,v2,v3)
(v1,v2,v3)=(2+4,5/3+4,9%6)
print(v1,v2,v3)
A=[1,2,3]
B=tuple(A)
print(B)

1 2 3
100 200 300
6 5.666666666666667 3
(1, 2, 3)

To print five friends details

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 30/32
2/17/2020 2019111048

In [4]:

Details=(('Manimaran.M','Mech'),('Prasanth.R','Mech'),('Nandha','Mech'),('Sabari','Mech'),
('Harisankar','Mech'))
for i in Details:
print(i)

('Manimaran.M', 'Mech')
('Prasanth.R', 'Mech')
('Nandha', 'Mech')
('Sabari', 'Mech')
('Harisankar', 'Mech')

To get the 4th element in the first and last


In [5]:

A=(1,2,3,4,5,6,7,8,9,10)
print("4th element=",A[3])
print("4th element form the last=",A[-4])

4th element= 4
4th element form the last= 7

To swap two values in tuple


In [6]:

v1=10
v2=20
print("v1=",v1,"v2=",v2)
(v1,v2)=(v2,v1)
print("v1=",v1,"v2=",v2)

v1= 10 v2= 20
v1= 20 v2= 10

In [9]:

a=input("enter the email address")


user_name,domain_name=a.split('@')
b=(user_name,domain_name)
print("b=",b)

b= ('mani', 'gmail.com')

List of both +ve,-ve and make a tuple with +ve values

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 31/32
2/17/2020 2019111048

In [10]:

A=[1,-2,-3,-4,5,6,7,8,10]
newtup=()
for i in A:
if(i>0):
newtup+=(i,)
print("New tuple=",newtup)

New tuple= (1, 5, 6, 7, 8, 10)

To find the length of the tuple


In [11]:

A=(1,2,3,4,5,6,7,8,9,10)
print("Length=",len(A))

Length= 10

Tuple with string formatting


In [12]:

Tup=("Mani",89,82,4)
print("%s got %d marks in CSA and his aggregate was %.2f"%(Tup[0],Tup[1],Tup[2]))

Mani got 89 marks in CSA and his aggregate was 82.00

To reverse a tuple
In [13]:

A=(1,2,3,4,5,6,7,8,9,10)
b=list(A)
b.reverse()
c=tuple(b)
print("The reversed tuple is=",c)

The reversed tuple is= (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

In [ ]:

https://jlab.annauniv.edu:8000/user/be2019111048/lab/workspaces/auto-8 32/32

You might also like