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):
- Can contain letters (
a–z,A–Z), digits (0–9), and underscores (_). - Must start with a letter or an underscore (
_)—never with a digit (1st_numis invalid;num_1is valid). - Case-Sensitive:
marks,Marks, andMARKSare three completely different variables. - 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:
int (Integers), float (Decimals), complex (3 + 4j)str (String), list ([1, 2]), tuple ((1, 2)), rangedict (Dictionary of Key-Value pairs: {"id": 101})set (Mutable unique items: {1, 2, 3}) & frozensetbool — Holds only True or False (Note capital T and F!)NoneType — Special keyword None representing absence of value3. 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
intoverflows above 2 billion), anintin 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 (eorEfor 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(ora + bJ), where:ais the Real part (z.real)bis the Imaginary part (z.imag), andjrepresents 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: ReturnsTrueif both conditions are true (x > 0 and x < 10).or: ReturnsTrueif at least one condition is true.not: Reverses the boolean state (not TruegivesFalse).
4. Assignment Operators
=,+=,-=,*=,/=,//=,%=,**=(e.g.,x += 5is shorthand forx = x + 5).- (Note: Python does not have
++or--increment/decrement operators! Usex += 1instead).
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: ReturnsTrueif the element is found in the sequence.not in: ReturnsTrueif 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: ReturnsTrueif both variables point to the same object in memory.is not: ReturnsTrueif 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 / 2gives5.0,7 / 2gives3.5).//(Floor Division) returns the integer quotient by rounding down (7 // 2gives3).
- Always highlight Membership Operators (
in,not in) and Identity Operators (is,is not) when asked an 8-mark question on Operators in Python!