What does dynamically typed mean in Python?
Python is dynamically typed: a name has no permanently declared type and may later refer to a value of a different type.
Study 2 Variables and Data Types with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What does dynamically typed mean in Python?
Python is dynamically typed: a name has no permanently declared type and may later refer to a value of a different type.
What rules apply to Python names?
A Python name may contain letters, digits, and underscores, but it cannot begin with a digit. Names are case-sensitive, and keywords cannot be ordinary names.
How do `=` and `==` differ?
The operator `=` assigns or binds a value to a name; it does not test mathematical equality. The comparison operator `==` tests whether two values are equal.
What happens when a name is rebound?
Assignment can rebind a name. After `temperature = 18` followed by `temperature = 21`, the name `temperature` refers to `21`.
Why can `x, y = y, x` swap values?
In `x, y = y, x`, Python evaluates the right-hand side before assignments take effect, so the two values are swapped without a temporary variable.
How can you inspect an object's type?
The `type()` function inspects an object and reports its type, such as `int`, `float`, `str`, or `bool`.
What do `/`, `//`, `%`, and `**` do?
`/` performs ordinary division, `//` performs floor division, `%` returns the remainder, and `**` raises a value to a power.
What type does `input()` return?
`input()` returns a string. Convert it before numeric operations, such as with `age = int(input("Enter your age: "))`.
How does `int()` convert a float?
Converting a float to `int` discards the fractional part rather than rounding: `int(7.9)` is `7` and `int(-7.9)` is `-7`.
What does string immutability mean?
Strings are immutable Unicode sequences, so their contents cannot be changed in place. Create a new string instead, such as by concatenating text.
How does Python index strings?
Python strings use zero-based indexing: `word[0]` returns the first character. Negative indexing counts from the end, so `word[-1]` returns the last character.
What is an f-string used for?
An f-string inserts expressions directly into text, as in `f"You ordered {quantity} {item}s."`. It provides readable formatted output.