Data Types, Variables and Expressions — TN Class 11 CS Exercise Solutions
Complete exercise solutions for the Tamil Nadu Board Class 11 Computer Science chapter Data Types, Variables and Expressions. Covers Python data types, variables, operators, expressions, and type conversions with working code.
How to Use This Page
1. Attempt each exercise on your own first — write Python code before looking at solutions. 2. Run every code example in IDLE or any Python editor to see the output. 3. Compare your output — if it differs, trace through the code step by step. 4. Understand the concept behind each answer, not just the syntax. 5. Practice variations — change values and operators to reinforce understanding.
Key Concepts (Quick Recap)
| Concept | Details |
|---|
| Data Type | Classification of data — determines what operations can be performed |
int | Whole numbers (e.g., 10, -5, 0) |
float | Decimal numbers (e.g., 3.14, -2.5, 1.0) |
complex | Complex numbers (e.g., 3+4j) |
str | String — sequence of characters in quotes (e.g., "Hello") |
bool | Boolean — True or False |
| Variable | Named storage location that holds a value |
| Dynamic Typing | Python automatically determines variable type; type can change |
| Expression | Combination of values, variables, and operators that evaluates to a result |
| Type Conversion | Converting one data type to another using int(), float(), str(), bool() |
| Implicit Conversion | Python automatically converts types (e.g., int + float = float) |
| Explicit Conversion | Programmer manually converts using functions (e.g., int("5")) |
Python Operators:
| Category | Operators | Example |
|---|
| Arithmetic | + - / // % * | 5 + 3 = 8 |
| Relational | == != < > <= >= | 5 > 3 is True |
| Logical | and or not | True and False is False |
| Assignment | = += -= *= /= //= | x += 5 |
| Membership | in not in | 'a' in 'abc' is True |
| Identity | is is not | x is y |
Operator Precedence (highest to lowest):
| Priority | Operator |
|---|
| 1 (highest) | () — Parentheses |
| 2 | ** — Exponentiation |
| 3 | +x, -x — Unary plus/minus |
| 4 | * / // % — Multiplication, Division, Floor Division, Modulo |
| 5 | + - — Addition, Subtraction |
| 6 | == != < > <= >= — Comparison |
| 7 | not — Logical NOT |
| 8 | and — Logical AND |
| 9 (lowest) | or — Logical OR |
Code Examples:
# Data types
x = 10 # int
y = 3.14 # float
z = 3 + 4j # complex
name = "Python" # str
flag = True # bool
# Check type
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
print(type(name)) # <class 'str'>
# Dynamic typing
a = 5 # a is int
a = "hello" # a is now str — type changes dynamically
# Type conversion
x = int(3.9) # x = 3 (truncates, does not round)
y = float(5) # y = 5.0
z = str(100) # z = "100"
w = bool(0) # w = False (0 is falsy)
w2 = bool(1) # w2 = True (non-zero is truthy)
# Arithmetic expressions
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.333... (true division)
print(10 // 3) # 3 (floor division)
print(10 % 3) # 1 (modulo/remainder)
print(10 ** 3) # 1000 (exponentiation)
I. Choose the Best Answer (MCQs)
Q1. Which of the following is an immutable data type in Python? (a) list (b) dict (c) tuple (d) set
Answer(c) tuple
Tuples are immutable — their elements cannot be changed after creation. Lists, dictionaries, and sets are mutable.
Q2. What is the output of type(3.14) in Python? (a) <class 'int'> (b) <class 'float'> (c) <class 'str'> (d) <class 'bool'>
Answer(b) <class 'float'>
3.14 is a decimal number, so its data type is float.
Q3. The expression 10 // 3 evaluates to: (a) 3.33 (b) 3 (c) 4 (d) 1
Answer(b) 3
The // operator performs floor division, which returns the integer part of the quotient. 10 divided by 3 is 3.33..., and the floor is 3.
Q4. What is the result of int("5") + 3? (a) "53" (b) 8 (c) Error (d) 53
Answer(b) 8
int("5") converts the string "5" to the integer 5. Then 5 + 3 = 8.
Q5. Which operator has the highest precedence in Python? (a) + (b) (c) * (d) /
Answer(c) **
Exponentiation () has higher precedence than all other arithmetic operators. Only parentheses () have higher precedence than .
Q6. The result of bool(0) is: (a) True (b) False (c) 0 (d) Error
Answer(b) False
In Python, 0 is a falsy value. bool(0) returns False. Any non-zero number returns True.
Q7. What does 10 % 3 return? (a) 3 (b) 3.33 (c) 1 (d) 0
Answer(c) 1
The % (modulo) operator returns the remainder. 10 divided by 3 gives quotient 3 and remainder 1.
II. Fill in the Blanks
1. The data type that stores whole numbers is int (integer). 2. In Python, the operator is used for exponentiation (raising to a power). 3. The function used to find the data type of a variable is type(). 4. The result of 5 / 2 in Python 3 is 2.5 (true division returns float). 5. The result of 5 // 2 in Python 3 is 2 (floor division returns integer quotient). 6. Python determines the type of a variable automatically based on the value assigned — this is called dynamic typing. 7. The process of converting one data type to another is called type conversion (or type casting). 8. int(3.7) returns 3 (truncates the decimal part). 9. float(5) returns 5.0. 10. str(100) returns "100"** (string). 11. bool("") returns
AnswerFalse (empty string is falsy). 12. The modulo operator % returns the remainder of a division. 13. In operator precedence, parentheses () have the highest priority. 14. The expression 2 + 3 4 evaluates to 14 (multiplication before addition: 34=12, 2+12=14).
III. True or False (with corrections for false statements)
Q1. In Python, variable types must be declared before assigning a value.
Answer
False. Python uses dynamic typing — you do not need to declare variable types. The type is determined automatically based on the value assigned.
Q2. The // operator returns a float result.
Answer
False. The // (floor division) operator returns an integer when both operands are integers. It returns a float only if at least one operand is a float. For example, 10 // 3 returns 3 (int), but 10.0 // 3 returns 3.0 (float).
Q3. bool(0) returns False in Python.
Answer
True. Zero (0) is a falsy value in Python. bool(0) returns False, while bool(any non-zero number) returns True.
Q4. String multiplication like "Ha" * 3 is valid in Python.
Answer
True. The operator with a string and an integer repeats the string. "Ha" 3 produces "HaHaHa".
Q5. The expression 2 3 2 evaluates to 512.
Answer
True. Exponentiation is right-associative in Python. 2 3 2 is evaluated as 2 (3 2) = 2 ** 9 = 512.
Q6. int("hello") will return 0.
Answer
False. int("hello") raises a ValueError because "hello" cannot be converted to an integer. Only strings containing numeric characters (like "5" or "-10") can be converted with int().
Q7. In Python, a variable can change its type during program execution.
Answer
True. Python supports dynamic typing. A variable can hold an integer at one point and a string later:
x = 10 # x is int
x = "hello" # x is now str
IV. Match the Following
| Column A | Column B |
|---|
1. int | (c) Whole numbers |
2. float | (e) Decimal numbers |
3. str | (a) Sequence of characters |
4. bool | (f) True or False |
5. complex | (d) Real + imaginary part |
6. type() | (b) Returns data type |
Answer1–c, 2–e, 3–a, 4–f, 5–d, 6–b
| Column A | Column B |
|---|
1. + | (a) Addition |
2. // | (e) Floor division |
3. % | (d) Modulo |
4. ** | (f) Exponentiation |
5. / | (c) True division |
6. * | (b) Multiplication |
Answer1–a, 2–e, 3–d, 4–f, 5–c, 6–b
V. Answer Briefly (2-Mark Questions)
Q1. What are data types? List the basic data types in Python.
Answer
Data types classify data and determine what operations can be performed on it. Python's basic data types are:
| Data Type | Keyword | Example |
|---|
| Integer | int | 10, -5, 0 |
| Float | float | 3.14, -2.5 |
| Complex | complex | 3+4j |
| String | str | "Hello" |
| Boolean | bool | True, False |
Q2. What is dynamic typing? Give an example.
Answer
Dynamic typing means Python determines the variable type automatically based on the assigned value, and the type can change during execution. No type declaration is needed.
x = 10 # x is int
print(type(x)) # <class 'int'>
x = 3.14 # x is now float
print(type(x)) # <class 'float'>
x = "Hello" # x is now str
print(type(x)) # <class 'str'>
Q3. Differentiate between / and // operators.
Answer
| Feature | / (True Division) | // (Floor Division) |
|---|
| Result type | Always float | Int if both operands are int |
| Example | 7 / 2 = 3.5 | 7 // 2 = 3 |
| Meaning | Regular division | Quotient without decimal part |
| Negative example | -7 / 2 = -3.5 | -7 // 2 = -4 (floors toward negative) |
Q4. What is type conversion? Give two examples.
Answer
Type conversion is the process of converting a value from one data type to another. It can be implicit (automatic) or explicit (manual).
# Explicit conversion (manual)
x = int("25") # String to int: x = 25
y = float(10) # Int to float: y = 10.0
# Implicit conversion (automatic)
z = 5 + 2.5 # int + float → Python converts 5 to 5.0
print(type(z)) # <class 'float'>, result is 7.5
VI. Answer in Detail (5-Mark Questions)
Q1. Explain all arithmetic operators in Python with examples and operator precedence.
Answer
Python provides the following arithmetic operators:
| Operator | Name | Example | Result |
|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 3 * 7 | 21 |
/ | True Division | 10 / 3 | 3.3333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulo (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
# Arithmetic operator examples
a = 15
b = 4
print(a + b) # 19 (Addition)
print(a - b) # 11 (Subtraction)
print(a * b) # 60 (Multiplication)
print(a / b) # 3.75 (True Division - always returns float)
print(a // b) # 3 (Floor Division - integer quotient)
print(a % b) # 3 (Modulo - remainder)
print(a ** b) # 50625 (Exponentiation - 15 to the power 4)
Operator Precedence (highest to lowest):
1. Parentheses () — evaluated first, overrides all other precedence 2. Exponentiation — evaluated next 3. Unary +x, -x — unary plus and minus 4. / // % — multiplication, division, floor division, modulo (left to right) 5. + -* — addition and subtraction (left to right)
Precedence examples:
# Without parentheses
print(2 + 3 * 4) # 14 (3*4=12, then 2+12=14)
# With parentheses
print((2 + 3) * 4) # 20 (2+3=5, then 5*4=20)
# Exponentiation first
print(2 + 3 ** 2) # 11 (3**2=9, then 2+9=11)
# Right-associative exponentiation
print(2 ** 3 ** 2) # 512 (3**2=9, then 2**9=512)
# Same precedence, left to right
print(10 / 2 * 5) # 25.0 (10/2=5.0, then 5.0*5=25.0)
Floor division with negative numbers:
print(-7 // 2) # -4 (floors toward negative infinity, not truncation)
print(7 // -2) # -4 (same principle)
Q2. Explain type conversion in Python with examples. Differentiate between implicit and explicit conversion.
Answer
Type conversion is the process of converting a value from one data type to another. Python supports two types of conversion:
1. Implicit Type Conversion (Automatic): Python automatically converts data types when mixing different types in an expression. It always converts to the higher/wider type to avoid data loss.
# int + float → float (Python converts int to float automatically)
a = 5 # int
b = 2.5 # float
c = a + b # Python converts 5 to 5.0, then adds
print(c) # 7.5
print(type(c)) # <class 'float'>
# int + complex → complex
x = 10 # int
y = 3 + 2j # complex
z = x + y # Python converts 10 to 10+0j
print(z) # (13+2j)
print(type(z)) # <class 'complex'>
Conversion hierarchy (lower to higher): int → float → complex
Python always converts to the higher type. You cannot go from complex to float or int implicitly.
2. Explicit Type Conversion (Manual / Type Casting): The programmer manually converts types using built-in functions: int(), float(), str(), bool(), complex().
# int() — converts to integer
print(int(3.9)) # 3 (truncates decimal part, does NOT round)
print(int("25")) # 25 (string to int)
print(int(True)) # 1
print(int(False)) # 0
# float() — converts to float
print(float(10)) # 10.0
print(float("3.14")) # 3.14
print(float(True)) # 1.0
# str() — converts to string
print(str(100)) # "100"
print(str(3.14)) # "3.14"
print(str(True)) # "True"
# bool() — converts to boolean
print(bool(0)) # False (0 is falsy)
print(bool(1)) # True (non-zero is truthy)
print(bool("")) # False (empty string is falsy)
print(bool("Hello")) # True (non-empty string is truthy)
print(bool([])) # False (empty list is falsy)
Common errors in type conversion:
# Cannot convert non-numeric string to int/float
print(int("hello")) # ValueError
print(int("3.14")) # ValueError (use float() first: int(float("3.14")))
# Cannot convert complex to int or float
print(int(3+4j)) # TypeError
Proper conversion chain:
# String "3.14" → float → int
s = "3.14"
f = float(s) # 3.14
i = int(f) # 3
print(i) # 3
Q3. Explain variables and expressions in Python. Write a Python program to evaluate arithmetic expressions.
Answer
Variables: A variable is a named memory location that stores a value. In Python, variables:
- Do not need to be declared with a type (dynamic typing)
- Can change type during execution
- Must follow naming rules
Variable Naming Rules: 1. Must start with a letter or underscore (not a number) 2. Can contain letters, digits, and underscores 3. Cannot contain spaces or special characters 4. Cannot be a Python keyword (if, for, while, etc.) 5. Case-sensitive (age and Age are different variables)
# Valid variable names
student_name = "Ravi"
_age = 16
marks123 = 95
# Invalid variable names
# 2name = "Error" # starts with number
# my name = "Error" # contains space
# class = "Error" # keyword
Expressions: An expression is a combination of values, variables, operators, and function calls that evaluates to a single value.
Types of expressions:
| Type | Example | Result |
|---|
| Arithmetic | 5 + 3 * 2 | 11 |
| Relational | 5 > 3 | True |
| Logical | True and False | False |
| String | "Hello" + " " + "World" | "Hello World" |
| Compound | (a + b) * c > d and e != f | True/False |
Python Program — Expression Evaluator:
# Program to evaluate various arithmetic expressions
a = int(input("Enter value of a: "))
b = int(input("Enter value of b: "))
c = int(input("Enter value of c: "))
# Arithmetic expressions
print("a + b =", a + b) # Addition
print("a - b =", a - b) # Subtraction
print("a * b =", a * b) # Multiplication
print("a / b =", a / b) # True Division
print("a // b =", a // b) # Floor Division
print("a % b =", a % b) # Modulo
print("a ** b =", a ** b) # Exponentiation
# Compound expressions (using precedence)
print("a + b * c =", a + b * c) # Multiplication first
print("(a + b) * c =", (a + b) * c) # Parentheses first
print("a ** b + c =", a ** b + c) # Exponentiation first
print("a + b > c and b != 0 =", a + b > c and b != 0) # Relational + Logical
# Type of result
x = a / b
print("Type of a/b:", type(x)) # <class 'float'>
y = a // b
print("Type of a//b:", type(y)) # <class 'int'> (if both are int)
Output:
Enter value of a: 10
Enter value of b: 3
Enter value of c: 5
a + b = 13
a - b = 7
a * b = 30
a / b = 3.3333333333333335
a // b = 3
a % b = 1
a ** b = 1000
a + b * c = 25
(a + b) * c = 65
a ** b + c = 1005
a + b > c and b != 0 = True
Type of a/b: <class 'float'>
Type of a//b: <class 'int'>
Common Mistakes to Avoid
1. Forgetting to convert input() to a number — input() always returns a string. Use int() or float() before arithmetic.
2. Confusing / and // — 7/2 is 3.5 (true division), 7//2 is 3 (floor division). They are different operators.
3. Wrong operator precedence — 2 + 3 4 is 14, not 20. Multiplication happens before addition. Use parentheses to clarify: (2 + 3) 4 is 20.
4. Assuming int() rounds — int(3.9) is 3, not 4. int() truncates (removes the decimal part), it does not round.
5. Trying to convert invalid strings — int("hello") raises a ValueError. Only numeric strings can be converted: int("5") works, int("5.0") does not (use int(float("5.0")) instead).
6. Confusing = and == — = is assignment, == is comparison. x = 5 assigns; x == 5 checks equality.
7. Forgetting floor division direction for negatives — -7 // 2 is -4, not -3. Floor division rounds toward negative infinity.
Exam Checklist
Before your exam, make sure you can:
- [ ] List and explain Python's basic data types (int, float, str, bool, complex)
- [ ] Use
type() to identify data types - [ ] Explain dynamic typing with examples
- [ ] Differentiate between
/ (true division) and // (floor division) - [ ] List all arithmetic operators with examples
- [ ] Apply operator precedence rules correctly
- [ ] Perform explicit type conversion using int(), float(), str(), bool()
- [ ] Explain implicit type conversion with the hierarchy (int → float → complex)
- [ ] Write valid variable names and identify invalid ones
- [ ] Evaluate compound arithmetic expressions step by step
- [ ] Identify truthy and falsy values for bool() conversion
- [ ] Explain the difference between
= (assignment) and == (comparison)