0% found this document useful (0 votes)
7 views5 pages

Python List and String Operations Guide

The document contains various Python code snippets demonstrating basic programming concepts such as list manipulation, string operations, tuple handling, and dictionary usage. Key functionalities include swapping elements, finding maxima and minima, checking for palindromes, and calculating the length of strings and lists. Additionally, it covers binary to decimal conversions, generating Pascal's triangle, and summing dictionary values.

Uploaded by

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

Python List and String Operations Guide

The document contains various Python code snippets demonstrating basic programming concepts such as list manipulation, string operations, tuple handling, and dictionary usage. Key functionalities include swapping elements, finding maxima and minima, checking for palindromes, and calculating the length of strings and lists. Additionally, it covers binary to decimal conversions, generating Pascal's triangle, and summing dictionary values.

Uploaded by

NOSSAM BHARGAVI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

'''print("hello world")

----------------------------------------------------------------------#swapping the
first and last elements in a list
a=[1,2,3,4,5]
for i in a:
print(i)
t=a[0]
a[0]=a[len(a)-1]
a[len(a)-1]=t
for i in a:
print(i)'''
----------------------------------------------------------------------------------
#swapping the values in a list
'''a=[1,2,3,4,5,6]
for i in a:
print(i)
p1=int(input("enter the postion1:"))
p2=int(input("enter the postion2:"))
t=a[p1]
a[p1]=a[p2]
a[p2]=t
for i in a:
print(i)'''
--------------------------------------------------------------------------
#finding the length of the list
'''a=[1,2,3,45,6,4523,3]
l=0
for i in a:
l+=1

print("length of a is",l,len(a))'''
-----------------------------------------------------------------------------------
--#finding the maxima and minima of the two numbers
'''a=int(input("value of a is "))
b=int(input("value of b is "))
if a>b:
print("maximum is a")
else:
print("maximun is b")'''

'''a=int(input("value of a is "))
b=int(input("value of b is "))
if a>b:
print("minimum is b")
else:
print("minimun is a")'''

-----------------------------------------------------------------------------------
----------#checking the palindrome condition

'''a=input("enter the string:")


g=0
r=len(a)-1
for i in range(0,len(a)):
if a[i]==a[r]:
g=0
r-=1
else:
g=1
break

if g==1:
print("it's not a palindrome")
else:
print("its a palindrome")'''

a=input("enter:")
k=0
for i in range(1,len(a)):
if (a[i]=="" and a[i-1]!=""):
k+=1

print("no of words in the string are :",k)


-----------------------------------------------------------------------------------
-#removing the ith position of the string
'''a="it is an easy task"
b=int(input("enter the position:"))
c=""
for i in range(0,b):
c+=a[i]
for i in range(b+1,len(a)):
c+=a[i]

a=c
print(a)'''
-----------------------------------------------------------------------------------
--------#printing the length of string
'''a="what is the length"
print(len(a))

n=0
for i in a:
n+=1

print(n)'''
-----------------------------------------------------------------------------------
-----------------
n="This is a python language" #printing the even
length words in a string ******
#splitting the words in a given string
s=[Link](" ")
print(s)
for i in s:
#checking the length of words
if len(i)%2==0:
print(i)

output:
This
is
python
language
--------------------------------------------------------------------------
n="This is a python language" #reversing the
elements in a string *****
s=[Link](" ")
print(s)
[Link]()
print(s)
q=""
for i in s:
q+=i
q+=" "
print(q)

output:
['This', 'is', 'a', 'python', 'language']
['language', 'python', 'a', 'is', 'This']
language python a is This

------------------------------------------------------------------------
a=[1,2,3]
k=[]
cube=1 #printing the list of
tuples with the number and its cube
for i in a:
cube=i*i*i
b=(i,cube)
[Link](b)

print(k)
output:
[(1, 1), (2, 8), (3, 27)]

-----------------------------------------------------------------

'''a=(1,2,3,4,5) #finding the maxima


in a tuple
max=a[0]

for k in a:
if max<k:
max=k

print("the greatest number is ",max)'''


----------------------------------------------------------------------#finding the
minima in a tuple
'''a=(1,2,3,4,5)
min=a[0]

for k in a:
if min>k:
min=k

print("the smallest number is ",min)'''


---------------------------------------------------------------------#finding the
sum of elements in a tuple
'''a=(1,2,3,4,5)
sum=0

for k in a:
sum+=k

print("sum of the elements is ",sum)'''


------------------------------------------------------------------#binary to
decimal and vice versa
'''a=int(input("enter the number:"))
b=0
r=None
i=0
while a>0:
r=a%2
b+=r*(10**i)
a=int(a/2)
i+=1

print("the binary value of a is",b)'''

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


b=0
r=None
i=0
while a>0:
r=a%10
b+=r*(2**i)
a=int(a/10)
i+=1

print("the integer value of a is",b)


------------------------------------------------------------------------------
#pascal triangle

import math
n = 5
for i in range(n):
for j in range(n-i+1):
print(end=" ")
for j in range(i+1):
# nCr = n!/((n-r)!*r!)
print([Link](i)//([Link](j)*[Link](i-j)), end=" ")
# for new line
print()

output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
-----------------------------------------------------------------------------

'''d={1:"one",2:"two"}
if 3 in d:
print("found")
else:
print("not found")'''

'''d={
"a":100,
"b":200,
"c":300
}
sum=0
k=list([Link]())
for i in k:
sum+=i
print("sum is:",sum)'''
dic3 = {1: "Lion", 2: "Tiger", 3: "Fox", 4: "Wolf"}
# print the sizes of sample Dictionaries
print("Size of dic3: " + str(dic3.__sizeof__()) + "bytes")
print("size of dic3: "+str(getsizeof(dic3))+"bytes")
-------------------------------------------------------

You might also like