exam Flashcards

(7 cards)

1
Q

write a FOR loop that prints the intergers from 1 to 10 (inclusive)

A

for i in range (1,11):
print (i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

code in python the fct f(x) = 3x^2 + x - 1

A

def f(x):
return 3*x**2+x-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

write a fct that takes a STRING as an argument and RETURN its length

A

def length_str(string):
length = len(string)
return (length)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using list comprehension, create a list that contauns squares of numbers from 1 to 10.

A

for i in range (1,11):
squares = [i**2]
print (squares)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

write a python fct g that thats an interger n and prints “even” if n is even and “‘odd” otherwise

A

def g(n):
if n % 2 == 0:
print (“Even”)
else:
print (“Odd”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

write fct that takes a list and an element as arguments and returns the number of occurences that element is in the list

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

write fct that takes a list and returns a new list contaning only unique elements (remove dups)

A

def rem_dupes(lst)
return list(set(lst))

fct set creates the unique elemtns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly