- LPU MCA Semester 1
- CAB106
- Unit 1
CAB106 · Unit 1: Python and OOP introduction
Variables, data types and operators
15 practice questions
Key points
- Names are bound to objects and can be rebound to other types
- / gives a float, // floors, % takes the divisor's sign
- ** is right-associative and comparisons can chain
- == compares values, is compares identity
A variable is a name bound to an object; Python is dynamically typed, so the same name can later refer to a value of another type. Common built-in types are int, float, str, bool, list, tuple, dict, set and None. Valid names start with a letter or underscore, use letters, digits and underscores, and cannot be keywords.
| Operator | Meaning | Example |
|---|---|---|
/ | true division, always float | 7 / 2 is 3.5 |
// | floor division | -7 // 2 is -4 |
% | remainder | -7 % 2 is 1 |
** | power, right-associative | 2 ** 3 ** 2 is 512 |
== / is | equal value / same object | [1] == [1] True, is False |
Precedence runs from ** down through arithmetic, comparisons, not, and, or. Comparisons chain, so 1 < 3 > 2 means 1 < 3 and 3 > 2. Empty containers, zero and None are falsy. round uses round-half-to-even, and floats such as 0.1 + 0.2 are not exact.
Exam trap: / always returns a float and // floors toward negative infinity, so -7 // 2 is -4, not -3.
Try two questions
Which built-in Python type holds only True or False?
- bool
- bit
- logic
- binary
Show the answer
A. bool has exactly two values, True and False, and is a subclass of int.
Why not B: Python has no built-in bit type.
Why not C: Python has no built-in logic type.
Why not D: binary is not a built-in type name.
Which of these is a valid Python variable name?
- 2total
- total-2
- _total2
- class
Show the answer
C. Names may start with a letter or underscore, contain letters, digits and underscores, and must not be keywords.
Why not A: A name cannot start with a digit.
Why not B: The hyphen is the minus operator, not part of a name.
Why not D: class is a reserved keyword.