0% found this document useful (0 votes)
16 views2 pages

Python SQL Practice Questions Guide

The document contains practice questions for Python and SQL, divided into five sections: Python Pandas, Python Matplotlib, SQL Queries, Python File Handling, and Python Data Analysis. Each section includes tasks such as creating DataFrames, plotting charts, executing SQL commands, and handling files. The goal is to enhance programming skills through practical exercises.

Uploaded by

krish125461
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)
16 views2 pages

Python SQL Practice Questions Guide

The document contains practice questions for Python and SQL, divided into five sections: Python Pandas, Python Matplotlib, SQL Queries, Python File Handling, and Python Data Analysis. Each section includes tasks such as creating DataFrames, plotting charts, executing SQL commands, and handling files. The goal is to enhance programming skills through practical exercises.

Uploaded by

krish125461
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 and SQL Practice Questions

1. Python Pandas Questions

a. Create a DataFrame with the following data and display it.

Data: {'Name': ['Aman', 'Riya', 'Sahil', 'Tina'], 'Age': [23, 21, 22, 20], 'Score': [90, 85, 88, 92]}

b. Add a column named "Passed" with values based on Score > 80.

c. Display rows where age is greater than 21.

d. Sort the DataFrame based on Score in descending order.

2. Python Matplotlib Questions

a. Plot a pie chart for the following data:

Subjects = ['Math', 'Science', 'English', 'History']

Marks = [85, 90, 78, 88]

b. Plot a scatter plot for the following data:

X = [1, 2, 3, 4, 5]

Y = [10, 20, 25, 30, 35]

3. SQL Queries

Given the table "Books":

BookID | Title | Author | Price

1 | Python Basics | A. Kumar | 300

2 | SQL Mastery | B. Singh | 400

3 | Data Science | C. Gupta | 500

a. Create the "Books" table with appropriate datatypes.

b. Insert the given data into the table.


c. Write a query to display books with price greater than 350.

d. Write a query to update the price of the book "Python Basics" to 350.

e. Write a query to delete books with price less than 400.

4. Python File Handling

a. Write a program to create a text file "[Link]" and write "Hello, this is a sample file" into it.

b. Append the text "This is an additional line." to the same file.

c. Read and display the contents of the file.

5. Python Data Analysis

a. Using Pandas, create a DataFrame from a CSV file and display the first 5 rows.

b. Write a program to group the DataFrame data by a specific column and display the mean of

another column.

c. Save the grouped data into a new CSV file.

---

Write and execute these programs/queries to strengthen your skills!

Common questions

Powered by AI

A scatter plot using datasets, like X=[1,2,3,4,5] and Y=[10,20,25,30,35], graphically displays potential relationships or correlations. It aids in identifying patterns such as linear trends or clustering. When interpreting scatter plots, consider axis scales, outliers, and the densification of plotted points to avoid overgeneralizing from visual results .

Best practices for maintaining data consistency during multiple SQL operations include using transactions to ensure atomicity, applying constraints for data integrity, and maintaining proper indexing. Regular audits and backups, combined with development of rollback mechanisms, further protect against inadvertent data loss or corruption .

Using Pandas to create and display DataFrames from CSV files streamlines data importation and visualization, providing structured and easily readable formats. This optimizes the analysis process by enabling rapid data exploration, cleaning, and transformation, essential for making robust, data-driven decisions .

Grouping data in a Pandas DataFrame by a column (e.g., 'Category') and computing the mean of another column (e.g., 'Value') provides summarized insights, facilitating trend analysis across groups. It highlights key disparities or similarities between categories, aiding in strategic decision making and pattern recognition .

Exporting grouped DataFrame data to a CSV file ensures data persistence for future reference and analysis. It allows for seamless data sharing and facilitates use in external software tools for additional computation or visualization. However, performance impacts and storage needs must be considered, especially with large datasets .

Updating SQL table entries, such as changing the price of a book like 'Python Basics' to 350, affects stored data integrity and can influence financial reporting or sales analysis. It may impact revenue calculations and alter derived statistics. Care should be taken to ensure accuracy and adherence to business rules when performing such updates to prevent discrepancies .

Deleting entries from an SQL database, such as removing books with prices less than 400, can clear out lesser valuable data, potentially improving query efficiency. However, it risks data integrity issues by permanently losing relevant records. Such actions should be validated to avoid unintended data loss, preserving necessary information for historical analyses and comparisons .

To append data to a text file in Python, first open the file in append ('a') mode, then write the additional data. For instance, after writing 'Hello, this is a sample file', opening 'sample.txt' with 'a' mode and appending 'This is an additional line.' places this text after existing content. Caveats include potential issues with file pointers and inadvertent overwriting if file modes are improperly set .

A pie chart created using Matplotlib can visually represent distribution across different categories. In the context of subject performance (e.g., Math, Science, English, History), it shows the portion of marks secured out of the total for each subject, making it easy to compare relative performance among the subjects. Major portions indicate subjects where higher marks were obtained, aiding in overall performance analysis .

To create a new Pandas DataFrame with an additional column, you can start with a DataFrame containing specified data. Use a conditional operation to add a 'Passed' column based on whether the 'Score' values exceed 80. Sorting the DataFrame by 'Score' in descending order reorganizes rows such that entries with higher scores appear first, which helps in quickly identifying top performers .

You might also like