Kavisha ConceptQuestion
Kavisha ConceptQuestion
In [35]:
#Q1.How do you create a list containing the numbers from 1 to 5 without using a for loop
l2=[1,2,3,4,5]
l2
Out[35]: [1, 2, 3, 4, 5]
In [22]:
#Q2. Given a list numbers = [1, 2, 3, 4, 5], how can you create a new list containing th
l1=[1,2,3,4,5]
l2=l1[:3]
l2
Out[22]: [1, 2, 3]
In [23]:
#Q3. Create two lists list1 = [1, 2, 3] and list2 = [4, 5, 6]. How can you concatenate t
list1 = [1, 2, 3]
list2=[4,5,6]
list1+list2
Out[23]: [1, 2, 3, 4, 5, 6]
In [25]:
#Q4. Generate a list containing the squares of numbers from 1 to 10 using list comprehen
l=[x*x for x in range(1,11)]
l
In [29]:
#Q5. Given a list original_list = [5, 4, 3, 2, 1], how do you reverse the list in-place
l=[5,4,3,2,1]
l=l[::-1]
l
Out[29]: [1, 2, 3, 4, 5]
In [31]:
#Q6. Given a list fruits = ['apple', 'banana', 'cherry'], how can you check if the strin
fruits = ['apple', 'banana', 'cherry']
fruits.index("banana")
Out[31]: 1
In [33]:
#Q7. Starting with a list numbers = [1, 2, 3, 4, 5], how can you remove the element '3'
num=[1,2,3,4,5]
num.pop(2)
num
Out[33]: [1, 2, 4, 5]
In [34]:
#Q8. How do you create a tuple containing three different colors ("red", "green", "blue
colors = ("red", "green", "blue")
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 1/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
colors
In [36]:
#Q9. Given a tuple my_tuple = (10, 20, 30, 40, 50), how can you access the third element
tup=(10,20,30,40,50)
tup3=tup[2:3]
tup3
Out[36]: (30,)
In [37]:
#Q10. If you have a tuple coordinates = (5, 10), how can you unpack its values into two
cor=(5,10)
x=cor[0]
y=cor[1]
print("x:",x)
print("y:",y)
x: 5
y: 10
In [39]:
#Q11. Create two tuples tuple1 = (1, 2, 3) and tuple2 = (4, 5, 6). How can you concatena
tup1=(1,2,3)
tup2=(4,5,6)
l1=list(tup1)
l2=list(tup2)
l1.extend(l2)
tup1=tuple(l1)
tup1
Out[39]: (1, 2, 3, 4, 5, 6)
In [40]:
#Q12. Given a list numbers = [1, 2, 3, 4, 5], how can you convert it into a tuple withou
num=[1,2,3,4,5]
num=tuple(num)
num
Out[40]: (1, 2, 3, 4, 5)
In [43]:
#Q13. Given a tuple fruits = ("apple", "banana", "cherry", "apple"), how can you find th
fruits = ("apple", "banana", "cherry", "apple")
apple = fruits.index("apple")
apple
Out[43]: 0
In [41]:
#Q14. Pack the values 25, "John", and True into a tuple named person?
name=[25,"John",True]
name=tuple(name)
name
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 2/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
In [15]:
#Q15. Explain why tuples are immutable and how this differs from lists?
as in list we can change the existing value or it can be modified
but we can make any change in the tuple or modify its value
In [44]:
# Q16. Given two strings "Hello" and "World", how can you concatenate them to form the s
a="Hello"
b="World"
res=" ".join((a,b))
res
In [45]:
# Q17. Given two strings "Hello" and "World", how can you concatenate them to form the s
a="Hello"
b="World"
res=" ".join((a,b))
res
In [48]:
# Q18. Given a string "abcdefgh", how can you create a new string containing the charact
a="abcdefgh"
b=a[2:5]
b
Out[48]: 'cde'
In [49]:
# Q19. Given a string "reverse", how can you create a new string that is the reverse of
a="reverse"
b=a[::-1]
b
Out[49]: 'esrever'
In [50]:
# Q20. Determine the length of the string "length" without using the len() function or a
s="length"
c=0
for i in s:
c+=1
c
Out[50]: 6
In [54]:
# Q21. Check if the substring "world" is present in the string "Hello, world!" without
a='Hello World'
present="world" in a.lower()
present
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 3/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
Out[54]: True
In [57]:
# Q22. Convert the integer 42 into a string without using the str() function or a loop?
num=42
result="{}".format(num)
result
Out[57]: '42'
In [131…
# Q23. Convert the string "Hello World" to uppercase and then to lowercase without using
string="Hello World"
lower=string.casefold()
upper=lower.swapcase()
print("Lower:",lower)
print("Upper:",upper)
In [143…
# Q24. Given the string "apple,banana,orange", how can you split it into a list of indi
string="apple,banana,orange"
l=[]
s=""
j=""
for i in string:
if i!=",":
s+=i
else:
l.append(s)
s=""
if len(s)>0:
l.append(s)
l
In [99]:
# Q25. Create a string by joining the elements of the list words = ["I", "love", "Python
word = ["I", "love", "Python"]
res=word[0]+" "+" "+word[1]+" "+word[2]
res
In [102…
# Q26. Replace all occurrences of the word "apple" with "pear" in the string "apple,app
string="apple,apple,apple"
par= string.split("apple")
result = "pear".join(par)
print(result)
pear,pear,pear
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 4/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
In [103…
# Q27. Count the number of occurrences of the letter "o" in the string "Hello, world!" w
string= "Hello, world!"
c=0
for i in string:
if(i=='o'):
c+=1
print(c)
In [104…
# Q28. Count the number of occurrences of the letter "o" in the string "Hello, world!" w
string= "Hello, world!"
c=0
for i in string:
if(i=='o'):
c+=1
print(c)
In [117…
# Q29. Write a program that takes three numbers as input and prints the maximum of those
a=int(input("enter a number"))
b=int(input("enter a number"))
c=int(input("enter a number"))
largest = 45
In [110…
# Q30. Create a program that takes a student's score as input and prints their correspon
# A: 90-100
# B: 80-89
# C: 70-79
# D: 60-69
# F: Below 60
score=int(input("enter number"))
print("Score:",score)
if 90 <= score <= 100:
print('A')
elif 80 <= score < 90:
print( 'B')
elif 70 <= score < 80:
print( 'C')
elif 60 <= score < 70:
print( 'D')
elif score < 60:
print ('F')
else:
print( 'Invalid score')
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 5/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
Score: 56
F
In [127…
# Q31. Create a program that checks the strength of a password based on certain criteria
passw=input("enter a password")
a=0
b=0
c=0
d=0
if (len(passw)>=8):
for i in passw:
if(i.isupper()):
a=1
elif(i.islower()):
b=1
elif(i.isdigit()):
c=1
elif(i in "!@#$%^&*(),.?\":{}|<>"):
d=1
e=a and b and c and d
print(passw)
if e ==1:
print("Strong")
else:
print("Weak")
"Adwbdh123@
Strong
In [113…
# Q32. Given a dictionary person = {"name": "John", "age": 30, "city": "New York"}, how
person = {"name": "John", "age": 30, "city": "New York"}
age = person.get("age")
print(age)
30
In [115…
# Q33. Check if the key "country" exists in the dictionary details = {"name": "Alice",
details = {"name": "Alice", "age": 25}
key = details.get("country")
print(key)
None
In [116…
# Q34. Convert a dictionary my_dict = {"a": 1, "b": 2, "c": 3} into a list of tuples con
my_dict = {"a": 1, "b": 2, "c": 3}
new= list(my_dict.items())
print(new)
In [95]:
# Q35. Write a program that takes two numbers as input and prints whether the first numb
a=int(input("number"))
b=int(input("number"))
if a>=b:
print("greater",a)
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 6/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
else:
print("greater",b)
greater 34
In [94]:
#Q36. Evaluate the expression 3 + 5 * 2 and then evaluate the expression (3 + 5) * 2, no
a=3 + 5 * 2 #here ans is 13 as 5*2 is evaluate first due to BODMAS
b=(3+5)*2 #here ans is 16 as (3+5) is evaluate first
c=b-a
c
Out[94]: 3
In [92]:
# Q37. Given the nested list matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]], how ca
matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
ma=matrix[1][1:2]
ma
Out[92]: [50]
In [88]:
# Q38. Modify the nested list grades = [["Alice", 85], ["Bob", 92], ["Charlie", 78]] to
In [87]:
# Q39. Given a nested list data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], how can you create
data=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new=data[0:2]
new
In [86]:
# Q40. Add a new nested list ['David', 28] to the list people = [['Alice', 25], ['Bob',
people = [['Alice', 25], ['Bob', 30]]
people.insert(2,['David', 28])
people
In [85]:
# Q41. Check if the nested list ['Alice', 25] is present in the list people = [['Alice',
people = [['Alice', 25], ['Bob', 30]]
people.index(['Alice', 25])
Out[85]: 0
In [118…
# Q42. Create a copy of the nested tuple original = ((1, 2), (3, 4)) without modifying t
original = ((1, 2), (3, 4))
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 7/8
10/3/24, 12:39 PM Kavisha_ConceptQuestion
copy=original
copy
In [83]:
# Q43.Given a string word = "abcdefg", how can you create a new string that contains eve
word="abcdefg"
w=word[::2]
w
Out[83]: 'aceg'
In [119…
# Q44. Prove how the string is immutable?
str="abcdefgh"
str[0]=q
#name error proves string is immutable
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-119-ab35d9095d43> in <module>
1 # Q44. Prove how the string is immutable?
2 str="abcdefgh"
----> 3 str[0]=q
In [82]:
#Q45. Using negative indexing, how can you access the second-to-last character of the st
message = "Hello, world!"
message[-2:]
Out[82]: 'd!'
In [ ]:
file:///E:/c++/dsa/Bootcamp/Kavisha_ConceptQuestion.html 8/8