exam Flashcards
(7 cards)
write a FOR loop that prints the intergers from 1 to 10 (inclusive)
for i in range (1,11):
print (i)
code in python the fct f(x) = 3x^2 + x - 1
def f(x):
return 3*x**2+x-1
write a fct that takes a STRING as an argument and RETURN its length
def length_str(string):
length = len(string)
return (length)
Using list comprehension, create a list that contauns squares of numbers from 1 to 10.
for i in range (1,11):
squares = [i**2]
print (squares)
write a python fct g that thats an interger n and prints “even” if n is even and “‘odd” otherwise
def g(n):
if n % 2 == 0:
print (“Even”)
else:
print (“Odd”)
write fct that takes a list and an element as arguments and returns the number of occurences that element is in the list
def counter (lst, element):
count = 0
for i in lst:
if i == element:
count = count + 1
print f”(There are {count} occurences of {element} in the list {lst}”
return count
write fct that takes a list and returns a new list contaning only unique elements (remove dups)
def rem_dupes(lst)
return list(set(lst))
fct set creates the unique elemtns.