Python Programming Questions and Answers (2, 6, and 10 Marks)
--- 2-MARKS QUESTIONS ---
1. Example of a membership operator:
x = [1, 2, 3]
print(2 in x) # Output: True
2. Example of a complex number in Python:
z = 3 + 4j
print(type(z)) # Output: <class 'complex'>
7. Two applications of Python:
- Web development (e.g., using Django or Flask)
- Data science and machine learning (e.g., using Pandas, NumPy, TensorFlow)
14. Define a variable in Python with an example:
name = "Alice"
print(name)
--- 6-MARKS QUESTIONS ---
3. Arithmetic and relational operators in Python:
Arithmetic: + - * / % ** //
a = 10; b = 3
print(a + b, a - b, a * b, a / b, a % b, a ** b, a // b)
Relational: > < == != >= <=
print(a > b, a < b, a == b, a != b, a >= b, a <= b)
4. Python data types and conversions:
Implicit: a = 10; b = 2.5; c = a + b # float
Explicit: x = int(5.9); y = str(10)
6. Identity, membership, and logical operators:
Identity: a = b = [1, 2]; print(a is b) # True
Membership: print(1 in [1, 2, 3]) # True
Logical: x = 10; print(x > 5 and x < 20) # True
8. Anaconda: A Python distribution with data science tools. Simplifies package management and
deployment. Used in machine learning and analysis.
13. Differences between Spyder and other IDEs:
- Tailored for data science with variable explorer.
- Supports inline plotting unlike IDLE.
16. Data types in Python:
int: x = 5
float: y = 3.14
str: name = "Alice"
list: numbers = [1, 2, 3]
tuple: t = (1, 2)
dict: d = {"a": 1}
bool: flag = True
--- 10-MARKS QUESTIONS ---
5. Python's applications in various fields:
- Web Development: Flask, Django (Instagram backend)
- Data Science: Pandas, NumPy
- AI/ML: TensorFlow, Scikit-learn
- Automation: Scripting with os
- Game Dev: Pygame
- Cybersecurity: Scapy
- IoT: Raspberry Pi
9. Compare number types:
int: 5 - Counting
float: 2.75 - Scientific calc
fraction: Fraction(3, 4) - Precision math
complex: 3+4j - Engineering
10. Python's key features:
- Easy syntax
- Interpreted & interactive
- Portable
- Libraries: NumPy, Pandas
- Open-source
- OOP and procedural support
11. Installation of Python, Anaconda, Spyder:
Python: Download from python.org, install, verify via terminal.
Anaconda: Download from anaconda.com, install (includes Python + Spyder).
Spyder: Open via Anaconda Navigator or run 'spyder' in terminal.
12. Importance of variables and data types:
Variables store data; types define operations.
Example:
name = "Alice"; age = 25; height = 5.6; is_student = True
print(name, age * 2, height + 0.4, not is_student)
15. What is Python:
High-level, interpreted language.
Features: Dynamic typing, OOP, community support, rapid development.
17. Basic operators in Python:
Arithmetic, Relational, Assignment, Logical, Membership, Identity, Bitwise.
Example:
a = 5; b = 3
print(a + b, a > b, a & b, a is b)
18. Numerical types and usage:
int: books = 120
float: price = 19.99
complex: z = 5 + 2j
fraction: from fractions import Fraction; f = Fraction(1, 3) + Fraction(1, 6)
print(f) # 1/2