←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Python Data Types, Variables, Numbers & Operators

Study material and explanations for Unit 1

Python Data Types, Variables, Numbers & Operators

1. Variables in Python

A Variable is a named label (reference) attached to an object stored in memory.

  • No Type Declaration Needed (Dynamic Typing): In Python, a variable is created the moment you first assign a value to it using the = operator!
  • Multiple Assignment in One Line: Python allows you to assign values to multiple variables in a single statement:
# 1. Standard dynamic variable assignment
roll_no = 101
student_name = "Aarav"
cgpa = 8.95

# 2. Assigning different values to multiple variables in one line
a, b, c = 10, 20, "DAVV"

# 3. Assigning the SAME value to multiple variables
x = y = z = 0

# 4. Checking the data type of any variable using type()
print(type(roll_no))      # Output: <class 'int'>
print(type(student_name)) # Output: <class 'str'>

Rules for Naming Python Identifiers (Variables):

  1. Can contain letters (a–z, A–Z), digits (0–9), and underscores (_).
  2. Must start with a letter or an underscore (_)—never with a digit (1st_num is invalid; num_1 is valid).
  3. Case-Sensitive: marks, Marks, and MARKS are three completely different variables.
  4. Cannot use reserved Python Keywords (if, else, for, while, def, class, True, False, None, and, or, not).

2. Built-in Data Types in Python

Since everything in Python is an object, data types are actually classes, and variables are instances (objects) of these classes. Python’s built-in data types are grouped into 6 standard categories:

Figure 1.5: Classification of Built-in Data Types in Python
1. Numeric Types
int (Integers), float (Decimals), complex (3 + 4j)
2. Sequence Types
str (String), list ([1, 2]), tuple ((1, 2)), range
3. Mapping Type
dict (Dictionary of Key-Value pairs: {"id": 101})
4. Set Types
set (Mutable unique items: {1, 2, 3}) & frozenset
5. Boolean Type
bool — Holds only True or False (Note capital T and F!)
6. None Type
NoneType — Special keyword None representing absence of value

3. Numbers in Python (int, float, complex)

Your DAVV Unit 1 syllabus specifically highlights Numbers in Python. Python supports three distinct numeric types:

1. Integers (int)

  • Whole numbers (positive, negative, or zero) without any fractional/decimal part.
  • Unlimited Precision in Python! Unlike C or Java (where a 32-bit int overflows above 2 billion), an int in Python 3 has unlimited size—it can be 100 digits long, limited only by your computer’s RAM!
  • Also supports binary (0b1010), octal (0o12), and hexadecimal (0xFF) literals.

2. Floating-Point Numbers (float)

  • Real numbers containing a decimal point (e.g., pi = 3.14159, temp = -4.5) or written in scientific exponential notation (e or E for powers of 10, e.g., 2.5e3 = 2500.0).

3. Complex Numbers (complex)

  • Python has built-in support for Complex Numbers written in the form a + bj (or a + bJ), where:
    • a is the Real part (z.real)
    • b is the Imaginary part (z.imag), and j represents the square root of -1!
# Demonstrating all 3 Number Types in Python
a = 99999999999999999999999  # 1. int (Unlimited length!)
b = 75.50                    # 2. float
c = 3 + 4j                   # 3. complex number

print("Real Part     :", c.real)  # Output: 3.0
print("Imaginary Part:", c.imag)  # Output: 4.0

# Type Conversion (Type Casting) between Numbers
print(float(10))      # Output: 10.0
print(int(99.89))     # Output: 99
print(complex(5, 2))  # Output: (5+2j)

4. Operators in Python (All 7 Categories)

Operators are special symbols used to perform arithmetic, logical, or comparison operations on values and variables. Python divides operators into 7 categories:

1. Arithmetic Operators (Pay Special Attention to // and **!)

Operator Name Example (a = 10, b = 3) Output & Explanation
+ Addition 10 + 3 13
- Subtraction 10 - 3 7
* Multiplication 10 * 3 30
/ True Division 10 / 3 3.3333333333333335 (Always returns a float!)
// Floor (Integer) Division 10 // 3 3 (Truncates decimal part and rounds down to nearest integer!)
% Modulus (Remainder) 10 % 3 1 (Remainder of 10 divided by 3)
** Exponentiation (Power) 2 ** 3 8 (Calculates 2 raised to the power 3)

2. Relational (Comparison) Operators

Compare two values and return a Boolean (True or False):

  • == (Equal to), != (Not equal to), > (Greater than), < (Less than), >= (Greater than or equal to), <= (Less than or equal to).
  • Unique Python Feature: Python allows Chained Comparisons like 18 <= age <= 60!

3. Logical Operators (and, or, not)

Unlike C/Java (which use symbols &&, ||, !), Python uses plain English words:

  • and: Returns True if both conditions are true (x > 0 and x < 10).
  • or: Returns True if at least one condition is true.
  • not: Reverses the boolean state (not True gives False).

4. Assignment Operators

  • =, +=, -=, *=, /=, //=, %=, **= (e.g., x += 5 is shorthand for x = x + 5).
  • (Note: Python does not have ++ or -- increment/decrement operators! Use x += 1 instead).

5. Bitwise Operators

Operate bit-by-bit on binary representations of integers:

  • & (Bitwise AND), | (Bitwise OR), ^ (Bitwise XOR), ~ (Bitwise NOT), << (Left Shift), >> (Right Shift).

6. Membership Operators (in and not in) — Unique to Python!

Used to test whether a value or substring is present inside a sequence (str, list, tuple, set, dict):

  • in: Returns True if the element is found in the sequence.
  • not in: Returns True if the element is not found in the sequence.
cities = ["Indore", "Bhopal", "Ujjain"]
print("Indore" in cities)      # Output: True
print("Delhi" not in cities)   # Output: True
print("BCA" in "BCA DAVV")     # Output: True

7. Identity Operators (is and is not) — Unique to Python!

Used to check whether two variables point to the exact same object in memory (having the same memory address id(a) == id(b)), rather than just having equal values:

  • is: Returns True if both variables point to the same object in memory.
  • is not: Returns True if they point to different objects in memory.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1

print(list1 == list2)  # True  (Values inside both lists are equal)
print(list1 is list2)  # False (They are two separate list objects in RAM!)
print(list1 is list3)  # True  (Both point to the exact same list object!)

💡 Exam Preparation Tips for DAVV BCA

  • Guaranteed 2-mark / Viva Question: “What is the difference between / and // in Python?”
    • / (True Division) always returns a floating-point number (10 / 2 gives 5.0, 7 / 2 gives 3.5).
    • // (Floor Division) returns the integer quotient by rounding down (7 // 2 gives 3).
  • Always highlight Membership Operators (in, not in) and Identity Operators (is, is not) when asked an 8-mark question on Operators in Python!