What rules must a valid Python name follow?
A Python name must begin with a letter or underscore, may continue with letters, digits, or underscores, is case-sensitive, and cannot be a keyword.
Study 2 Variables, Types, and Expressions with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What rules must a valid Python name follow?
A Python name must begin with a letter or underscore, may continue with letters, digits, or underscores, is case-sensitive, and cannot be a keyword.
What does Python assignment do?
Assignment evaluates the expression on the right first, then binds its resulting object to the name on the left. The equals sign does not mean mathematical equality.
What does dynamically typed mean in Python?
Python is dynamically typed: a name has no permanently declared type; the object currently associated with it determines the type.
What do `//` and `%` do in Python?
The operator `//` performs floor division, while `%` produces the remainder. For example, `7 // 3` is `2` and `7 % 3` is `1`.
What is the difference between `=` and `==`?
`=` assigns a value to a name; `==` compares two values and produces a Boolean result.
What is short-circuit evaluation?
Python may skip the right-hand operand of `and` or `or` when the result is already determined. This behavior is called short-circuit evaluation.
What type does `input()` return?
`input()` always returns a string. Convert it with `int()` or `float()` before using the input in numeric calculations.
How does `int()` convert a float?
`int()` truncates a floating-point value toward zero; it does not round it. Thus, `int(3.9)` returns `3`.
How do f-strings insert values into text?
An f-string places expressions inside braces, such as `f"{name} was born in {year}."`, making interpolation readable and direct.
What happens when `other_numbers = numbers` aliases a list?
Assignment binds another name to the same list object; it does not copy the list. Mutating through either name is therefore visible through both.
How can you make a shallow list copy?
Use a slice such as `numbers[:]` or the `list()` constructor to create a separate shallow copy of a list.
Why can’t a Python string be changed in place?
A string is immutable, so operations such as `.upper()` create a new string. Rebinding the name does not modify the original string.