CS-1064 Q3
Sure, here are the questions and answers formatted for Quizlet:
### Escape Sequences:
Question: What is an escape sequence in Python?
Answer: An escape sequence is a combination of characters preceded by a backslash (`\`) used to represent special characters within strings.
---
Question: What does the escape sequence \n do?
Answer: It represents a newline character, which moves the cursor to the beginning of the next line.
---
Question: How do you represent a tab in Python using escape sequences?
Answer: By using the escape sequence \t, which inserts a tab.
---
Question: How would you represent a single quote within a string using escape sequences?
Answer: By using the escape sequence \'.
---
Question: How would you represent a backslash character within a string using escape sequences?
Answer: By using the escape sequence \\.
---
### Comprehensions:
Question: What are comprehensions in Python?
Answer: Comprehensions are concise ways to create sequences such as lists, dictionaries, and sets.
---
Question: How would you create a list comprehension in Python?
Answer: By using square brackets (`[]`) enclosing an expression followed by a for clause, like [x**2 for x in range(10)].
---
Question: What does a dictionary comprehension return?
Answer: A dictionary comprehension returns a dictionary created from an expression and a for loop within curly braces (`{}`).
---
Question: How do you create a set comprehension?
Answer: By using curly braces (`{}`) enclosing an expression followed by a for clause, like {x**2 for x in range(10)}.
---
### Dictionaries:
Question: What is a dictionary in Python?
Answer: A dictionary is an unordered collection of items, where each item is a key-value pair.
---
Question: How do you access a value in a dictionary?
Answer: By specifying the key within square brackets (`[]`), like my_dict['key1'].
---
Question: How would you add a new key-value pair to a dictionary?
Answer: By assigning a value to a new key, like my_dict['key3'] = 'value3'.
---
Question: How do you remove a key-value pair from a dictionary?
Answer: By using the del keyword followed by the dictionary name and the key to be deleted, like del my_dict['key2'].
---
### Sets:
Question: What is a set in Python?
Answer: A set is an unordered collection of unique elements.
---
Question: How do you add an element to a set?
Answer: By using the add() method, like my_set.add(4).
---
Question: How do you remove an element from a set?
Answer: By using the remove() method, like my_set.remove(3).
---
Question: What operation would you use to find the union of two sets?
Answer: The | operator, like set1 | set2.
---
### Objects/Classes:
Question: What is a class in Python?
Answer: A class is a blueprint for creating objects in Python.
---
Question: How do you define a class in Python?
Answer: By using the class keyword followed by the class name and a colon, like class MyClass:.
---
Question: What is the purpose of the __init__ method in a class?
Answer: It is a special method called the constructor, used to initialize objects of the class.
---
Question: How would you create an object of a class in Python?
Answer: By calling the class name followed by parentheses and any required arguments, like obj1 = MyClass(5).
---
Question: What is inheritance in object-oriented programming?
Answer: Inheritance is the mechanism of basing a new class on an existing class, inheriting its attributes and methods.
---
Question: How do you achieve encapsulation in Python?
Answer: By using private variables, indicated by prefixing variable names with double underscores __.