搜尋此網誌

2026年3月16日星期一

Quiz

An object is any value that lives in memory in Python (numbers, strings, lists, functions, classes, user‑defined things, etc.).

An instance is an object viewed in relation to its class/type, i.e. “this object is an instance of that class.”

In Python, a constructor is the special method that runs automatically when you create an object from a class, usually used to set up initial attributes.

class Person:

    def __init__(self, name, age):  # constructor

        self.name = name

        self.age = age

p = Person("Alice", 30)

Assuming a Dog class exists,

type(Dog('Rover'))

Python evaluates it like this:

- Dog('Rover') creates a new instance (object) of the Dog class, with 'Rover' passed to its constructor.

- type(...) then returns the type of that object.

So the result is the class itself


myList = [[1,2], ['apple'], [3,4,5]]

listLen = len(myList)

- myList is a list containing three sublists:

- [1, 2]

- ['apple']

- [3, 4, 5]

- When you call len(myList), Python counts the top-level elements of myList, not the items inside each sublist.


false = False
- False (capitalized) is a built-in Boolean constant.
- false (lowercase) is just a variable name you created. Python is case-sensitive, so false and False are completely different things


Tuples in Python are immutable — once created, you cannot add, remove, or change their elements.


In Python, while True: means “keep looping forever” because the condition after while is always True.

Here’s the breakdown:

- while is a loop that repeats as long as its condition is true.

- True is a Boolean constant that never changes.

- So while True: creates an infinite loop — the code inside will run endlessly until you manually stop it (like pressing Ctrl + C in a terminal) or break out of it with a break statement.


Microsoft Copilot
Perplexity.AI

沒有留言:

發佈留言