1. Home
  2. Explore
  3. Computer Science
  4. Python for Data Science

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python for Data Science

  • 48Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Problem Solving & Algorithms

What is the primary focus of the course described in the transcript?

48 questions

  1. 1

    What is the primary focus of the course described in the transcript?

    1. A

      Mastering Python for system administration

    2. B

      Mastering Python for web development

    3. C

      Mastering Python for advanced machine learning

    4. D

      Mastering Python for beginners in data science

    Correct answer
    D

    Mastering Python for beginners in data science

    Explanation

    The course is titled 'mastering Python for beginners in data science' and explicitly states its focus on teaching Python for data science.

  2. 2

    According to the transcript, why is Python particularly important for data science?

    1. A

      It is the only language that can handle large datasets

    2. B

      It provides a vast variety of applications and packages for data science

    3. C

      It is the fastest programming language

    4. D

      It is the most secure programming language

    Correct answer
    B

    It provides a vast variety of applications and packages for data science

    Explanation

    The transcript states that Python provides a vast variety of applications used in data science and offers flexibility in machine learning and deep learning.

  3. 3

    What is an algorithm according to the transcript?

    1. A

      A complete Python program

    2. B

      A flowchart drawn on paper

    3. C

      A set of rules for coding

    4. D

      A step-by-step solution to a problem

    Correct answer
    D

    A step-by-step solution to a problem

    Explanation

    The transcript defines an algorithm as a step-by-step procedure or general solution that works for every instance of a problem.

  4. 4

    In the problem-solving example of finding the customer with maximum sales, what is the tie-breaking rule?

    1. A

      Pick the first record from top to bottom among those with maximum sales

    2. B

      Pick the record with the most recent entry

    3. C

      Pick the record with the highest sales value

    4. D

      Pick a random record among those with maximum sales

    Correct answer
    A

    Pick the first record from top to bottom among those with maximum sales

    Explanation

    The transcript states: 'if there are more than one records with maximum sales, then pick the first one from top to bottom and ignore the rest'.

  5. 5

    What is the purpose of a pseudocode according to the transcript?

    1. A

      To express algorithms in an unambiguous, concise way using structured keywords

    2. B

      To directly execute code on a computer

    3. C

      To draw graphical representations of algorithms

    4. D

      To replace all programming languages

    Correct answer
    A

    To express algorithms in an unambiguous, concise way using structured keywords

    Explanation

    Pseudocode is described as a structured way to express algorithms with unambiguous statements, using keywords like input, while, if, etc., without natural language ambiguity.

  6. 6

    In the transcript, what is the main advantage of pseudocode over flowcharts when the goal is to convert to actual programming code?

    1. A

      Flowcharts cannot represent loops

    2. B

      Flowcharts are more precise

    3. C

      Pseudocode is graphical and intuitive

    4. D

      Pseudocode is easier to convert to code

    Correct answer
    D

    Pseudocode is easier to convert to code

    Explanation

    The transcript states that writing pseudocode is more feasible because converting pseudocode to code is simpler than converting flowcharts to code.

  7. 7

    In the selection sort algorithm described in the transcript, what is the first step in each iteration?

    1. A

      Find the maximum value and swap it with the last element

    2. B

      Divide the list into halves and recursively sort them

    3. C

      Compare adjacent elements and swap them if needed

    4. D

      Find the minimum value and swap it with the first element of the remaining list

    Correct answer
    D

    Find the minimum value and swap it with the first element of the remaining list

    Explanation

    The selection sort algorithm finds the minimum value in the list (or remaining portion) and swaps it with the first element of that portion.

  8. 8

    Which tool is recommended in the transcript for installing Python with pre-installed packages like Jupyter Notebook?

    1. A

      Python.org installer

    2. B

      Miniconda

    3. C

      pip package manager

    4. D

      Anaconda distribution

    Correct answer
    D

    Anaconda distribution

    Explanation

    The transcript recommends using Anaconda distribution because it includes Python, many packages pre-installed, and Jupyter Notebook.

  9. 9

    In Python, what is the default index of the first element in a list?

    1. A

      1

    2. B

      It depends on the list type

    3. C

      0

    4. D

      -1

    Correct answer
    C

    0

    Explanation

    The transcript explicitly states that in Python, indexing starts from 0, not 1.

  10. 10

    What is the output of the following Python code? x = 5; print(x + 3.2)

    1. A

      5 + 3.2

    2. B

      8

    3. C

      Error: type mismatch

    4. D

      8.2

    Correct answer
    D

    8.2

    Explanation

    Python automatically upcasts integer to float when adding an int and float, resulting in 8.2.

  11. 11

    Which of the following is a valid variable name in Python?

    1. A

      class

    2. B

      my-var

    3. C

      _var

    4. D

      3x

    Correct answer
    C

    _var

    Explanation

    Variable names can start with an underscore. They cannot start with a digit, contain hyphens, or be reserved keywords like 'class'.

  12. 12

    What does the bool data type in Python represent?

    1. A

      Floating point numbers

    2. B

      Any integer value

    3. C

      Complex numbers

    4. D

      True or False values

    Correct answer
    D

    True or False values

    Explanation

    The bool data type has only two values: True and False.

  13. 13

    What is the result of True and False in Python?

    1. A

      None

    2. B

      True

    3. C

      False

    4. D

      Error

    Correct answer
    C

    False

    Explanation

    The 'and' operator returns True only if both operands are True. Since False is one operand, the result is False.

  14. 14

    What does the == operator do in Python?

    1. A

      Checks if two variables refer to the same object

    2. B

      Assigns a value to a variable

    3. C

      Returns True if both values are identical in type and value

    4. D

      Compares two values for equality

    Correct answer
    D

    Compares two values for equality

    Explanation

    The == operator is used for comparison, not assignment. It returns a Boolean value indicating whether the two operands are equal.

  15. 15

    In Python, what is the purpose of the if statement?

    1. A

      To conditionally execute a block of code based on a Boolean expression

    2. B

      To repeat a block of code multiple times

    3. C

      To define a function

    4. D

      To import external modules

    Correct answer
    A

    To conditionally execute a block of code based on a Boolean expression

    Explanation

    The if statement evaluates a condition and executes the indented block only if the condition is True.

  16. 16

    What is the difference between elif and else in an if-elif-else structure?

    1. A

      elif is used only once; else can be used multiple times

    2. B

      elif is used for additional conditions; else is a catch-all for when no condition is True

    3. C

      There is no difference; they are interchangeable

    4. D

      elif requires a condition; else does not require a colon

    Correct answer
    B

    elif is used for additional conditions; else is a catch-all for when no condition is True

    Explanation

    elif allows checking multiple conditions; else executes if none of the previous conditions are True.

  17. 17

    What does the break statement do inside a loop?

    1. A

      Exits the entire program

    2. B

      Skips the rest of the current iteration and starts the next iteration

    3. C

      Terminates the loop immediately and continues with the next statement after the loop

    4. D

      Pauses the loop until a condition is met

    Correct answer
    C

    Terminates the loop immediately and continues with the next statement after the loop

    Explanation

    The break statement exits the loop immediately, regardless of the loop condition.

  18. 18

    What is the output of the following code? for i in range(3): print(i)

    1. A

      1 2 3

    2. B

      0 1 2 3

    3. C

      0 1 2

    4. D

      0 1

    Correct answer
    C

    0 1 2

    Explanation

    range(3) generates numbers 0, 1, 2. The loop prints each on a new line.

  19. 19

    In Python, what does the else clause in a for loop do?

    1. A

      It executes when the loop condition becomes False

    2. B

      It executes after each iteration

    3. C

      It executes when the loop completes all iterations without a break

    4. D

      It executes only if the loop body is empty

    Correct answer
    C

    It executes when the loop completes all iterations without a break

    Explanation

    The else clause in a for loop runs only if the loop finishes normally (i.e., not interrupted by a break).

  20. 20

    What is the purpose of the def keyword in Python?

    1. A

      To declare a variable

    2. B

      To define a class

    3. C

      To define a function

    4. D

      To delete a variable

    Correct answer
    C

    To define a function

    Explanation

    The def keyword is used to define (create) a function.

  21. 21

    What is a docstring in a function?

    1. A

      A string used to define default arguments

    2. B

      A string used to document what the function does, accessible via help()

    3. C

      A special comment that is ignored by Python

    4. D

      A string that is automatically printed when the function is called

    Correct answer
    B

    A string used to document what the function does, accessible via help()

    Explanation

    A docstring is a string literal placed as the first statement in a function body, used to describe the function's purpose, and can be viewed using help().

  22. 22

    How does Python handle a variable number of arguments in a function?

    1. A

      Both of the above

    2. B

      By using the *args parameter to capture positional arguments as a tuple

    3. C

      By using the **kwargs parameter to capture keyword arguments as a dictionary

    4. D

      Python does not support variable number of arguments

    Correct answer
    A

    Both of the above

    Explanation

    Python allows *args for variable positional arguments and **kwargs for variable keyword arguments.

  23. 23

    What is the scope of a variable defined inside a function?

    1. A

      Built-in: accessible in all Python code

    2. B

      Enclosing: accessible in nested functions

    3. C

      Global: accessible everywhere

    4. D

      Local: accessible only inside the function

    Correct answer
    D

    Local: accessible only inside the function

    Explanation

    Variables defined inside a function are local to that function and cannot be accessed outside it unless returned.

  24. 24

    What is a Python module?

    1. A

      A built-in data structure

    2. B

      A file containing Python code that can be imported into other programs

    3. C

      A single function used in a program

    4. D

      A type of variable

    Correct answer
    B

    A file containing Python code that can be imported into other programs

    Explanation

    A module is a .py file containing functions, variables, and classes that can be reused by importing into other Python scripts.

  25. 25

    How do you import a custom module named mymod.py located in a different directory?

    1. A

      Copy the module into the Python library directory

    2. B

      Use import mymod and specify the full path in the import statement

    3. C

      Add the directory to sys.path and then use import mymod

    4. D

      Just write import mymod and Python will find it automatically

    Correct answer
    C

    Add the directory to sys.path and then use import mymod

    Explanation

    To import a module from a non-standard location, you must add its path to sys.path using sys.path.append(path) before importing.

  26. 26

    In Python, what is the result of 'Hello' + ' World'?

    1. A

      'HelloWorld'

    2. B

      Error: cannot concatenate strings

    3. C

      'Hello World '

    4. D

      'Hello World'

    Correct answer
    D

    'Hello World'

    Explanation

    The + operator concatenates strings, resulting in 'Hello World' (including the space).

  27. 27

    What does the string method strip() do?

    1. A

      Removes all spaces from the string

    2. B

      Removes leading and trailing whitespace

    3. C

      Removes all occurrences of a specified substring

    4. D

      Converts the string to lowercase

    Correct answer
    B

    Removes leading and trailing whitespace

    Explanation

    The strip() method removes whitespace from both ends of the string.

  28. 28

    How can you access the last character of a string s in Python?

    1. A

      s[-1]

    2. B

      s[len(s)]

    3. C

      s.last

    4. D

      s[0]

    Correct answer
    A

    s[-1]

    Explanation

    Negative indexing starts from the end; -1 refers to the last character.

  29. 29

    What will s = 'abcdef'; print(s[1:4]) output?

    1. A

      'bcd'

    2. B

      'abc'

    3. C

      'abcd'

    4. D

      'bcde'

    Correct answer
    A

    'bcd'

    Explanation

    Slicing s[start:stop] returns characters from index start (inclusive) to stop (exclusive). Here, indices 1,2,3 are 'b','c','d'.

  30. 30

    Which of the following correctly splits the string 'a,b,c' into a list?

    1. A

      'a,b,c'.split()

    2. B

      'a,b,c'.partition(',')

    3. C

      split('a,b,c', ',')

    4. D

      'a,b,c'.split(',')

    Correct answer
    D

    'a,b,c'.split(',')

    Explanation

    The split() method with a separator splits the string into a list of substrings.

  31. 31

    What is the difference between a list and a tuple in Python?

    1. A

      Lists are mutable; tuples are immutable

    2. B

      Lists can only contain numbers; tuples can contain any type

    3. C

      Lists are immutable; tuples are mutable

    4. D

      There is no difference

    Correct answer
    A

    Lists are mutable; tuples are immutable

    Explanation

    Lists are mutable (can be changed), while tuples are immutable (cannot be changed after creation).

  32. 32

    Which data structure in Python does not allow duplicate elements?

    1. A

      Dictionary

    2. B

      List

    3. C

      Set

    4. D

      Tuple

    Correct answer
    C

    Set

    Explanation

    A set is an unordered collection of unique elements; duplicates are automatically removed.

  33. 33

    How do you add a single element to an existing set s?

    1. A

      s.push(element)

    2. B

      s.insert(element)

    3. C

      s.append(element)

    4. D

      s.add(element)

    Correct answer
    D

    s.add(element)

    Explanation

    The add() method is used to add a single element to a set.

  34. 34

    In a dictionary, what must each key be?

    1. A

      Unique only within the dictionary values

    2. B

      Mutable

    3. C

      Immutable and hashable

    4. D

      A string

    Correct answer
    C

    Immutable and hashable

    Explanation

    Dictionary keys must be immutable and hashable (e.g., strings, numbers, tuples).

  35. 35

    What happens when you assign one list to another variable in Python (e.g., a = [1,2]; b = a)?

    1. A

      b becomes a reference to the same list object, so changes through b affect a

    2. B

      a and b are independent copies

    3. C

      An error occurs

    4. D

      a list is copied automatically

    Correct answer
    A

    b becomes a reference to the same list object, so changes through b affect a

    Explanation

    Assignment of a mutable object creates a reference, not a copy. Both names refer to the same list in memory.

  36. 36

    What is the purpose of np.arange(10) in NumPy?

    1. A

      Creates a list of numbers from 0 to 9

    2. B

      Creates a range object similar to Python's range

    3. C

      Creates a NumPy array of numbers from 0 to 9

    4. D

      Creates a random permutation of numbers from 0 to 10

    Correct answer
    C

    Creates a NumPy array of numbers from 0 to 9

    Explanation

    np.arange returns a NumPy array with evenly spaced values within the given range. For example, np.arange(10) gives array([0,1,...,9]).

  37. 37

    What does the shape attribute of a NumPy array return?

    1. A

      The number of dimensions

    2. B

      A tuple representing the size of each dimension

    3. C

      The total number of elements

    4. D

      The data type of the array

    Correct answer
    B

    A tuple representing the size of each dimension

    Explanation

    The shape attribute returns a tuple with the length of each dimension of the array.

  38. 38

    In NumPy, what is the difference between slicing with : and using an integer index?

    1. A

      Both return copies

    2. B

      Both return views

    3. C

      Slicing returns a view (reference) while integer indexing returns a copy

    4. D

      Slicing returns a copy while integer indexing returns a view

    Correct answer
    C

    Slicing returns a view (reference) while integer indexing returns a copy

    Explanation

    In NumPy, slicing returns a view of the original array (no data copied), whereas integer indexing (like fancy indexing) returns a copy.

  39. 39

    How can you create a 3x4 matrix of random numbers uniformly distributed between 0 and 1 using NumPy?

    1. A

      np.random.randn(3,4)

    2. B

      np.random.rand(3,4)

    3. C

      np.random.randint(0,1, (3,4))

    4. D

      np.random.uniform(0,1, (3,4))

    Correct answer
    B

    np.random.rand(3,4)

    Explanation

    np.random.rand(3,4) creates an array of shape (3,4) with random floats in [0,1).

  40. 40

    What does the axis parameter in NumPy functions like np.sum() indicate?

    1. A

      The number of dimensions to sum over

    2. B

      The number of times to repeat the operation

    3. C

      The data type of the result

    4. D

      The axis along which the operation is performed (0 for column-wise, 1 for row-wise)

    Correct answer
    D

    The axis along which the operation is performed (0 for column-wise, 1 for row-wise)

    Explanation

    The axis parameter specifies the dimension along which to compute the operation. For a 2D array, axis=0 sums over rows (column-wise), axis=1 sums over columns (row-wise).

  41. 41

    In pandas, what is the primary difference between a Series and a DataFrame?

    1. A

      Series is one-dimensional; DataFrame is two-dimensional

    2. B

      There is no difference

    3. C

      Series can only contain numbers; DataFrame can contain any data type

    4. D

      Series is mutable; DataFrame is immutable

    Correct answer
    A

    Series is one-dimensional; DataFrame is two-dimensional

    Explanation

    A Series is a one-dimensional labeled array, while a DataFrame is a two-dimensional labeled data structure with columns.

  42. 42

    How do you read a CSV file into a pandas DataFrame?

    1. A

      pd.open_csv(filename)

    2. B

      pd.read_csv(filename)

    3. C

      pd.load_csv(filename)

    4. D

      pd.import_csv(filename)

    Correct answer
    B

    pd.read_csv(filename)

    Explanation

    The function to read a CSV file is pd.read_csv().

  43. 43

    What does the head() method in pandas do?

    1. A

      Returns a summary of the DataFrame

    2. B

      Returns the column names

    3. C

      Returns the first few rows (default 5) of the DataFrame

    4. D

      Returns the last few rows of the DataFrame

    Correct answer
    C

    Returns the first few rows (default 5) of the DataFrame

    Explanation

    The head() method returns the first n rows (default 5) of the DataFrame.

  44. 44

    How can you handle missing values (NaN) in a pandas DataFrame by filling them with a constant?

    1. A

      df.dropna()

    2. B

      df.replace(NaN, value)

    3. C

      df.impute(value)

    4. D

      df.fillna(value)

    Correct answer
    D

    df.fillna(value)

    Explanation

    The fillna() method fills missing values with a specified value.

  45. 45

    In pandas, what is the difference between iloc and loc for indexing?

    1. A

      Both use integer positions

    2. B

      iloc uses labels; loc uses integer positions

    3. C

      iloc uses integer positions; loc uses labels

    4. D

      Both use labels

    Correct answer
    C

    iloc uses integer positions; loc uses labels

    Explanation

    iloc is integer-location based indexing (like standard NumPy), while loc is label-based indexing.

  46. 46

    What does the groupby() method in pandas do?

    1. A

      Groups rows by a column and then applies an aggregate function

    2. B

      Sorts the DataFrame by a column

    3. C

      Removes duplicate rows

    4. D

      Creates a new column based on a condition

    Correct answer
    A

    Groups rows by a column and then applies an aggregate function

    Explanation

    groupby() splits the data into groups based on a column, allowing you to apply aggregation functions like sum, mean, etc.

  47. 47

    Which function in matplotlib is used to create a line plot?

    1. A

      plt.lineplot()

    2. B

      plt.plot()

    3. C

      plt.hist()

    4. D

      plt.scatter()

    Correct answer
    B

    plt.plot()

    Explanation

    The plot() function is used for line plots.

  48. 48

    How can you display a plot created with matplotlib in a Jupyter Notebook?

    1. A

      plt.display()

    2. B

      plt.print()

    3. C

      plt.render()

    4. D

      plt.show()

    Correct answer
    D

    plt.show()

    Explanation

    The plt.show() command displays the figure in the notebook.

Question 1 of 48
Question 1 of 48

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent