1 String (str)
A string is an immutable sequence of characters.
Properties of Strings
Immutable (cannot be modified after creation)
Ordered (maintains sequence)
Supports indexing and slicing
Can contain letters, numbers, symbols
Example of String
text = "Hello, Python!"
print(text[0]) # H (Indexing)
2 List (list)
A list is a mutable, ordered collection of elements.
Properties of Lists
Mutable (can be modified after creation)
Ordered (maintains sequence)
Supports indexing and slicing
Can contain different data types (int, float, string, etc.)
Example of List
my_list = [10, "apple", 3.14, True]
print(my_list[1]) # "apple"
Tuple (tuple)
A tuple is an immutable, ordered collection of elements.
Properties of Tuples
Immutable (cannot be modified after creation)
Ordered (maintains sequence)
Supports indexing and slicing
Can contain different data types
Faster than lists (better performance)
Example of Tuple
my_tuple = (10, "apple", 3.14, True)
print(my_tuple[1]) # "apple"
String Programs:
#string fundamental
mylist=["Today","is","Monday"]
x=" ".join(mylist)
print(x)
str="AN apple a day,keeps octor away"
l=len(str)
print("LEngth=",l)
c1=str.count('apple')
print(c1)
c2=str.count('apple')
print(c2)
x2=str.count('a',5)
#string comarision
print("Hello"=="HEllo")
print("Hello"<"HEllo")
print("Hello">"HEllo")
print("Hello"!="HEllo")
#finding index
str2="Hello Friends!!!"
print(str2.find('e'))
print(str2.find('g'))
#partition
str3="covid19 is the deadliest virus"
s4=str3.partition("deadliest")
print(s4)
s5=str3.partition("Hapiest")
print(s5)
Join function
list1=['1','hi']
str1='#'
str2=str1.join(list1)
print(str2)
str3=str1.join('Hello Friends')
print(str3)
Palindrome
str1=input("enter string:ma")
str2=str1[::-1]
if str1==str2:
print("Palindrome")
else:
print("not palindrome")
Symmetrical
str1=input("enter your string:")
l=int(len(str1)/2)
str2=str1[:l]
str3=str1[l:]
print(str2,str3)
if str2==str3:
print("Symmetrical")
else:
print("non symmetrical")
Even Length
#WAP to display all the words in a string whose length is even
str1=input("Input a sentence:")
l=str1.split(" ")
for i in l:
if len(i)%2==0:
print(i)
String Check
#wap to check if the string contains atleast 1 aplphabet and 1 digit
str1=input("Input a string to be checked:")
alpha=any(i.isalpha() for i in str1)
digit=any(i.isdigit() for i in str1)
if alpha and digit:
print("alphabet and digit is present")
elif alpha:
print("alphabet present but no digit present")
elif digit:
print("digit present but no alphabet present")
else:
print("both not present")
List Programs
1)Write a python program to print the sum of elements of a list
str= [ 1,2, 5 , 7 , 8 ]
t=0
for i in str:
t=t+i
print("Sum of list: ",t)
2)Write a python program to print the multiplication of elements of a list
str= [ 1,2, 5 , 7 , 8 ]
t=1
for i in str:
t=t*i
print("Multiplication of list: ",t)
3)Write a python program to reverse a list at a specific location
l1=[17,23,57,72,91,44,55]
x= int(input("First index: "))
y= int(input("Last index: "))
z= l1[x:y]
z.reverse()
l1[x:y]=z[:]
print(l1)
4)Write a python program to remove the empty string from a list of strings
str= ["hello","","hiii"]
for i in str:
if i=="":
str.remove(i)
print("Result: ",str)
5)Write a python program to find the first non repeated number in a list
l1=[1,2,1,2,3,5,7]
for i in l1:
a=l1.index(i)
if i not in l1[a+1:]:
print(i)
break;
6)Write a python program to find all the pairs from a list whose sum is equal to
given input
l1=[1,7,2,4,3,6,9]
pairs=[]
a=int(input("List all the pairs of the l1 whose sum = "))
for i in l1:
x=l1.index(i)
for j in l1[x+1:]:
if i+j==10:
pairs.append((i,j))
print(pairs)
--List comprehension
sentence='Unity Has strenghth'
chars=[i for i in sentence]
print("All charachters=",chars)
vowels=[i for i in sentence if i in 'aeiou']
print("\nvowels=",vowels)
orig_prices=[1.25,-9.45,10.22,3.78,-5.92,1.16]
new=[i if i>0 else 0 for i in orig_prices]
print(new)
l=[-2,1,2,3,4,5,6,7,8]
s1=[i*i for i in l]
print(s1)
list=['keras','tensorflow','kubernetes']
k=[i for i in list if i[0]=='k']
print(k)
list=['keras','tensorflow','kubernetes']
k=[i for i in list if i[0]=='k']
print(k)
l=['abc','bef','abcd','fegs']
new=[i for i in l if len(i)>3]
print(new)
Tuple programs are remaining will send that
tomorrow!!!!!!!!!!!!