2 - Python Language Basics, IPython, and Jupyter Notebooks

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

16 Terms

1
New cards

What is a function in Python?

A reusable block of code that performs a specific task, defined with the def keyword. It can take arguments and return values.

2
New cards

How do you call a method on an object?

Using dot notation: obj.method_name(arguments).

3
New cards

What is the difference between an attribute and a method?

An attribute is data stored within an object (e.g., obj.name), while a method is a function that belongs to the object (e.g., obj.calculate()).

4
New cards

What does the return statement do in a function?

It exits the function and optionally passes a value back to the caller. If omitted, the function returns None.

5
New cards

How do you check if an object has a specific attribute?

Use hasattr(obj, 'attribute_name'), which returns True or False.

6
New cards

What is a magic method in Python?

A special method with double underscores (e.g., __init__) that defines behavior for built-in operations like initialization or arithmetic.

7
New cards

What is the purpose of __init__?

It's the constructor method called when an object is created, used to initialize its attributes.

8
New cards

What is 'duck typing'?

Determining an object's type by its behavior (methods/attributes) rather than its explicit class. Example: 'If it quacks like a duck, it's a duck.'

9
New cards

How do you dynamically access an attribute using a string name?

Use getattr(obj, 'attribute_name').

10
New cards

What is a ternary expression?

A concise if-else in one line: value = true_expr if condition else false_expr.

11
New cards

What is the self parameter in a method?

A reference to the current instance of the class, automatically passed as the first argument to instance methods.

12
New cards

How do you list all methods/attributes of an object?

Use dir(obj) or tab-completion in IPython/Jupyter.

13
New cards

What is a docstring?

A string literal (enclosed in triple quotes) at the start of a function/class/module that documents its purpose. Accessed via obj.__doc__.

14
New cards

What's the difference between mutable and immutable objects?

Mutable objects (e.g., lists) can be modified after creation; immutable ones (e.g., strings) cannot.

15
New cards

What does pass do?

A no-op placeholder used where syntax requires code but no action is needed (e.g., empty functions).

16
New cards

How do you handle variable-length arguments in a function?

Use args for positional arguments and *kwargs for keyword arguments.