This introduction to Python will kickstart your learning of Python for data science, as well as programming in general. This beginner-friendly Python course will take you from zero to programming in Python in a matter of hours. Upon its completion, you’ll be able to write your own Python scripts and perform basic hands-on data analysis using our Jupyter-based lab environment. If you want to learn Python from scratch, this free course is for you.

## Cognitive Class – Python for Data Science Answers

### Module 1: Python Basics

1. What is the result of the following operation in Python:

1. 3 + 2 * 2
2. 10
3. 7
4. 9
5. 12

2. In Python, if you executed name = ‘Lizz’, what would be the output of print(name[0:2])?

1. Lizz
2. L
3. Liz
4. Li

3. In Python, if you executed var = ‘01234567’, what would be the result of print(var[::2])?

1. 0246
2. 1357
3. 1234567
4. 8903

4. In Python, what is the result of the following operation ‘1’+’2′

1. ‘2’
2. ‘3’
3. ’12’
4. 3

5. Given myvar = ‘hello’, how would you convert myvar into uppercase?

1. len(myvar)
2. myvar.find(‘hello’)
3. myvar.upper()
4. myvar.sum()

### Module 2: Python Data Structures

1. What is the syntax to obtain the first element of the tuple:

1. A=(‘a’,’b’,’c’)
2. A
3. A correct
4. A[:]

2. After applying the following method,L.append([‘a’,’b’]), the following list will only be one element longer.

1. True
2. False

3. How many duplicate elements can you have in a set?

1. 1
2. 0, you can only have one unique element in a set
3. 100
4. depends on the number of elements in your set.

4. >>>>Consider the following Python Dictionary: Dict={“A”:1,”B”:”2″,”C”:[3,3,3],”D”:(4,4,4),’E’:5,’F’:6}, what is the result of the following operation: Dict[“D”]

1. 4
2. 3
3. [3,3,3]
4. (4, 4, 4)
5. error

5. What is an important difference between lists and tuples?

1. Lists can’t contain a string
2. Tuples can only have integers
3. Lists and tuples are the same.
4. Lists are mutable tuples are not
5. There is no zeros in lists

### Module 3: Python Programming Fundamentals

1. What is the output of the following lines of code:

x=1
if(x!=1):
print(‘Hello’)
else:
print(‘Hi’)
print(‘Mike’)

1. Hi Mike
2. Mike
3. Hello Mike
4. The Mike

2. What is the output of the following few lines of code ?

A=[‘1′,’2′,’3’]
for a in A:
print(2*a)

1. 2 4 6
2. ‘2’ ‘4’ ‘6’
3. ’11’ ’22’ ’33’
4. A B C

3. Consider the function Delta, when will the function return a value of 1

def Delta(x):
if x==0:
y=1;
else:
y=0;
return(y)

1. When the input is anything but 0
2. When the input in 1
3. Never
4. When the input is 0

4. What is the correct way to sort the list ‘B’ using a method, the result should not return a new list, just change the list ‘B’.

1. B.sort()
2. sort(B)
3. sorted(B)
4. B.sorted()

5. What are the keys of the of the following {‘a’:1,’b’:2}

1. 1,2
2. ;,:
3. a,b

### Module 4: Working with Data in Python

1. What do the following lines of code do? with open(“Example1.txt”,”r”) as file1:

print(FileContent)

1. Read the file “Example1.txt” correct
2. Write to the file “Example1.txt”
3. Append the file “Example1.txt”

2. What do the following lines of code do? with open(“Example2.txt”,”w”) as writefile:

writefile.write(“This is line A\n”)
writefile.write(“This is line B\n”)

2. Write to the file “Example2.txt”
3. Append the file “Example2.txt”

3. What do the following lines of code do? with open(“Example3.txt”,”a”) as file1:

file1.write(“This is line C\n”)

2. Write to the file “Example3.txt”
3. Append the file “Example3.txt”

4. What is the result of applying the following method df.head(), to the dataframe df

1. prints the first row of the dataframe
2. prints the first column of the dataframe
3. prints the first 5 rows of the dataframe
4. prints the dateframe out

### Module 5: Working with Numpy Arrays

1. What is the result of the following lines of code:

a=np.array([0,1,0,1,0])
b=np.array([1,0,1,0,1])
a*b

1. 0
2. array([1, 1, 1, 1, 1])
3. array([0, 0, 0, 0, 0])

2. What is the result of the following lines of code:

a=np.array([0,1])
b=np.array([1,0])
np.dot(a,b)

1. 1
2. array([1,1])
3. 0
4. array([0,0])

3. What is the result of the following lines of code:

a=np.array([1,1,1,1,1])
a+10

1. array([10,10,10,10,10])
2. array([11, 11, 11, 11, 11])
3. array([1,1,1,1,1])

4. What is the correct code to perform matrix multiplication on the matrix A and B

1. np.dot(A,B)
2. A*B
3. AxB

### Python for Data Science Final Exam Answers

1. What is the result of the following operation 3+2*2?

1. 3
2. 12
3. 9
4. 7

2. What is the type of the following variable: a=True?

1. int
2. bool
3. str
4. list

3. What is the result of the following operation int(3.2)?

1. 3.2
2. 3
3. 4
4. ‘3.2’

4. Consider the string A=’1234567′, what is the result of the following operation: A[1::2]

1. ‘1234567’
2. ‘246’
3. ‘1357’
4. error

5. Consider the string Name=”Michael Jackson” , what is the result of the following operation Name.find(‘el’)

1. 5
2. 4
3. 5,6
4. -1

6. The variables A=’1′ and B=’2′ ,what is the result of the operation A+B? you can’t add two strings

1. 3
2. ‘3’
3. ’12’

7. Consider the variable F=”You are wrong”, Convert the values in the variable F to uppercase?

1. F.up()
2. F.upper
3. F.upper()

8. Consider the tuple tuple1=(“A”,”B”,”C” ), what is the result of the following operation tuple1[-1]?

1. “A”
2. “B”
3. “C”

9. Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A:

1. ((11,12),[21,22])
2. (11,12)
3. (21,22)
4. [21,22]

10. Consider the tuple A=((11,12),[21,22]), that contains a tuple and list. What is the result of the following operation A:

1. 12
2. 11
3. 22
4. 21

11. What is the result of the following operation ‘1,2,3,4’.split(‘,’)

1. ‘1’,’2′,’3′,’4′
2. (‘1′,’2′,’3′,’4’)
3. [‘1′,’2′,’3′,’4’]
4. ‘1234’

12. Concatenate the following lists A=[1,’a’] and B=[2,1,’d’]:

1. A+B
2. A-B
3. A*B
4. A/B

13. How do you cast the list ‘A’ to the set ‘a’?

1. a.set()
2. a=A.append()
3. a=A.dict()
4. a=set(A)

14. Consider the Set: V={‘A’,’B’}, what is the result of V.add(‘C’)?

• {‘A’,’B’}
• {‘A’,’B’,’C’}
• {‘AC’,’BC’}
• error

15. Consider the Set: V={‘A’,’B’,’C’ }, what is the result of V.add(‘C’)?

1. {‘A’,’B’}
2. {‘A’,’B’,’C’}
3. {‘A’,’B’,’C’,’C’}
4. error

16. What is the output of the following lines of code:

x=”Go”
if(x!=”Go”):
print(‘Stop’)
else:
print(‘Go ‘)
print(‘Mike’)

1. Go Mike
2. Mike
3. Stop Mike
4. The Mike

17. What is the output of the following lines of code:

x=”Go”
if(x==”Go”):
print(‘Go ‘)
else:
print(‘Stop’)
print(‘Mike’)

1. Go Mike
2. Mike
3. Stop Mike
4. The Mike

18. How many iterations are performed in the following loop?

for n in range(3):
print(n)

1. 1
2. 2
3. 3
4. 4

19. What does the following loop print?

for n in range(3):
print(n+1)

1. 0 1 2
2. 1 2 3
3. 3 2 1
4. 2 1 0

20. What is the output of the following few lines of code ?

A=[‘1′,’2′,’3’]
for a in A:
print(2*a)

1. 2 4 6
2. ‘2’ ‘4’ ‘6’
3. ’11’ ’22’ ’33’
4. A B C

21. Consider the function add, what is the result of calling the following Add(‘1′,’1’) (look closely at the return statement )

z=y+x
return(y)

1. error
2. ‘2’
3. ’11’
4. ‘1’

22. Consider the class Points, what are the data attributes:

class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’y=’,self.y)

1. __init__
2. self.x self.y
3. print_point

23. What is the result of running the following lines of code ?

class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
p1=Points(1,2)
p1.print_point()

1. x=1
2. y=2
3. x=1 y=2

24. What is the result of running the following lines of code ?

class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
def print_point(self):
print(‘x=’,self.x,’ y=’,self.y)
p2=Points(1,2)
p2.x=2
p2.print_point()

1. x=1
2. y=2
3. x=1 y=2
4. x=2 y=2

25. Consider the following line of code: with open(example1,”r”) as file1:

What mode is the file object in?