Python Tuples and Lists Quiz
10 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

How can you retrieve the first and second elements of a tuple named 'dimensions' correctly?

  • X_axis, Y_axis = dimensions[0:2]
  • X_axis, Y_axis = (dimensions[0], dimensions[1]) (correct)
  • X_axis, Y_axis = dimensions (correct)
  • X_axis, Y_axis = dimensions::1
  • What will happen if you try to append an item to a tuple?

  • The tuple will convert into a list.
  • An AttributeError will occur. (correct)
  • The item will be added successfully.
  • The tuple will become empty.
  • Why are tuples more efficient than lists in certain situations?

  • They allow dynamic sizing.
  • They have more built-in methods.
  • They use less memory and are simpler. (correct)
  • They enable faster sorting.
  • Which of the following methods cannot be used with tuples?

    <p>append()</p> Signup and view all the answers

    What code snippet will correctly loop through all values in a tuple called 'dimensions'?

    <p>for dimension in dimensions: print(dimension)</p> 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']?

    <p>['Bengali', 'Russian', 'Arabic', 'Portuguese']</p> Signup and view all the answers

    Which statement is true regarding the copying of the list copy_languages = languages?

    <p>Both variables will reference the same list in memory.</p> Signup and view all the answers

    Which of the following is a key characteristic that differentiates tuples from lists?

    <p>Lists are mutable, whereas tuples are immutable.</p> 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)?

    <p>A TypeError will be raised indicating that tuples do not support item assignment.</p> Signup and view all the answers

    What is the result of executing the loop for iter in y: with y = [1, 9, 2]?

    <p>It will print each element of <code>y</code> on a new line: 1 2 9.</p> 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 dictionaries D1 and D2 and returns a new dictionary with the common keys in D1 and D2.
    • 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 number n and returns n 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 name F 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 list L and an integer n. The list contains numbers from 1 to n 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 list L 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.

    Quiz Team

    Related Documents

    Revision Notes (PDF)

    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.

    More Like This

    Python Lists and Tuples Quiz
    5 questions
    Python Tuple and List Operations
    10 questions
    Python List, Tuple, and Set Operations
    24 questions
    Use Quizgecko on...
    Browser
    Browser