0% found this document useful (0 votes)
2 views4 pages

Python Solutions Final Clean

The document provides comprehensive answers to Python programming questions across different groups, including definitions, functions, and code examples. It covers topics such as Python identifiers, selection statements, list vs tuples, and database operations using SQLite3 and CSV operations with Pandas. Each section includes brief and comprehensive answers along with sample code and terminal outputs.

Uploaded by

Ashish Dahait
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Python Solutions Final Clean

The document provides comprehensive answers to Python programming questions across different groups, including definitions, functions, and code examples. It covers topics such as Python identifiers, selection statements, list vs tuples, and database operations using SQLite3 and CSV operations with Pandas. Each section includes brief and comprehensive answers along with sample code and terminal outputs.

Uploaded by

Ashish Dahait
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAMMING SOLUTIONS

Comprehensive Answers for Groups A, B, C, & D

Group “A” – Brief Answer Questions

1. Define a Python identifier.


A Python identifier is a name used to identify a variable, function, class, module, or other
object. It must start with a letter (a-z, A-Z) or underscore (_), followed by letters, digits, or
underscores, and is case-sensitive.

2. What is the purpose of the id() function in Python?


The id() function returns a unique integer identifier (memory address) for an object. It is
used to check whether two variables refer to the same object.

3. List two selection statements available in Python.


1. if statement (with elif and else)
2. match statement (Python 3.10+).

4. How do you denote a comment in Python?


Single-line: #
Multi-line: Triple quotes """ ... """.

6. Difference between "=" and 'is' operator?


= is for assignment. is checks object identity (same location in memory).

9. Purpose of 'with' statement in file handling?


It ensures that resources (like files) are properly closed automatically after the block
finishes, even if an exception occurs.

Group “B” – Short Answer Questions

11. Lists vs Tuples.

Feature List Tuple

Mutability Mutable (Can change) Immutable (Cannot change)


Performance Slower Faster

13. List Comprehension.


Concise way to create lists. Example:

squares = [x**2 for x in range(10)]


Group “D” – Comprehensive Answer Questions

21. Database Program (SQLite3)

This program connects to a database, creates an 'employees' table, inserts records, and
displays them.

import sqlite3

# Connection and Table Creation


conn = [Link]('[Link]')
cursor = [Link]()
[Link]('''CREATE TABLE IF NOT EXISTS employees
(id INTEGER PRIMARY KEY, name TEXT, salary REAL)''')

# Insertion
employees_data = [(1, 'Alice', 55000), (2, 'Bob', 62000), (3, 'Charlie', 48000)]
[Link]('INSERT OR REPLACE INTO employees VALUES (?, ?, ?)', employees
[Link]()

# Retrieval
[Link]('SELECT * FROM employees')
print("Employees Table:")
for row in [Link]():
print(f"ID: {row[0]}, Name: {row[1]}, Salary: {row[2]}")
[Link]()

TERMINAL OUTPUT:

Employees Table:
ID: 1, Name: Alice, Salary: 55000.0
ID: 2, Name: Bob, Salary: 62000.0
ID: 3, Name: Charlie, Salary: 48000.0

22. CSV Operations Program (Pandas)

This program utilizes Pandas to read a student CSV and display specific subsets of data.

import pandas as pd

# Reading CSV
df = pd.read_csv('[Link]')

# First 5 records
print("First 5 records:")
print([Link](5))

# Last 5 records
print("\nLast 5 records:")
print([Link](5))

TERMINAL OUTPUT:

First 5 records:
name age grade
0 John 20 A
1 Jane 21 B
2 Sam 22 A
3 Sora 23 C
4 Mike 20 B

Last 5 records:
name age grade
5 Lisa 21 A
6 Anna 22 B
7 Zack 23 C
8 Elena 24 A
9 Leo 25 B

You might also like