Podcast
Questions and Answers
What will be the result of the expression {**d1, 'a': 10, 'c': 3} if d1 is defined as {'a': 1, 'b': 2}?
What will be the result of the expression {**d1, 'a': 10, 'c': 3} if d1 is defined as {'a': 1, 'b': 2}?
In a nested unpacking assignment, how many times can the * operator be used in the left-hand side?
In a nested unpacking assignment, how many times can the * operator be used in the left-hand side?
Given the list li = [1, 2, [3, 4]], what will the variable b hold after unpacking a, b, c = li?
Given the list li = [1, 2, [3, 4]], what will the variable b hold after unpacking a, b, c = li?
What is the result of the unpacking a, *b, (c, d, e) = [1, 2, 3, 'XYZ']?
What is the result of the unpacking a, *b, (c, d, e) = [1, 2, 3, 'XYZ']?
Signup and view all the answers
What happens if you try to use the * operator more than once in a single unpacking assignment?
What happens if you try to use the * operator more than once in a single unpacking assignment?
Signup and view all the answers
What will be the output of print(s)
if s is defined as s = {7, -77, 6, 'g'}
?
What will be the output of print(s)
if s is defined as s = {7, -77, 6, 'g'}
?
Signup and view all the answers
Which assignment will result in a = -77
, b = ['g', 6']
, and c = 7
from s
?
Which assignment will result in a = -77
, b = ['g', 6']
, and c = 7
from s
?
Signup and view all the answers
What does the expression s = {*d1, *d2, *d3}
produce when d1, d2, and d3 contain overlapping keys?
What does the expression s = {*d1, *d2, *d3}
produce when d1, d2, and d3 contain overlapping keys?
Signup and view all the answers
What is the result of d = {**d1, **d2, **d3}
if d1, d2, and d3 all contain the key 'h'?
What is the result of d = {**d1, **d2, **d3}
if d1, d2, and d3 all contain the key 'h'?
Signup and view all the answers
Which statement about unpacking sets is true?
Which statement about unpacking sets is true?
Signup and view all the answers
What will be the output of print(li)
if li = [*d1, *d2, *d3]
?
What will be the output of print(li)
if li = [*d1, *d2, *d3]
?
Signup and view all the answers
What is not a valid use of the ** operator?
What is not a valid use of the ** operator?
Signup and view all the answers
Which of the following would result in extracting only the first key from a dictionary?
Which of the following would result in extracting only the first key from a dictionary?
Signup and view all the answers
What will happen if the function log
is called without providing a value for the parameter dt
?
What will happen if the function log
is called without providing a value for the parameter dt
?
Signup and view all the answers
Which statement regarding default values in function arguments is correct?
Which statement regarding default values in function arguments is correct?
Signup and view all the answers
Which part of the lambda expression is mandatory?
Which part of the lambda expression is mandatory?
Signup and view all the answers
What is a primary advantage of using lambda expressions?
What is a primary advantage of using lambda expressions?
Signup and view all the answers
What happens when 'a' is passed to sys.getrefcount()?
What happens when 'a' is passed to sys.getrefcount()?
Signup and view all the answers
In statically typed languages like C++ and Java, what will occur if you try to assign a different type to a variable?
In statically typed languages like C++ and Java, what will occur if you try to assign a different type to a variable?
Signup and view all the answers
How does Python treat variables in terms of typing?
How does Python treat variables in terms of typing?
Signup and view all the answers
What does the type() function do when called on a variable in Python?
What does the type() function do when called on a variable in Python?
Signup and view all the answers
If 'my_str' is initially assigned the string 'MEK3100', what will happen when it is later assigned the value 10?
If 'my_str' is initially assigned the string 'MEK3100', what will happen when it is later assigned the value 10?
Signup and view all the answers
In the example given, what does the memory address 0x1000 represent?
In the example given, what does the memory address 0x1000 represent?
Signup and view all the answers
What is true about the variable 'my_str' in Python?
What is true about the variable 'my_str' in Python?
Signup and view all the answers
What does the reference count indicate about an object in memory?
What does the reference count indicate about an object in memory?
Signup and view all the answers
What will be the output of list(filter(None, [0, 1, 2, 3, 4]))?
What will be the output of list(filter(None, [0, 1, 2, 3, 4]))?
Signup and view all the answers
Which of the following correctly demonstrates using zip with three iterables?
Which of the following correctly demonstrates using zip with three iterables?
Signup and view all the answers
What does the expression [x**2 for x in [2, 3, 4]] return?
What does the expression [x**2 for x in [2, 3, 4]] return?
Signup and view all the answers
What is the output of list(map(lambda x, y: x + y, [1, 2, 3], [10, 20, 30]))?
What is the output of list(map(lambda x, y: x + y, [1, 2, 3], [10, 20, 30]))?
Signup and view all the answers
Which of the following expressions effectively filters out odd numbers from the list [1, 2, 3, 4]?
Which of the following expressions effectively filters out odd numbers from the list [1, 2, 3, 4]?
Signup and view all the answers
What is the purpose of reducing functions in Python?
What is the purpose of reducing functions in Python?
Signup and view all the answers
Which expression correctly combines map and filter to square numbers less than 25 from the range of 10?
Which expression correctly combines map and filter to square numbers less than 25 from the range of 10?
Signup and view all the answers
What will the output be for list(zip(range(100), 'abcd'))?
What will the output be for list(zip(range(100), 'abcd'))?
Signup and view all the answers
What is meant by 'nonlocal scope' in the context of nested functions?
What is meant by 'nonlocal scope' in the context of nested functions?
Signup and view all the answers
Which statement is true when referring to a variable from an enclosing scope within an inner function?
Which statement is true when referring to a variable from an enclosing scope within an inner function?
Signup and view all the answers
What will the output be when calling the function 'outer_func' defined in the example?
What will the output be when calling the function 'outer_func' defined in the example?
Signup and view all the answers
In which situation will Python look for a variable in the global scope when executing an inner function?
In which situation will Python look for a variable in the global scope when executing an inner function?
Signup and view all the answers
What happens if the inner function tries to print a variable that is only defined locally in the outer function?
What happens if the inner function tries to print a variable that is only defined locally in the outer function?
Signup and view all the answers
What would happen if an inner function attempts to modify a variable defined in the outer function without declaring it as 'nonlocal'?
What would happen if an inner function attempts to modify a variable defined in the outer function without declaring it as 'nonlocal'?
Signup and view all the answers
Which of the following correctly describes the scopes accessed by an inner function?
Which of the following correctly describes the scopes accessed by an inner function?
Signup and view all the answers
If 'outer_func' contains a variable 'a' defined as 10, what will be the value of 'a' inside its inner function after calling 'outer_func'?
If 'outer_func' contains a variable 'a' defined as 10, what will be the value of 'a' inside its inner function after calling 'outer_func'?
Signup and view all the answers
What is the main issue with using a mutable object or callable as a default argument in a function?
What is the main issue with using a mutable object or callable as a default argument in a function?
Signup and view all the answers
What happens when you provide a specific value for the parameter dt
in the log
function?
What happens when you provide a specific value for the parameter dt
in the log
function?
Signup and view all the answers
Which of the following best describes a lambda expression?
Which of the following best describes a lambda expression?
Signup and view all the answers
In the log
function implementation, what is the purpose of checking if dt
is None?
In the log
function implementation, what is the purpose of checking if dt
is None?
Signup and view all the answers
When defining a function with default arguments, what happens if the default argument is mutable?
When defining a function with default arguments, what happens if the default argument is mutable?
Signup and view all the answers
What is the correct syntax for creating a lambda function with one parameter?
What is the correct syntax for creating a lambda function with one parameter?
Signup and view all the answers
Which scenario would correctly utilize the log
function to log a message with custom date/time?
Which scenario would correctly utilize the log
function to log a message with custom date/time?
Signup and view all the answers
Why should default values for function parameters not be evaluated when the function is defined?
Why should default values for function parameters not be evaluated when the function is defined?
Signup and view all the answers
Study Notes
Programming 2 - Lecture 1
- Python is an Object-Oriented Programming (OOP) language.
- Almost everything in Python is an object.
- Data objects have properties and methods.
- A class is like an object constructor, or a blueprint for creating objects.
Object Oriented Programming
- Python supports many data types, including integers, floats, strings, lists, sets, dictionaries, and more.
- Each data type is an object.
- Each object has a type, an internal representation, and associated methods.
- An object is an instance of a type (e.g., 777 is an instance of int, "Python" is an instance of str).
Object Oriented Programming
- Creating a class uses the keyword
class
. - Creating objects from a class uses the class name followed by parentheses.
- Example (using
MyClass
class)class MyClass: x = 5 p1 = MyClass() print(p1.x)
The __init__
Function
- All classes have an
__init__
function, automatically called when an object is created. - Used to initialize properties of an object to meaningful values.
Object Methods
- Objects can have methods (functions associated with an object).
- The parameter
self
refers to the object itself.
Modify Object Properties
- Object properties can be modified after they're created.
Defining Your Own Print Method
- User can define methods for handling the output of objects using
__str__
method. - Custom print methods change how objects are printed
Other Special Methods
- Python has special methods for operators like
+
,-
,<
,>
,len()
,str()
.
add
Method
- Used for custom operator handling within a class, for example adding two coordinate objects
Public vs Private Members
- Python does not have private variables in the strict sense.
- Single underscore prefix is a convention for protected attributes.
- Double underscore prefix is a convention for private attributes, but does not enforce privacy.
Lecture 2 - Inheritance
- Inheritance defines a way for a new class to inherit properties and methods from an existing class.
- Parent class (base class) is the class being inherited from.
- Child class (derived class) inherits from the parent class.
Lecture 3 - Variables & Memory
- Variables are memory references that point to objects.
- The id() method returns the memory address of an object.
- The hex() method converts a base-10 integer to a hexadecimal string.
- Reference counting counts how many variables refer to an object.
- Mutability refers to whether the internal state of an object can be changed (immutable).
- Immutable objects (integers, booleans) do not change.
- Mutable objects (lists) can change.
Lecture 4 - Functions Parameters
- Arguments are the values passed to a function when it's called.
- Parameters are the variables inside a function's definition.
- Positional arguments are passed in the order they are declared.
- Keyword arguments are passed using the parameter names.
- Default values for parameters can be specified in the function definition; using
def func(a, b=0)
- Default values are evaluated at the time of function definition (not every time it's called).
Lecture 5 - First-Class Functions
- Lambda expressions are anonymous functions.
- They're defined with the
lambda
keyword, and their body is a single expression. - Lambdas can be assigned to variables and passed as arguments to functions (like other functions defined with
def
).
Lecture 6 - Scopes & Closures
- A variable's scope is the part of the program where the variable is visible and accessible.
- There are three scopes
- Built-in scope
- Global scope
- Local scope (function local scope)
- Python searches scopes locally, then globally, then if not found, the built-in scope.
- If an inner function uses variables from the outer function scopes for its calculations and operations, then the inner function will become a closure in Python.
Lecture 7 - Decorators
- Decorators are functions that wrap another function and add functionality to it
- Decorators can be useful for adding functionality.
- Decorators are often used for tasks like logging, timing and other things like validation.
Lecture 8 - Tuples as Data Structures
- Tuples are sequences.
- Tuples contain heterogeneous data.
- Tuples are immutable.
- Tuples are more useful when the order of the objects matter.
- Named tuples extend tuples by giving names to entries.
Lecture 9 - Sequence Types
- Sequences can be indexed, sliced and iterated over
Lecture 10 - Iterables & Iterators
- Iterables are objects that can be iterated over.
- Iterators are objects that implement the
iter
andnext
methods. - The
iter
method returns a new iterator object each time from the iterable.
Lecture 11 - Generators
- Python's
yield
keyword creates a generator, instead of returning the whole list. - Generators are also iterators and can handle infinite sequences.
- Python's
iter
andnext
methods are what makes for/while loops work in the first place.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz tests your understanding of dictionary unpacking and assignment in Python, including nested unpacking and the use of the * and ** operators. It covers various scenarios and expected outcomes to ensure a comprehensive grasp of these concepts.