0% found this document useful (0 votes)
3 views3 pages

Python_QnA_Fixed

The document contains a series of Python programming questions and answers covering various topics such as global variables, mutable vs immutable types, list functions, random output, tuple manipulation, file handling, and CSV operations. Each question includes code examples and explanations of the output or functionality. Key concepts include modifying global variables, distinguishing between mutable and immutable data types, and handling files for specific data extraction tasks.

Uploaded by

Garish Thakur
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)
3 views3 pages

Python_QnA_Fixed

The document contains a series of Python programming questions and answers covering various topics such as global variables, mutable vs immutable types, list functions, random output, tuple manipulation, file handling, and CSV operations. Each question includes code examples and explanations of the output or functionality. Key concepts include modifying global variables, distinguishing between mutable and immutable data types, and handling files for specific data extraction tasks.

Uploaded by

Garish Thakur
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/ 3

Python Programming Questions and Answers

Q1. Output of the code

c = 10

def add():

global c

c=c+2

print(c, end='#')

add()

c = 15

print(c, end='%')

Output: 12#15%

Explanation:

- Global variable 'c' is modified in the function to 12.

- Then reassigned to 15 and printed.

Q2. Mutable vs Immutable

- Mutable: Can be changed (e.g., list, dict)

- Immutable: Cannot be changed (e.g., tuple, str)

From: (1,2), [1,2], {1:1,2:2}, '123'

- Mutable: [1,2], {1:1,2:2}

- Immutable: (1,2), '123'

Q3. List Functions

L1.count(4) - Count 4s in L1

L1.sort() - Sort L1

L1.extend(L2) - Add L2 to end of L1

L2.reverse() - Reverse L2

Q4. Random Output and Range


Python Programming Questions and Answers

import random

a = 'Wisdom'

b = random.randint(1,6)

for i in range(0,b,2):

print(a[i], end='#')

b ranges from 1 to 6

Possible outputs: W#, W#i#, W#i#s#

Q5. Tuple Swap (Corrected)

def swap_first_last(tup):

if len(tup) < 2:

return tup

new_tup = (tup[-1],) + tup[1:-1] + (tup[0],)

return new_tup

result = swap_first_last((1,2,3,4))

print('Swapped tuple:', result)

Q6. Words with @cmail

def find_cmail():

with open('Emails.txt') as file:

for line in file:

for word in line.split():

if '@cmail' in word:

print(word)

Q7. Words > 5 chars from 'Words.txt'

def long_words():
Python Programming Questions and Answers

with open('Words.txt') as file:

for line in file:

for word in line.split():

if len(word) > 5:

print(word)

Q8. CSV 'Happiness.csv'

(I) def display_high_population():

with open('Happiness.csv') as file:

for row in csv.reader(file):

if int(row[1]) > 5000000:

print(row)

(II) def count_records():

with open('Happiness.csv') as file:

print(sum(1 for _ in csv.reader(file)))

Q9. Binary File for Candidates

(I) add_candidate(): get user input, pickle and append to file

(II) update_senior_managers(): change designation to 'Senior Manager' if exp > 10

(III) display_non_senior(): show all except Senior Managers

Q10. CSV 'Result.csv' Handling

Accept(): Write column headers and student records

wonCount(): Count and display rows where Result == 'Won'

You might also like