1. Home
  2. Explore
  3. Computer Science
  4. Python Variables Basics

Study methods

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

Python Variables Basics

  • 33Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Python Basics and Installation

What is Python and what are some of its common uses?

33 questions

  1. 1

    What is Python and what are some of its common uses?

    Model answer

    Python is a popular programming language used for backend web development, data analysis, and scientific research.

    Explanation

    Python's simple syntax makes it easy to learn, and it's used at companies like Google and in university courses.

  2. 2

    How do you install Python and Jupyter Notebook together?

    Model answer

    Install Anaconda, which includes both Python and Jupyter Notebook.

    Explanation

    Anaconda is a package manager that simplifies installing Python and scientific libraries along with Jupyter Notebook.

  3. 3

    Which version of Python should you use for this tutorial?

    Model answer

    Python 3 should be used, not Python 2.

    Explanation

    The tutorial specifies using Python 3 throughout the course.

  4. 4

    What is Anaconda?

    Model answer

    Anaconda is a package manager used to install Python, Jupyter Notebook, and many scientific libraries at once.

    Explanation

    It's designed for math and science but simplifies setup for beginners.

  5. 5

    After installing Anaconda, how do you launch Jupyter Notebook?

    Model answer

    Open Anaconda Navigator and click 'Launch' under Jupyter Notebook.

    Explanation

    Anaconda Navigator provides a graphical interface to launch applications like Jupyter Notebook.

  6. 6

    What are the two main components of Jupyter Notebook?

    Model answer

    A server (command-line interface) and a user interface in a browser window or tab.

    Explanation

    The server runs the code, and the browser interface allows you to write and execute code.

  7. 7

    What is the typical URL for the Jupyter Notebook interface?

    Model answer

    Explanation

    When Jupyter starts, it opens a browser connected to localhost on port 8888 (or similar).

  8. 8

    What is a 'cell' in Jupyter Notebook?

    Model answer

    A cell is a box where you type Python code; it can be executed individually.

    Explanation

    Cells allow you to break your code into sections and run them separately.

  9. 9

    What file extension does a Jupyter Notebook have?

    Model answer

    .ipynb

    Explanation

    For example, 'what_are_variables.ipynb'.

  10. 10

    How do you create a new Jupyter Notebook?

    Model answer

    In the Jupyter interface, click 'New' at the top right and select 'Python 3'.

    Explanation

    This creates a new notebook with a Python 3 kernel.

  11. 11

    What does the print function do in Python?

    Model answer

    The print function outputs the content inside its parentheses to the console.

    Explanation

    It can print strings, numbers, or the value of variables.

  12. 12

    How do you denote a string in Python?

    Model answer

    A string is enclosed in either double quotes or single quotes.

    Explanation

    Both 'hello' and "hello" are valid strings.

  13. 13

    What happens if you forget to close quotes or parentheses in a print statement?

    Model answer

    You get an error, and the code will not run.

    Explanation

    Python requires exact syntax; missing closing characters cause a syntax error.

  14. 14

    Can you print a number directly using the print function?

    Model answer

    Yes, you can print a number like print(3) without quotes.

    Explanation

    Numbers are not strings; they are printed as their numeric value.

  15. 15

    If you have two print statements in the same cell, what happens when you run the cell?

    Model answer

    Both lines are executed one after another, printing each result on a separate line.

    Explanation

    Cells can contain multiple lines of code; they run sequentially.

  16. 16

    What is a variable in Python?

    Model answer

    A variable is a name that refers to a value, like a name tag attached to an object.

    Explanation

    In Python, variables are not containers but references to objects.

  17. 17

    How do you assign a value to a variable in Python?

    Model answer

    Use the syntax: variable_name = value, e.g., a = 1.

    Explanation

    The equal sign is the assignment operator.

  18. 18

    What is the more accurate way to think about variables in Python compared to languages like C++?

    Model answer

    In Python, a variable is like a name tag that refers to a value, not a box that contains the value.

    Explanation

    This distinction becomes important when variables point to the same object.

  19. 19

    Can a variable refer to a string?

    Model answer

    Yes, a variable can refer to a string, e.g., c = 'hello there'.

    Explanation

    Variables are not limited to numbers; they can hold any Python object.

  20. 20

    How do you print the value a variable is referring to?

    Model answer

    Use print(variable_name) without quotes around the variable name.

    Explanation

    If you put quotes, Python will print the variable's name as a string.

  21. 21

    What error occurs if you try to print a variable that has not been defined?

    Model answer

    NameError: name 'x' is not defined.

    Explanation

    Python does not allow you to use a variable before it has been assigned a value.

  22. 22

    Can you reassign a variable to a different value?

    Model answer

    Yes, you can reassign a variable to any new value, even a different data type.

    Explanation

    For example, b = 2 then b = 1 or b = 'ah'.

  23. 23

    Can two variables point to the same value?

    Model answer

    Yes, two or more variables can refer to the same object, e.g., b = 2; d = 2.

    Explanation

    Both b and d refer to the integer 2.

  24. 24

    What happens if you reassign a variable that already exists?

    Model answer

    The variable now refers to the new value; the old value may be garbage-collected if no other variable refers to it.

    Explanation

    Reassignment changes the reference, not the original object.

  25. 25

    Can a variable be reassigned to a value of a different type?

    Model answer

    Yes, Python is dynamically typed; a variable that held a number can later hold a string.

    Explanation

    For example, b = 1 then b = 'hello' is perfectly valid.

  26. 26

    If you write f = a (where a = 1), what does f refer to?

    Model answer

    f refers to the value that a refers to, which is 1.

    Explanation

    f does not refer to the variable a; it gets the current value of a.

  27. 27

    If a is reassigned after setting f = a, does f change?

    Model answer

    No, f still refers to the original value (1), not the new value of a.

    Explanation

    f's reference is independent; it was set to the value 1, not to a.

  28. 28

    What is the output of the following code? a = 1; c = 'hello'; f = a; a = 2; print(f)

    Model answer

    1

    Explanation

    f was assigned the value a referred to at that time (1), so f remains 1 even though a changes.

  29. 29

    What is the output of the following code? c = 'hello world'; g = c; c = 'hello'; print(g)

    Model answer

    hello world

    Explanation

    g refers to the string 'hello world' that c originally pointed to; reassigning c does not affect g.

  30. 30

    Why does the naive swap v1 = v2; v2 = v1 fail?

    Model answer

    Because after v1 = v2, both variables refer to the same value (v2's original value), so v2 = v1 just points v2 to that same value, losing the original v1 content.

    Explanation

    The naive code overwrites the original value of v1 before it can be stored.

  31. 31

    What is the correct way to swap the values of two variables using one temporary variable?

    Model answer

    temp = v1; v1 = v2; v2 = temp

    Explanation

    The temporary variable preserves the original value of v1 so it can be assigned to v2 later.

  32. 32

    In the swapping problem, why is using a temporary variable better than directly writing v1 = 'second string'; v2 = 'first string'?

    Model answer

    Because if the strings change, you don't have to rewrite the code; the solution works with any values without repeating them.

    Explanation

    Hardcoding the values is not scalable or maintainable.

  33. 33

    What is an alternative solution to swapping two variables using two temporary variables?

    Model answer

    temp1 = v1; temp2 = v2; v1 = temp2; v2 = temp1

    Explanation

    This works but uses an extra temporary variable; the one-temp method is more efficient.

1 of 33
Question 1 of 33

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent