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}?
- {'a': 1, 'c': 3, 'b': 2}
- {'a': 10, 'b': 2, 'c': 3} (correct)
- {'a': 1, 'b': 2}
- {'a': 10, 'c': 3}
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?
- Only once (correct)
- Not at all
- At least twice
- As many as desired
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?
- 3
- 2 (correct)
- [3, 4]
- None of the above
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']?
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?
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'}
?
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
?
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?
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'?
Which statement about unpacking sets is true?
Which statement about unpacking sets is true?
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]
?
What is not a valid use of the ** operator?
What is not a valid use of the ** operator?
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?
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
?
Which statement regarding default values in function arguments is correct?
Which statement regarding default values in function arguments is correct?
Which part of the lambda expression is mandatory?
Which part of the lambda expression is mandatory?
What is a primary advantage of using lambda expressions?
What is a primary advantage of using lambda expressions?
What happens when 'a' is passed to sys.getrefcount()?
What happens when 'a' is passed to sys.getrefcount()?
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?
How does Python treat variables in terms of typing?
How does Python treat variables in terms of typing?
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?
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?
In the example given, what does the memory address 0x1000 represent?
In the example given, what does the memory address 0x1000 represent?
What is true about the variable 'my_str' in Python?
What is true about the variable 'my_str' in Python?
What does the reference count indicate about an object in memory?
What does the reference count indicate about an object in memory?
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]))?
Which of the following correctly demonstrates using zip with three iterables?
Which of the following correctly demonstrates using zip with three iterables?
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?
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]))?
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]?
What is the purpose of reducing functions in Python?
What is the purpose of reducing functions in Python?
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?
What will the output be for list(zip(range(100), 'abcd'))?
What will the output be for list(zip(range(100), 'abcd'))?
What is meant by 'nonlocal scope' in the context of nested functions?
What is meant by 'nonlocal scope' in the context of nested functions?
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?
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?
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?
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?
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'?
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?
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'?
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?
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?
Which of the following best describes a lambda expression?
Which of the following best describes a lambda expression?
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?
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?
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?
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?
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?
Flashcards
Reference Counting
Reference Counting
A method used to track how many variables are referencing a particular object in memory.
Dynamic Typing
Dynamic Typing
A programming language feature where variable types are not explicitly declared and are determined at runtime.
Static Typing
Static Typing
A programming language feature where variable types are declared and fixed at compile time, preventing type mismatches.
sys.getrefcount(a)
sys.getrefcount(a)
Signup and view all the flashcards
Type checking (Python)
Type checking (Python)
Signup and view all the flashcards
Memory Address (Example)
Memory Address (Example)
Signup and view all the flashcards
Variable in relation to an object
Variable in relation to an object
Signup and view all the flashcards
Type() Function in Python
Type() Function in Python
Signup and view all the flashcards
Dictionary Unpacking with **
Dictionary Unpacking with **
Signup and view all the flashcards
Nested Unpacking with *
Nested Unpacking with *
Signup and view all the flashcards
Unpacking Strings
Unpacking Strings
Signup and view all the flashcards
Multiple * Usage
Multiple * Usage
Signup and view all the flashcards
Extended Unpacking Benefits
Extended Unpacking Benefits
Signup and view all the flashcards
Unpacking sets
Unpacking sets
Signup and view all the flashcards
Unpacking with * operator
Unpacking with * operator
Signup and view all the flashcards
Extended Unpacking
Extended Unpacking
Signup and view all the flashcards
Combining multiple dictionaries
Combining multiple dictionaries
Signup and view all the flashcards
** operator
** operator
Signup and view all the flashcards
Set immutability
Set immutability
Signup and view all the flashcards
Order in Sets
Order in Sets
Signup and view all the flashcards
Unpacking with the * operator(Sets)
Unpacking with the * operator(Sets)
Signup and view all the flashcards
Higher Order Functions
Higher Order Functions
Signup and view all the flashcards
Filter Function
Filter Function
Signup and view all the flashcards
Zip Function
Zip Function
Signup and view all the flashcards
List Comprehension
List Comprehension
Signup and view all the flashcards
List Comprehension (map)
List Comprehension (map)
Signup and view all the flashcards
List Comprehension (filter)
List Comprehension (filter)
Signup and view all the flashcards
Reducing Functions
Reducing Functions
Signup and view all the flashcards
Comprehension (map and filter)
Comprehension (map and filter)
Signup and view all the flashcards
Default Value Re-evaluation
Default Value Re-evaluation
Signup and view all the flashcards
Mutable Default Value Issue
Mutable Default Value Issue
Signup and view all the flashcards
Default Value Solution
Default Value Solution
Signup and view all the flashcards
Lambda Expression
Lambda Expression
Signup and view all the flashcards
Lambda Syntax
Lambda Syntax
Signup and view all the flashcards
Lambda Expression Example
Lambda Expression Example
Signup and view all the flashcards
Lambda Expression Use Cases
Lambda Expression Use Cases
Signup and view all the flashcards
Anonymous Function
Anonymous Function
Signup and view all the flashcards
Nested Functions
Nested Functions
Signup and view all the flashcards
Non local Scope
Non local Scope
Signup and view all the flashcards
What happens if a variable is not found in a nested function's local scope?
What happens if a variable is not found in a nested function's local scope?
Signup and view all the flashcards
Enclosing Scope
Enclosing Scope
Signup and view all the flashcards
Global Scope
Global Scope
Signup and view all the flashcards
Local Scope
Local Scope
Signup and view all the flashcards
Built-in Scope
Built-in Scope
Signup and view all the flashcards
Name Resolution in Python
Name Resolution in Python
Signup and view all the flashcards
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.