1. LPU MCA Semester 1
  2. CAB106
  3. 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.

OperatorMeaningExample
/true division, always float7 / 2 is 3.5
//floor division-7 // 2 is -4
%remainder-7 % 2 is 1
**power, right-associative2 ** 3 ** 2 is 512
== / isequal 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

Question 1 · Tier 1, Recall

Which built-in Python type holds only True or False?

  1. bool
  2. bit
  3. logic
  4. 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.

Question 2 · Tier 2, Understand

Which of these is a valid Python variable name?

  1. 2total
  2. total-2
  3. _total2
  4. 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.