In: Computer Science
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you.
1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function.
A = [-4, 1, -16, 36, -49, 64, -128]
2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1 to 50.
3)Consider a string called S. Convert S into a list of words using split method. Then use a for loop to iterate over the list of words, only if letter c is in a word, print it. For example:
if ‘x’ in word: print(word).
S = “This class has three credits”
4) Create a for loop that iterates over list C and only prints an
element if it’s a number (int or float).
C = [13, ‘not me’, -4, ‘skip this one’, 6.1, 0.9, ‘NO’]
1)
#list
A = [-1,1,-16,36,-49,64,-128]
# runs the loop on a list
for i in range(len(A)):
# pow(base,power) is a function which calculates the power of given
number
# square number is stored in temp varaible
temp = pow(A[i],2)
#prints the squre of a number
print(temp)
2)
#list
A = [-1,1,-16,36,-49,64,-128]
counter = 0
# runs the loop on a list
for i in range(len(A)):
#checks the condition for the number is in range 1 to 50
# and is it divisible by 3 or not when all three conditions are
meet then only if block is executed
if(A[i]>1 and A[i]<50 and A[i]%3==0):
#counter increment
counter+=1
#prints the
print(counter)
3)
# string
S = "This class has three credits"
# converting string to list by .split()function
lst = S.split()
# runs the loop on list
for i in range(len(lst)):
# checks the condition character 'c' present in word or not
if 'c' in lst[i]:
#print the word
print(lst[i])
4)
C = [13,'not me',-4,'skip this one',6.1,0.9,'NO']
# runs the loop
for i in range(len(C)):
# checks the condition
if (type(C[i]) == int or type(C[i]) == float):
# print the element
print(C[i])
TRY TO TYPE THE CODE FROM GIVEN IMAGES
BEAWARE OF INDENTATION
THUMBS UP