Podcast
Questions and Answers
How can you retrieve the first and second elements of a tuple named 'dimensions' correctly?
How can you retrieve the first and second elements of a tuple named 'dimensions' correctly?
What will happen if you try to append an item to a tuple?
What will happen if you try to append an item to a tuple?
Why are tuples more efficient than lists in certain situations?
Why are tuples more efficient than lists in certain situations?
Which of the following methods cannot be used with tuples?
Which of the following methods cannot be used with tuples?
Signup and view all the answers
What code snippet will correctly loop through all values in a tuple called 'dimensions'?
What code snippet will correctly loop through all values in a tuple called 'dimensions'?
Signup and view all the answers
What will the output of the command print(languages[4:])
be if languages
is defined as ['English', 'Hindi', 'Chinese', 'Spanish', 'Bengali', 'Russian', 'Arabic', 'Portuguese']
?
What will the output of the command print(languages[4:])
be if languages
is defined as ['English', 'Hindi', 'Chinese', 'Spanish', 'Bengali', 'Russian', 'Arabic', 'Portuguese']
?
Signup and view all the answers
Which statement is true regarding the copying of the list copy_languages = languages
?
Which statement is true regarding the copying of the list copy_languages = languages
?
Signup and view all the answers
Which of the following is a key characteristic that differentiates tuples from lists?
Which of the following is a key characteristic that differentiates tuples from lists?
Signup and view all the answers
What will happen if you attempt to reassign an element of a tuple, such as z[1] = 4
where z = (5, 4, 3)
?
What will happen if you attempt to reassign an element of a tuple, such as z[1] = 4
where z = (5, 4, 3)
?
Signup and view all the answers
What is the result of executing the loop for iter in y:
with y = [1, 9, 2]
?
What is the result of executing the loop for iter in y:
with y = [1, 9, 2]
?
Signup and view all the answers
Study Notes
Introduction to Python
- Course revision
- Questions and answers provided
Q1
- Write a function
Intersec(D1, D2)
that takes two dictionariesD1
andD2
and returns a new dictionary with the common keys inD1
andD2
. - Example:
D1 = {1:'a', 2:'b', 3:'c'}
,D2 = {2:'x', 4:'y', 1:'z'}
Intersec(D1, D2) => {1: ['a', 'z'], 2: ['b', 'x']}
Q1 Answer
-
def Intersec(D1, D2):
-
res = {}
-
for i in D1:
-
if i in D2:
-
res[i] = [D1[i], D2[i]]
-
return res
-
D1 = {1:'a', 2:'b', 3:'c'}
-
D2 = {2:'x', 4:'y', 1:'z'}
-
print(Intersec(D1, D2))
Q2
- Write a function
Round(n)
that takes a float numbern
and returnsn
rounded to the nearest integer. - Note: you are not allowed to use the round built-in function.
- Example:
Round (3.5) => 4
,Round (2.2) => 2
Q2 Answer
-
def Round(n):
-
if n - int(n) >= 0.5:
-
return int(n) + 1
-
return int(n)
-
print (Round(3.5))
Q3
- Consider a text file where each line contains 3 integers separated by commas. For each line, the 1st integer is at position 0, the 2nd integer is at position 1, and the 3rd integer is at position 2. A column of data is defined as all integers in the file with the same position.
- Write a function
ColAvg(F)
that takes a file nameF
of the above format and returns a list of the averages of each column. - Example:
numbers.txt
-
3,2,6
-
2,3,3
-
1,10,15
-
ColAvg("numbers.txt") => [2, 5, 8]
-
Q3 Answer
-
def ColAvg(F):
-
fin = open(F,'r')
-
res = [0,0,0]
-
lines = fin.readlines()
-
i = 0
-
for line in lines:
-
for num in line.split(','):
-
res[i] += int(num)
-
i += 1
-
i = 0
-
-
for i in range(len(res)):
-
res[i] = int(res[i]/len(lines))
-
-
return res
-
print (ColAvg("numbers.txt"))
Q4
- Write a function
missing(L, n)
that takes a listL
and an integern
. The list contains numbers from 1 ton
except a missing number. Your function should return this missing number. - Example:
missing([1,2,4,5], 5) => [3]
,missing([1,2,3,4,5], 6) => [6]
,missing([1,2,4],6) =>[3,5,6]
Q4 Answer
-
def missing(L, n):
-
set1=set(L)
-
L2=list(range(1,n+1))
-
set2=set(L2)
-
diff=list(set2-set1)
-
return diff
-
print(missing([1,2,4,5], 5))
Q5
- Write a function
CountInList(L)
that takes a listL
to count the number of zeros, even and odd numbers from a list of numbers. - Example:
CountInList([0,33,25,64,58,0,66,94,23,47,45,0,29,28])
- Output:
- Number of Zeros: 3
- Number of evens: 5
- Number of odds: 6
Q5 Answer
-
def CountInList(L):
-
count_zeros=0
-
count_odd=0
-
count_even=0
-
for i in L:
-
if i==0:
-
count_zeros+=1
-
-
elif i%2==0:
-
count_even+=1
-
-
else:
-
count_odd+=1
-
-
-
return count_zeros,count_even,count_odd
-
zeros,evens,odds=CountInList([0,33,25,64,58,0,66,94,23,47,45,0,29,28])
-
print("Number of Zeros: ", zeros)
-
print("Number of evens: ", evens)
-
print("Number of odds: ", odds)
Other Output Questions
- Provided code output as requested.
File Processing
- Introduction
- Opening a file
- Reading from a file
- Writing to files
- Closing a file
- Reading from the web
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge about Python tuples and lists with this quiz. Questions cover tuple manipulation, list slicing, and the differences between these two data structures. Perfect for beginners looking to deepen their understanding of Python's data types.