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

  • 38Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Problem Solving and Algorithms

What is an algorithm?

38 questions

  1. 1

    What is an algorithm?

    Model answer

    An algorithm is a step-by-step procedure that provides a general solution to every instance of a problem.

    Explanation

    The instructor defines an algorithm as a step-by-step solution that works for every instance of a problem, not just one specific case.

  2. 2

    What is the difference between pseudocode and flowchart?

    Model answer

    Pseudocode is a textual, structured way to express an algorithm using keywords, while flowchart is a graphical representation using shapes like ovals, rectangles, and parallelograms.

    Explanation

    Both are used to express algorithms, but pseudocode is closer to actual code and easier to convert to a programming language, especially for complex problems.

  3. 3

    In a flowchart, which shape is used for input operations?

    Model answer

    A parallelogram is used for input operations.

    Explanation

    The instructor explicitly states that input actions are described using a parallelogram in flowcharts.

  4. 4

    What does it mean that Python is dynamically typed?

    Model answer

    It means you do not need to specify the type of a variable; the type is automatically determined based on the value assigned to it.

    Explanation

    Python determines the type of a variable at runtime from its assigned value, as shown with examples like x = 2 making x an integer.

  5. 5

    Can a variable name start with a digit?

    Model answer

    No, a variable name cannot start with a digit.

    Explanation

    The instructor shows that 3x = 5 gives a syntax error, and explains that variable names cannot begin with digits or most special characters except underscore.

  6. 6

    What is the result of the expression 2 + 3.0?

    Model answer

    5.0 (a float).

    Explanation

    When an integer and a float are added, Python upcasts the integer to float, so the result is a float.

  7. 7

    What Boolean value does the expression '3 == 3.0' evaluate to?

    Model answer

    True.

    Explanation

    The == operator compares values, ignoring type; 3 and 3.0 are numerically equal, so it returns True.

  8. 8

    What is the difference between the 'and' and 'or' operators?

    Model answer

    'and' returns True only if both operands are True; 'or' returns True if at least one operand is True.

    Explanation

    The instructor defines both: 'and' requires both true, 'or' requires at least one true.

  9. 9

    Write an if-else statement that prints 'a' if a > b, else prints 'b'.

    Model answer

    if a > b: print('a') else: print('b')

    Explanation

    The example from the course: if a > b then print a, else print b.

  10. 10

    What does the 'elif' keyword do?

    Model answer

    'elif' is short for 'else if' and allows checking an additional condition if the previous if condition is false.

    Explanation

    The instructor demonstrates elif in a grading example: if marks >= 85: print('A'), elif marks >= 80: print('A-'), etc.

  11. 11

    What is the purpose of the 'break' statement in a loop?

    Model answer

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

    Explanation

    The course explains that 'break' exits the loop when encountered, even if the loop condition is still true.

  12. 12

    What does the 'continue' statement do in a loop?

    Model answer

    The 'continue' statement skips the rest of the current iteration and moves to the next iteration of the loop.

    Explanation

    In the course, continue is shown to skip remaining statements in the loop body and start the next iteration.

  13. 13

    How does the 'else' clause of a 'for' loop execute?

    Model answer

    The 'else' clause executes only if the loop completes all iterations normally (i.e., without encountering a 'break' statement).

    Explanation

    The course demonstrates that if a break occurs, the else does not run; otherwise, it runs after the loop finishes.

  14. 14

    What is the syntax to define a function in Python?

    Model answer

    Use the 'def' keyword, followed by the function name, parentheses for parameters, and a colon, then the indented function body.

    Explanation

    Example: def my_function(): print('Hello')

  15. 15

    What is a docstring in a function?

    Model answer

    A docstring is a multi-line string placed at the beginning of a function to describe its purpose.

    Explanation

    The instructor explains that docstrings are used for documentation and can be accessed with help() or ?.

  16. 16

    What happens to variables defined inside a function when the function ends?

    Model answer

    They are destroyed and cannot be accessed outside the function.

    Explanation

    The course shows that variables inside a function are local and not available after the function completes.

  17. 17

    How do you define a function that accepts a variable number of positional arguments?

    Model answer

    Use a single asterisk (*) before the parameter name, e.g., def my_func(*args):

    Explanation

    The instructor demonstrates *args to accept an arbitrary number of arguments.

  18. 18

    How do you define a function that accepts a variable number of keyword arguments?

    Model answer

    Use a double asterisk (**) before the parameter name, e.g., def my_func(**kwargs):

    Explanation

    The course uses **kwargs to handle a variable number of key-value arguments.

  19. 19

    What is a module in Python?

    Model answer

    A module is a Python file containing functions, variables, and classes that can be imported and used in other Python files.

    Explanation

    The instructor explains that modules are .py files used to organize code for reuse.

  20. 20

    What is the index of the first character in a string?

    Model answer

    Explanation

    String indexing starts at 0 in Python.

  21. 21

    What does the string method .split() return?

    Model answer

    It returns a list of substrings split by a specified delimiter.

    Explanation

    The course shows .split() returning a list of strings separated by the given delimiter.

  22. 22

    Can you modify a string after it is created?

    Model answer

    No, strings are immutable.

    Explanation

    The instructor states that strings cannot be changed once created.

  23. 23

    What is the difference between a list and a tuple?

    Model answer

    A list is mutable (can be changed), while a tuple is immutable (cannot be changed).

    Explanation

    The comparison slide lists list as changeable and tuple as unchangeable.

  24. 24

    How do you add an element to the end of a list?

    Model answer

    Using the .append() method.

    Explanation

    Example: my_list.append(5).

  25. 25

    What is the main characteristic of a set?

    Model answer

    A set is unordered, mutable, and cannot contain duplicate elements.

    Explanation

    The data structures comparison slide describes sets as unordered, changeable, and with no duplicates.

  26. 26

    How do you access a value in a dictionary?

    Model answer

    Using the key inside square brackets: dict[key].

    Explanation

    Example: d['a'] returns the value for key 'a'.

  27. 27

    How do you create a NumPy array from a list?

    Model answer

    Using np.array(list).

    Explanation

    Example: arr = np.array([1,2,3]).

  28. 28

    What attribute of a NumPy array gives the number of dimensions?

    Model answer

    .ndim.

    Explanation

    The course uses a.ndim to get the number of dimensions.

  29. 29

    What is the shape of a 1D NumPy array with 10 elements?

    Model answer

    (10,).

    Explanation

    The shape property returns a tuple; for a 1D array, it is (length,).

  30. 30

    How do you create a 2D array of zeros of shape (3,4)?

    Model answer

    np.zeros((3,4)).

    Explanation

    The course demonstrates np.zeros for creating arrays of zeros.

  31. 31

    What is the difference between slicing in NumPy and slicing in a regular Python list?

    Model answer

    Slicing a NumPy array returns a view (reference to same memory), while slicing a list returns a copy.

    Explanation

    The course explains that modifying a slice of a NumPy array affects the original, unlike lists.

  32. 32

    What is broadcasting in NumPy?

    Model answer

    Broadcasting allows operations between arrays of different shapes by automatically expanding the smaller array to match the larger one.

    Explanation

    Example: arr + 5 adds 5 to every element.

  33. 33

    Why are universal functions (ufuncs) in NumPy faster than Python loops?

    Model answer

    Because they are implemented in compiled C code and operate on entire arrays without Python-level loops.

    Explanation

    The course shows time comparison where np.sum is ~100x faster than Python sum.

  34. 34

    What is the main difference between a pandas Series and a pandas DataFrame?

    Model answer

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

    Explanation

    The course introduces Series as 1D and DataFrame as 2D.

  35. 35

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

    Model answer

    Using pd.read_csv('filename.csv').

    Explanation

    The instructor uses pd.read_csv to load the COVID-19 dataset.

  36. 36

    How do you handle missing values in a DataFrame by filling them with a constant?

    Model answer

    Use df.fillna(value) or df.fillna(value, inplace=True).

    Explanation

    The course shows df.fillna('Not Available') to replace NaN values.

  37. 37

    What does the .groupby() method do in pandas?

    Model answer

    It groups data by one or more columns, allowing aggregate operations like sum, mean, etc. on each group.

    Explanation

    The instructor uses groupby('country').sum() to sum values per country.

  38. 38

    How do you create a simple line plot with matplotlib?

    Model answer

    Use plt.plot(x, y) followed by plt.show().

    Explanation

    Example: plt.plot(x, y) where x and y are arrays of data.

1 of 38
Question 1 of 38

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent