0% found this document useful (0 votes)
24 views8 pages

Untitled Document

The document contains a series of short and long answer questions related to Python programming, covering topics such as data types, variables, type casting, and operators. It also includes practice programs demonstrating various programming tasks, along with conceptual questions about Python's features and libraries. Additionally, it provides examples of code snippets for practical applications in Python.

Uploaded by

erjainsunil1976
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)
24 views8 pages

Untitled Document

The document contains a series of short and long answer questions related to Python programming, covering topics such as data types, variables, type casting, and operators. It also includes practice programs demonstrating various programming tasks, along with conceptual questions about Python's features and libraries. Additionally, it provides examples of code snippets for practical applications in Python.

Uploaded by

erjainsunil1976
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
You are on page 1/ 8

B.

Short Answer Questions


input() function accepts the value as a string only. How can you convert string to int?​
Answer: Use int() function.​
Example:​

user_input = input("Enter a number: ")


number = int(user_input)

1.​
2.​ What are variables? What are the rules of declaring variables in Python?​
Answer: Variables are names used to store data in memory.​
Rules:​

○​ Must start with a letter or an underscore.


○​ Cannot start with a digit.
○​ Can only contain alphanumeric characters and underscores.
○​ Keywords cannot be used as variable names.

What do you mean by type casting?​


Answer: Type casting refers to converting a value from one data type to another.​
Example:​

float_value = float(10)

3.​

"Python supports dynamic typing", True or False. Justify your answer.​


Answer: True. Python allows variable types to change at runtime.​
Example:​

x = 10 # Initially an integer
x = "Hello" # Now a string

4.​
5.​ Name any four features of Python language.​
Answer:​

○​ Easy to learn and use.


○​ Interpreted language.
○​ Supports multiple programming paradigms.
○​ Extensive libraries and frameworks.
6.​ Give examples for keywords.​
Answer: if, else, while, for, True, False, def.​

7.​ Expand CSV.​


Answer: Comma-Separated Values.​

How do you read data from a CSV file into a Pandas DataFrame?​
Answer: Use pandas.read_csv() method.​
Example:​

import pandas as pd
df = pd.read_csv("file.csv")

8.​

C. Long Answer Questions

1.​ Describe the data types supported by Python, providing relevant examples.​
Answer:​

○​ Integer: Whole numbers. Example: x = 10


○​ Float: Decimal numbers. Example: x = 3.14
○​ String: Text data. Example: x = "Hello"
○​ Boolean: True or False values. Example: x = True
○​ List: Ordered, mutable collection. Example: x = [1, 2, 3]
○​ Tuple: Ordered, immutable collection. Example: x = (1, 2, 3)
○​ Set: Unordered, unique collection. Example: x = {1, 2, 3}
○​ Dictionary: Key-value pairs. Example: x = {"key": "value"}
2.​ Define an operator and provide examples of different operators along with their
functions.​
Answer: Operators perform operations on variables and values.​

○​ Arithmetic: +, -, *, /. Example: x + y
○​ Relational: <, >, ==. Example: x > y
○​ Logical: and, or, not. Example: x > 5 and y < 10
○​ Assignment: =, +=. Example: x += 1
D. Practice Programs
Tipper Program​

bill = float(input("Enter the total bill: "))


tip_15 = bill * 0.15
tip_20 = bill * 0.20
print("15% Tip:", tip_15)
print("20% Tip:", tip_20)

1.​

Driving License Eligibility​

age = int(input("Enter your age: "))


if age >= 18:
print("Eligible for driving license.")
else:
print("Not eligible for driving license.")

2.​

Car Service Check​

kilometers = int(input("Enter the kilometer reading: "))


if kilometers >= 15000:
print("Car needs servicing.")
else:
print("No servicing required.")

3.​

First Ten Even Natural Numbers​

for i in range(2, 21, 2):


print(i)

4.​

Net Salary Calculation​

basic_salary = float(input("Enter basic salary: "))


hra = 0.3 * basic_salary
da = 0.2 * basic_salary
pf = 0.12 * basic_salary
net_salary = basic_salary + hra + da - pf
print("Net Salary:", net_salary)

5.​

Series from Array in Python​

import pandas as pd
import numpy as np
array = np.array([1, 2, 3, 4, 5])
series = pd.Series(array)
print(series)

6.​

Admission DataFrame Operations​

import pandas as pd

# a. Create a dataframe
data = {
"Name": ["Amit", "Ashu", "Abhinav", "Ravi", "Rashmi", "Ramesh", "Mohit", "Manavi", "Dhruv"],
"CLASS": [10, 9, 9, 10, 11, 10, 9, 10, 9],
"Gender": ["M", "F", "M", "M", "F", "M", "M", "F", "M"],
"Marks": [75, 95, 86, 57, 78, 72, 53, 47, 76],
}
df = pd.DataFrame(data)

# b. Display first 3 rows


print(df.head(3))

# c. Display details of Ravi


print(df[df["Name"] == "Ravi"])

# d. Total rows and columns


print("Rows and Columns:", df.shape)

# e. Display column "Gender"


print(df["Gender"])

7.​

Let me know if you need additional explanations!

The Python unit includes several exercises involving programming tasks. Below, I'll provide
answers and explanations for the questions in the unit:
Sample Program-1

Task: Display the string “National Animal-Tiger” on the screen.​


Code:

print("National Animal-Tiger")

Sample Program-2

Task: Write a program to calculate the area of a rectangle with length 50 and breadth 20.​
Code:

length = 50
breadth = 20
area = length * breadth
print("Area of the rectangle:", area)

Sample Program-3

Task: Read the name and marks of a student and display the total mark.​
Code:

name = input("Enter the student's name: ")


marks = float(input("Enter the student's marks: "))
print(f"Student Name: {name}, Total Marks: {marks}")

Sample Program-4

Task: Determine the choice of food according to menu options (Veg, Non-Veg).​
Code:

menu = input("Enter your menu choice (veg/nonveg): ").lower()


if menu == "veg":
print("You chose vegetarian food.")
elif menu == "nonveg":
print("You chose non-vegetarian food.")
else:
print("Invalid choice.")

Sample Program-5

Task: Determine the type of triangle based on side lengths.​


Code:

a = int(input("Enter the first side: "))


b = int(input("Enter the second side: "))
c = int(input("Enter the third side: "))

if a == b == c:
print("Equilateral Triangle")
elif a == b or b == c or a == c:
print("Isosceles Triangle")
else:
print("Scalene Triangle")

Sample Program-6

Task: Display even numbers and their squares between 100 and 110.​
Code:

for i in range(100, 111):


if i % 2 == 0:
print(f"{i} squared is {i**2}")

Sample Program-7

Task: Read a list, display each element, and its type.​


Code:

lst = [25, 15.6, "car", "XY"]


for item in lst:
print(f"Element: {item}, Type: {type(item)}")

Sample Program-8
Task: Split a string into words and display each word.​
Code:

text = input("Enter a sentence: ")


words = text.split()
for word in words:
print(word)

Sample Program-9

Task: Display values stored in a dictionary.​


Code:

my_dict = {"name": "John", "age": 25, "city": "New York"}


for key, value in my_dict.items():
print(f"{key}: {value}")

Sample Program-10

Task: Open a CSV file (students.csv) and display its details.​


Code:

import csv

with open("students.csv", "r") as file:


reader = csv.reader(file)
for row in reader:
print(row)

Conceptual Questions

1.​ What is Python?​


Python is a high-level, interpreted programming language known for its simplicity and
versatility. It supports multiple programming paradigms, such as procedural,
object-oriented, and functional programming.​

2.​ Explain Data Types.​


Python's standard data types include:​
○​ Integer (int)
○​ Floating Point (float)
○​ String (str)
○​ Boolean (bool)
○​ List (list)
○​ Tuple (tuple)
○​ Dictionary (dict)
○​ Set (set)
3.​ Difference between Mutable and Immutable Data Types:​

○​ Mutable: Can be changed after creation (e.g., List, Dictionary, Set).


○​ Immutable: Cannot be changed after creation (e.g., String, Tuple).
4.​ What are Libraries in Python?​
Libraries are collections of modules or functions that provide reusable code. Examples
include NumPy (numerical computing), Pandas (data manipulation), and Scikit-learn
(machine learning).​

This covers the solutions and explanations for the Python unit exercises in the handbook. If you
have any specific programs or doubts, let me know!

You might also like