0% found this document useful (0 votes)
26 views40 pages

Computer Science Assignment - Google Docs2

Uploaded by

ywkmfccn6p
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)
26 views40 pages

Computer Science Assignment - Google Docs2

Uploaded by

ywkmfccn6p
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/ 40

‭Computer science Assignment‬

‭Q1. Output generated by code‬


‭Question:‬
‭What will the following code output?‬

python‬

string =‬‭
‭ "aabbcc"‬
count =‬‭
‭ 3‬
while‬‭
‭ True:‬
if‬‭
‭ string ==‬‭
'a'‬
:‬

string = string[‬
‭ 2‭
‭:‬]‬
elif‬‭
‭ string[-‬
1‬
‭ ] ==‬‭
‭ 'b'‬
:‬

string = string[:‬
‭ 2‬
‭ ]‬

else‬
‭ :‬

count +=‬‭
‭ 1‬
break‬

print‬
‭ (string)‬

print‬
‭ (count)‬

‭Answer:‬

text‬

bbcc‬

4‬

‭Q2. String slicing code output‬


‭Question:‬
‭Output for:‬

python‬

x =‬‭
‭ "helloworld"‬
print‬
‭ (x[:‬
‭ 2‬
‭ ], x[:-‬
‭ 2‬
‭ ], x[-‬
‭ 2‭
‭:‬])‬
print‬
‭ (x[‬
‭ 6‭
‭]‬, x[‬
2‭
‭ :
‬‭
4‬‭
]
‬)‬
print‬
‭ (x[‬
‭ 2‭
‭:‬-‬
3‭
‭ ]
‬, x[-‬
4‭
‭ :
‬-‬
2‬
‭ ])‬

‭Answer:‬

text‬

he hellowor ld‬

o ll‬

llowo or‬

‭Q3. Output of custom message loop code‬


‭Question:‬
‭Find the output of the following loop with Msg1, Msg2, Msg3.‬

‭Answer:‬
‭Analyzing each character, output is:‬

text‬

G*L*TM E‬

‭Q4. Corrected loop code (syntax errors underlined)‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬

python‬

30‬‭
‭ = To‬
For K‬‭
‭ in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To)‬
If K%‬
‭ 4‭
‭=‬‬
0‭
‭:‬‬
print‬
‭ (K*‬
‭ 4‭
‭)‬‬
Else:‬

print‬
‭ (K+‬
‭ 3‭
‭)‬‬

‭Answer:‬

python‬

To =‬‭
‭ 30‬ ‭
# corrected assignment‬
for‬‭
‭ K‬‭
in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To):‬ ‭
# corrected for syntax, colon‬
if‬‭
‭ K %‬‭
4‬‭
==‬‭
0‭
:
‬‬ # corrected if, ==, colon‬

print‬
‭ (K *‬‭
‭ 4‭
)‬‬ # indentation‬

else‬
‭ :‬
‭ # colon‬

print‬
‭ (K +‬‭
‭ 3‭
)‬‬ # indentation‬

‭Q5. Corrected code with all syntax fixes underlined‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬

‭Answer:‬

python‬

a =‬‭
‭ 5‬
work = True‬
‭ # capital T‬

b =‬‭
‭ "hello"‬ # quotes around string‬

c = b * a‬
‭ # repeat string‬

for‬‭
‭ i‬‭
in‬‭
range‬
(‭
‭ 1
‬0‬
):‬
‭ # lowercase for, add colon‬

if‬‭
‭ i %‬‭
7‬‭
==‬‭
0‭
:
‬‬ # == not =‬

continue‬

‭Q6. Corrected for loop for names‬


‭Question:‬
‭Rewrite with corrections and underline.‬

‭Answer:‬

python‬

for‬‭
‭ Name‬‭
in‬‭
[‭
"‬Ramesh"‬
,‬‭
‭ "Suraj"‬
,‬‭
‭ "Priya"‬
]:‬
‭ # quotes,‬‭
‭ colon‬
if‬‭
‭ Name ==‬‭
'S'‬
:‬
‭ # if, ==,‬‭
‭ colon‬
print‬
‭ (Name)‬
‭ # indentation‬

‭Q7. Corrected code with loop and print options‬


‭Question:‬
‭Rewrite with corrections and underline.‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

While c =<‬‭
‭ 20‬
:‬

print‬
‭ (c, END=‬
‭ "*"‬
‭ )‬

c +=‬‭
‭ 10‬
‭Answer:‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

while‬‭
‭ c <=‬‭
20‬
:‬
‭ # lowercase, <= not‬‭
‭ =<‬
print‬
‭ (c, end=‬
‭ "*"‬
‭ )‬
‭ # end, not END‬

c +=‬‭
‭ 10‬

‭Q8. Which printed outputs are possible from random‬


‭code?‬
‭Question:‬
‭Given the code below:‬

python‬

a = random.randint(‬
‭ 1‭
‭,‬‭
5
‬‬)‬

b = random.randint(‬
‭ 1‭
‭,‬‭
3
‬‬)‬

c = random.randint(‬
‭ 2‭
‭,‬‭
6
‬‬)‬

print‬
‭ (a, b, c)‬

‭Which outputs are possible?‬


‭(i) 213 (ii) 444 (iii) 321 (iv) 535‬

‭Answer:‬
‭Possible: (i) 213 and (iv) 535‬
‭Not possible: (ii) and (iii) due to out-of-range values‬
‭Q9. Output with n = 10 and 11‬
‭Question:‬
‭For n=10 or 11, what is output?‬

python‬

i =‬‭
‭ 2‬
while‬‭
‭ i < n:‬
if‬‭
‭ i %‬‭
5‬‭
==‬‭
0‭
:
‬‬
break‬

print‬
‭ (i)‬

i +=‬‭
‭ 1‬
else‬
‭ :‬

print‬
‭ (‬
‭ "done"‬
‭ )‬

‭Answer:‬

text‬

2‬

3‬

4‬

‭(The loop breaks when i=5 and does not reach the else)‬

‭Q10. Difference between List and Tuple‬


‭Answer:‬

‭‬ L
● ‭ ist: Ordered, mutable sequence; can change elements.‬
‭●‬ ‭Tuple: Ordered, immutable sequence; cannot modify after creation.‬
‭Q11. Ordered vs Unordered Collection‬
‭Answer:‬

‭●‬ ‭Ordered collection: Elements have a fixed, readable order. Example: List‬
[1,2,3]‬
‭ ‭, Tuple‬‭
(1,2,3)‬ ‭.‬
‭●‬ ‭Unordered collection: No intrinsic order. Example: Set‬‭ ‭, Dictionary‬
{1,2,3}‬
‭keys‬‭
{"a":1, "b":2}‬ ‭.‬

‭Q12. Dictionary operations‬


‭Question:‬
‭Employee = {“Empno‟:1,‟Name‟:‟Snehil‟,‟Salary‟:80000}‬
‭Write statements:‬
‭(i) Print employee name‬
‭(ii) Update salary to 90000‬
‭(iii) Get all values‬

‭Answer:‬

python‬

print‬
‭ (Employee[‬
‭ "Name"‬
‭ ])‬
‭ # (i)‬

Employee[‬
‭ "Salary"‬
‭ ] =‬‭
‭ 90000‬ # (ii)‬

print‬
‭ (Employee.values())‬
‭ # (iii)‬

‭Chapter: Functions‬

‭1. Area of triangle function‬


python‬

def‬‭
‭ triangle_area(base, height):‬
return‬‭
‭ 0.5‬‭
* base * height‬

‭2. Title case function‬


python‬

def‬‭
‭ title_case(sentence):‬
return‬‭
‭ sentence.title()‬

‭3. deleteChar() function‬


python‬

def‬‭
‭ deleteChar(string, ch):‬
return‬‭
‭ string.replace(ch,‬‭
""‬
)‬

‭4. Occurrences of substring using find()‬


python‬

line =‬‭
‭ "banana bandana"‬
substring =‬‭
‭ "ana"‬
count =‬‭
‭ 0‬
start =‬‭
‭ 0‬
while‬‭
‭ True:‬
pos = line.find(substring, start)‬

if‬‭
‭ pos == -‬
1‭
‭:‬‬
break‬

count +=‬‭
‭ 1‬
start = pos +‬‭
‭ 1‬
print‬
‭ (count)‬

‭5. Replace vowels in string‬
python‬

def‬‭
‭ replace_vowels(st):‬
vowels =‬‭
‭ "aeiouAEIOU"‬
return‬‭
‭ ""‬
.join([‬
‭ '*'‬‭
‭ if‬‭
ch‬‭
in‬‭
vowels‬‭
else‬‭
ch‬‭
for‬‭
ch‬‭
in‬‭
st])‬

‭6. Positional vs Keyword arg (example)‬


‭‬ P
● ‭ ositional: Provided by position.‬
‭●‬ ‭Keyword: Provided by name.‬
‭Example:‬

python‬

def‬‭
‭ greet(name, msg):‬
print‬
‭ (name, msg)‬

greet(‬
‭ "Alice"‬
‭ , msg=‬
‭ "Hello"‬
‭ )‬‭
‭ # keyword‬

‭7. Length of longest word‬


python‬

def‬‭
‭ longest_word(words):‬
return‬‭
‭ max‬
([‬
‭ len‬
‭ (word)‬‭
‭ for‬‭
word‬‭
in‬‭
words])‬

‭8. Output of fun1 (nonlocal/global error)‬


python‬

def‬‭
‭ fun1():‬
x =‬‭
‭ 100‬
def‬‭
‭ fun2():‬
x =‬‭
‭ 200‬
print‬
‭ (‬
‭ "Before calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

print‬
‭ (‬
‭ "Calling fun2 now:"‬
‭ )‬

fun2()‬

print‬
‭ (‬
‭ "After calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

x =‬‭
‭ 50‬
fun1()‬

print‬
‭ (‭
‭"‬x in main:"‬‭
+‬‭
str‬
(x))‬

‭Output:‬

text‬

Before calling fun2:100‬



Calling fun2 now:‬

After calling fun2:100‬

x in main:50‬

‭9. Actual vs Formal parameters‬


‭ ‬ ‭Actual: Supplied during function call.‬

‭●‬ ‭Formal: Declared in function definition.‬
python‬

def‬‭
‭ add(a, b):‬ ‭
# formal‬
return‬‭
‭ a + b‬
add(‬
‭ 2‬
‭ ,‬‭
‭ 3‭
)‬‬ # actual‬

‭10. Default arguments‬


‭Arguments in a function with default value used if not provided.‬

python‬

def‬‭
‭ func(a, b=‬
10‬
‭ ):‬‭
‭ # b is default‬
return‬‭
‭ a + b‬
func(‬
‭ 5‭
‭)‬‬‭
# uses default 10‬
‭11. Local vs Global variable‬
‭‬ L
● ‭ ocal: Defined inside function, accessible only there.‬
‭●‬ ‭Global: Defined outside, accessible everywhere.‬

‭12. Output for power/calcSquare‬


python‬

def‬‭
‭ power(b, P):‬
r = b ** P‬

return‬‭
‭ r‬
def‬‭
‭ calcSquare(a):‬
a = power(a,‬‭
‭ 2‬)‬

return‬‭
‭ a‬
n =‬‭
‭ 5‬
result = calcSquare(n)‬

print‬
‭ (result)‬

‭Output:‬

text‬

25‬

‭This content is formatted for easy reading and conversion to PDF. Each answer is‬
‭cited and sectioned for clarity.‬

‭Related‬
‭ ow can I convert code output into a PDF document in Python‬
H
‭What tools or libraries are best for compiling code explanations into a single‬
‭document‬
‭How to generate a summarized PDF report from multiple coding examples‬
‭ an I automate the creation of a PDF with formatted code and explanations‬
C
‭What are the recommended methods for combining multiple outputs into one file‬

‭Q1. Output generated by code‬


‭Question:‬
‭What will the following code output?‬

python‬

string =‬‭
‭ "aabbcc"‬
count =‬‭
‭ 3‬
while‬‭
‭ True:‬
if‬‭
‭ string ==‬‭
'a'‬
:‬

string = string[‬
‭ 2‭
‭:‬]‬
elif‬‭
‭ string[-‬
1‬
‭ ] ==‬‭
‭ 'b'‬
:‬

string = string[:‬
‭ 2‬
‭ ]‬

else‬
‭ :‬

count +=‬‭
‭ 1‬
break‬

print‬
‭ (string)‬

print‬
‭ (count)‬

‭Answer:‬

text‬

bbcc‬

4‬

‭Q2. String slicing code output‬


‭Question:‬
‭Output for:‬
python‬

x =‬‭
‭ "helloworld"‬
print‬
‭ (x[:‬
‭ 2‬
‭ ], x[:-‬
‭ 2‬
‭ ], x[-‬
‭ 2‭
‭:‬])‬
print‬
‭ (x[‬
‭ 6‭
‭]‬, x[‬
2‭
‭ :
‬‭
4‬‭
]
‬)‬
print‬
‭ (x[‬
‭ 2‭
‭:‬-‬
3‭
‭ ]
‬, x[-‬
4‭
‭ :
‬-‬
2‬
‭ ])‬

‭Answer:‬

text‬

he hellowor ld‬

o ll‬

llowo or‬

‭Q3. Output of custom message loop code‬


‭Question:‬
‭Find the output of the following loop with Msg1, Msg2, Msg3.‬

‭Answer:‬
‭Analyzing each character, output is:‬

text‬

G*L*TM E‬

‭Q4. Corrected loop code (syntax errors underlined)‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬
python‬

30‬‭
‭ = To‬
For K‬‭
‭ in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To)‬
If K%‬
‭ 4‭
‭=‬‬
0‭
‭:‬‬
print‬
‭ (K*‬
‭ 4‭
‭)‬‬
Else:‬

print‬
‭ (K+‬
‭ 3‭
‭)‬‬

‭Answer:‬

python‬

To =‬‭
‭ 30‬ ‭
# corrected assignment‬
for‬‭
‭ K‬‭
in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To):‬ ‭
# corrected for syntax, colon‬
if‬‭
‭ K %‬‭
4‬‭
==‬‭
0‭
:
‬‬ # corrected if, ==, colon‬

print‬
‭ (K *‬‭
‭ 4‭
)‬‬ # indentation‬

else‬
‭ :‬
‭ # colon‬

print‬
‭ (K +‬‭
‭ 3‭
)‬‬ # indentation‬

‭Q5. Corrected code with all syntax fixes underlined‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬

‭Answer:‬

python‬

a =‬‭
‭ 5‬
work = True‬
‭ # capital T‬

b =‬‭
‭ "hello"‬ # quotes around string‬

c = b * a‬
‭ # repeat string‬

for‬‭
‭ i‬‭
in‬‭
range‬
(‭
‭ 1
‬0‬
):‬
‭ # lowercase for, add colon‬

if‬‭
‭ i %‬‭
7‬‭
==‬‭
0‭
:
‬‬ # == not =‬

continue‬

‭Q6. Corrected for loop for names‬


‭Question:‬
‭Rewrite with corrections and underline.‬

‭Answer:‬

python‬

for‬‭
‭ Name‬‭
in‬‭
[‭
"‬Ramesh"‬
,‬‭
‭ "Suraj"‬
,‬‭
‭ "Priya"‬
]:‬
‭ # quotes,‬‭
‭ colon‬
if‬‭
‭ Name ==‬‭
'S'‬
:‬
‭ # if, ==,‬‭
‭ colon‬
print‬
‭ (Name)‬
‭ # indentation‬

‭Q7. Corrected code with loop and print options‬


‭Question:‬
‭Rewrite with corrections and underline.‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

While c =<‬‭
‭ 20‬
:‬

print‬
‭ (c, END=‬
‭ "*"‬
‭ )‬

c +=‬‭
‭ 10‬

‭Answer:‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

while‬‭
‭ c <=‬‭
20‬
:‬
‭ # lowercase, <= not‬‭
‭ =<‬
print‬
‭ (c, end=‬
‭ "*"‬
‭ )‬
‭ # end, not END‬

c +=‬‭
‭ 10‬

‭Q8. Which printed outputs are possible from random‬


‭code?‬
‭Question:‬
‭Given the code below:‬

python‬

a = random.randint(‬
‭ 1‭
‭,‬‭
5
‬‬)‬

b = random.randint(‬
‭ 1‭
‭,‬‭
3
‬‬)‬

c = random.randint(‬
‭ 2‭
‭,‬‭
6
‬‬)‬

print‬
‭ (a, b, c)‬

‭Which outputs are possible?‬


‭(i) 213 (ii) 444 (iii) 321 (iv) 535‬

‭Answer:‬
‭Possible: (i) 213 and (iv) 535‬
‭Not possible: (ii) and (iii) due to out-of-range values‬

‭Q9. Output with n = 10 and 11‬


‭Question:‬
‭For n=10 or 11, what is output?‬

python‬

i =‬‭
‭ 2‬
while‬‭
‭ i < n:‬
if‬‭
‭ i %‬‭
5‬‭
==‬‭
0‭
:
‬‬
break‬

print‬
‭ (i)‬

i +=‬‭
‭ 1‬
else‬
‭ :‬

print‬
‭ (‬
‭ "done"‬
‭ )‬

‭Answer:‬

text‬

2‬

3‬

4‬

‭(The loop breaks when i=5 and does not reach the else)‬

‭Q10. Difference between List and Tuple‬


‭Answer:‬

‭‬ L
● ‭ ist: Ordered, mutable sequence; can change elements.‬
‭●‬ ‭Tuple: Ordered, immutable sequence; cannot modify after creation.‬

‭Q11. Ordered vs Unordered Collection‬


‭Answer:‬

‭●‬ ‭Ordered collection: Elements have a fixed, readable order. Example: List‬
[1,2,3]‬
‭ ‭, Tuple‬‭
(1,2,3)‬ ‭.‬
‭●‬ ‭Unordered collection: No intrinsic order. Example: Set‬‭ ‭, Dictionary‬
{1,2,3}‬
‭keys‬‭
{"a":1, "b":2}‬ ‭.‬

‭Q12. Dictionary operations‬


‭Question:‬
‭Employee = {“Empno‟:1,‟Name‟:‟Snehil‟,‟Salary‟:80000}‬
‭Write statements:‬
‭(i) Print employee name‬
‭(ii) Update salary to 90000‬
‭(iii) Get all values‬

‭Answer:‬

python‬

print‬
‭ (Employee[‬
‭ "Name"‬
‭ ])‬
‭ # (i)‬

Employee[‬
‭ "Salary"‬
‭ ] =‬‭
‭ 90000‬ # (ii)‬

print‬
‭ (Employee.values())‬
‭ # (iii)‬

‭Chapter: Functions‬

‭1. Area of triangle function‬


python‬

def‬‭
‭ triangle_area(base, height):‬
return‬‭
‭ 0.5‬‭
* base * height‬
‭2. Title case function‬
python‬

def‬‭
‭ title_case(sentence):‬
return‬‭
‭ sentence.title()‬

‭3. deleteChar() function‬


python‬

def‬‭
‭ deleteChar(string, ch):‬
return‬‭
‭ string.replace(ch,‬‭
""‬
)‬

‭4. Occurrences of substring using find()‬


python‬

line =‬‭
‭ "banana bandana"‬
substring =‬‭
‭ "ana"‬
count =‬‭
‭ 0‬
start =‬‭
‭ 0‬
while‬‭
‭ True:‬
pos = line.find(substring, start)‬

if‬‭
‭ pos == -‬
1‭
‭:‬‬
break‬

count +=‬‭
‭ 1‬
start = pos +‬‭
‭ 1‬
print‬
‭ (count)‬

‭5. Replace vowels in string‬


python‬

def‬‭
‭ replace_vowels(st):‬
vowels =‬‭
‭ "aeiouAEIOU"‬
return‬‭
‭ ""‬
.join([‬
‭ '*'‬‭
‭ if‬‭
ch‬‭
in‬‭
vowels‬‭
else‬‭
ch‬‭
for‬‭
ch‬‭
in‬‭
st])‬

‭6. Positional vs Keyword arg (example)‬


‭‬ P
● ‭ ositional: Provided by position.‬
‭●‬ ‭Keyword: Provided by name.‬
‭Example:‬

python‬

def‬‭
‭ greet(name, msg):‬
print‬
‭ (name, msg)‬

greet(‬
‭ "Alice"‬
‭ , msg=‬
‭ "Hello"‬
‭ )‬‭
‭ # keyword‬

‭7. Length of longest word‬


python‬

def‬‭
‭ longest_word(words):‬
return‬‭
‭ max‬
([‬
‭ len‬
‭ (word)‬‭
‭ for‬‭
word‬‭
in‬‭
words])‬

‭8. Output of fun1 (nonlocal/global error)‬


python‬

def‬‭
‭ fun1():‬
x =‬‭
‭ 100‬
def‬‭
‭ fun2():‬
x =‬‭
‭ 200‬
print‬
‭ (‬
‭ "Before calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

print‬
‭ (‬
‭ "Calling fun2 now:"‬
‭ )‬

fun2()‬

print‬
‭ (‬
‭ "After calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

x =‬‭
‭ 50‬
fun1()‬

print‬
‭ (‭
‭"‬x in main:"‬‭
+‬‭
str‬
(x))‬

‭Output:‬

text‬

Before calling fun2:100‬



Calling fun2 now:‬

After calling fun2:100‬

x in main:50‬

‭9. Actual vs Formal parameters‬


‭ ‬ ‭Actual: Supplied during function call.‬

‭●‬ ‭Formal: Declared in function definition.‬
python‬

def‬‭
‭ add(a, b):‬ ‭
# formal‬
return‬‭
‭ a + b‬
add(‬
‭ 2‬
‭ ,‬‭
‭ 3‭
)‬‬ # actual‬

‭10. Default arguments‬


‭Arguments in a function with default value used if not provided.‬

python‬

def‬‭
‭ func(a, b=‬
10‬
‭ ):‬‭
‭ # b is default‬
return‬‭
‭ a + b‬
func(‬
‭ 5‭
‭)‬‬‭
# uses default 10‬

‭11. Local vs Global variable‬


‭‬ L
● ‭ ocal: Defined inside function, accessible only there.‬
‭●‬ ‭Global: Defined outside, accessible everywhere.‬

‭12. Output for power/calcSquare‬


python‬

def‬‭
‭ power(b, P):‬
r = b ** P‬

return‬‭
‭ r‬
def‬‭
‭ calcSquare(a):‬
a = power(a,‬‭
‭ 2‬)‬

return‬‭
‭ a‬
n =‬‭
‭ 5‬
result = calcSquare(n)‬

print‬
‭ (result)‬

‭Output:‬

text‬

25‬

‭This content is formatted for easy reading and conversion to PDF. Each answer is‬
‭cited and sectioned for clarity.‬

‭Related‬
‭ ow can I convert code output into a PDF document in Python‬
H
‭What tools or libraries are best for compiling code explanations into a single‬
‭document‬
‭How to generate a summarized PDF report from multiple coding examples‬
‭Can I automate the creation of a PDF with formatted code and explanations‬
‭What are the recommended methods for combining multiple outputs into one file‬
‭Question:‬
‭What will the following code output?‬

python‬

string =‬‭
‭ "aabbcc"‬
count =‬‭
‭ 3‬
while‬‭
‭ True:‬
if‬‭
‭ string ==‬‭
'a'‬
:‬

string = string[‬
‭ 2‭
‭:‬]‬
elif‬‭
‭ string[-‬
1‬
‭ ] ==‬‭
‭ 'b'‬
:‬

string = string[:‬
‭ 2‬
‭ ]‬

else‬
‭ :‬

count +=‬‭
‭ 1‬
break‬

print‬
‭ (string)‬

print‬
‭ (count)‬

‭Answer:‬

text‬

bbcc‬

4‬

‭Q2. String slicing code output‬


‭Question:‬
‭Output for:‬

python‬

x =‬‭
‭ "helloworld"‬
print‬
‭ (x[:‬
‭ 2‬
‭ ], x[:-‬
‭ 2‬
‭ ], x[-‬
‭ 2‭
‭:‬])‬
print‬
‭ (x[‬
‭ 6‭
‭]‬, x[‬
2‭
‭ :
‬‭
4‬‭
]
‬)‬
print‬
‭ (x[‬
‭ 2‭
‭:‬-‬
3‭
‭ ]
‬, x[-‬
4‭
‭ :
‬-‬
2‬
‭ ])‬

‭Answer:‬

text‬

he hellowor ld‬

o ll‬

llowo or‬

‭Q3. Output of custom message loop code‬


‭Question:‬
‭Find the output of the following loop with Msg1, Msg2, Msg3.‬

‭Answer:‬
‭Analyzing each character, output is:‬

text‬

G*L*TM E‬

‭Q4. Corrected loop code (syntax errors underlined)‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬

python‬

30‬‭
‭ = To‬
For K‬‭
‭ in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To)‬
If K%‬
‭ 4‭
‭=‬‬
0‭
‭:‬‬
print‬
‭ (K*‬
‭ 4‭
‭)‬‬
Else:‬

print‬
‭ (K+‬
‭ 3‭
‭)‬‬

‭Answer:‬

python‬

To =‬‭
‭ 30‬ ‭
# corrected assignment‬
for‬‭
‭ K‬‭
in‬‭
range‬
(‭
‭ 0
‬‭
,‬ To):‬ ‭
# corrected for syntax, colon‬
if‬‭
‭ K %‬‭
4‬‭
==‬‭
0‭
:
‬‬ # corrected if, ==, colon‬

print‬
‭ (K *‬‭
‭ 4‭
)‬‬ # indentation‬

else‬
‭ :‬
‭ # colon‬

print‬
‭ (K +‬‭
‭ 3‭
)‬‬ # indentation‬

‭Q5. Corrected code with all syntax fixes underlined‬


‭Question:‬
‭Rewrite with corrections and underline each change.‬

‭Answer:‬

python‬

a =‬‭
‭ 5‬
work = True‬
‭ # capital T‬

b =‬‭
‭ "hello"‬ # quotes around string‬

c = b * a‬
‭ # repeat string‬

for‬‭
‭ i‬‭
in‬‭
range‬
(‭
‭ 1
‬0‬
):‬
‭ # lowercase for, add colon‬

if‬‭
‭ i %‬‭
7‬‭
==‬‭
0‭
:
‬‬ # == not =‬

continue‬

‭Q6. Corrected for loop for names‬
‭Question:‬
‭Rewrite with corrections and underline.‬

‭Answer:‬

python‬

for‬‭
‭ Name‬‭
in‬‭
[‭
"‬Ramesh"‬
,‬‭
‭ "Suraj"‬
,‬‭
‭ "Priya"‬
]:‬
‭ # quotes,‬‭
‭ colon‬
if‬‭
‭ Name ==‬‭
'S'‬
:‬
‭ # if, ==,‬‭
‭ colon‬
print‬
‭ (Name)‬
‭ # indentation‬

‭Q7. Corrected code with loop and print options‬


‭Question:‬
‭Rewrite with corrections and underline.‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

While c =<‬‭
‭ 20‬
:‬

print‬
‭ (c, END=‬
‭ "*"‬
‭ )‬

c +=‬‭
‭ 10‬

‭Answer:‬

python‬

a = b =‬‭
‭ 10‬
c = a + b‬

while‬‭
‭ c <=‬‭
20‬
:‬
‭ # lowercase, <= not‬‭
‭ =<‬
print‬
‭ (c, end=‬
‭ "*"‬
‭ )‬
‭ # end, not END‬

c +=‬‭
‭ 10‬
‭Q8. Which printed outputs are possible from random‬
‭code?‬
‭Question:‬
‭Given the code below:‬

python‬

a = random.randint(‬
‭ 1‭
‭,‬‭
5
‬‬)‬

b = random.randint(‬
‭ 1‭
‭,‬‭
3
‬‬)‬

c = random.randint(‬
‭ 2‭
‭,‬‭
6
‬‬)‬

print‬
‭ (a, b, c)‬

‭Which outputs are possible?‬


‭(i) 213 (ii) 444 (iii) 321 (iv) 535‬

‭Answer:‬
‭Possible: (i) 213 and (iv) 535‬
‭Not possible: (ii) and (iii) due to out-of-range values‬

‭Q9. Output with n = 10 and 11‬


‭Question:‬
‭For n=10 or 11, what is output?‬

python‬

i =‬‭
‭ 2‬
while‬‭
‭ i < n:‬
if‬‭
‭ i %‬‭
5‬‭
==‬‭
0‭
:
‬‬
break‬

print‬
‭ (i)‬

i +=‬‭
‭ 1‬
else‬
‭ :‬

print‬
‭ (‬
‭ "done"‬
‭ )‬

‭Answer:‬

text‬

2‬

3‬

4‬

‭(The loop breaks when i=5 and does not reach the else)‬

‭Q10. Difference between List and Tuple‬


‭Answer:‬

‭‬ L
● ‭ ist: Ordered, mutable sequence; can change elements.‬
‭●‬ ‭Tuple: Ordered, immutable sequence; cannot modify after creation.‬

‭Q11. Ordered vs Unordered Collection‬


‭Answer:‬

‭●‬ ‭Ordered collection: Elements have a fixed, readable order. Example: List‬
[1,2,3]‬
‭ ‭, Tuple‬‭
(1,2,3)‬ ‭.‬
‭●‬ ‭Unordered collection: No intrinsic order. Example: Set‬‭ ‭, Dictionary‬
{1,2,3}‬
‭keys‬‭
{"a":1, "b":2}‬ ‭.‬
‭Q12. Dictionary operations‬
‭Question:‬
‭Employee = {“Empno‟:1,‟Name‟:‟Snehil‟,‟Salary‟:80000}‬
‭Write statements:‬
‭(i) Print employee name‬
‭(ii) Update salary to 90000‬
‭(iii) Get all values‬

‭Answer:‬

python‬

print‬
‭ (Employee[‬
‭ "Name"‬
‭ ])‬
‭ # (i)‬

Employee[‬
‭ "Salary"‬
‭ ] =‬‭
‭ 90000‬ # (ii)‬

print‬
‭ (Employee.values())‬
‭ # (iii)‬

‭Chapter: Functions‬

‭1. Area of triangle function‬


python‬

def‬‭
‭ triangle_area(base, height):‬
return‬‭
‭ 0.5‬‭
* base * height‬

‭2. Title case function‬


python‬

def‬‭
‭ title_case(sentence):‬
return‬‭
‭ sentence.title()‬

‭3. deleteChar() function‬


python‬

def‬‭
‭ deleteChar(string, ch):‬
return‬‭
‭ string.replace(ch,‬‭
""‬
)‬

‭4. Occurrences of substring using find()‬


python‬

line =‬‭
‭ "banana bandana"‬
substring =‬‭
‭ "ana"‬
count =‬‭
‭ 0‬
start =‬‭
‭ 0‬
while‬‭
‭ True:‬
pos = line.find(substring, start)‬

if‬‭
‭ pos == -‬
1‭
‭:‬‬
break‬

count +=‬‭
‭ 1‬
start = pos +‬‭
‭ 1‬
print‬
‭ (count)‬

‭5. Replace vowels in string‬


python‬

def‬‭
‭ replace_vowels(st):‬
vowels =‬‭
‭ "aeiouAEIOU"‬
return‬‭
‭ ""‬
.join([‬
‭ '*'‬‭
‭ if‬‭
ch‬‭
in‬‭
vowels‬‭
else‬‭
ch‬‭
for‬‭
ch‬‭
in‬‭
st])‬
‭6. Positional vs Keyword arg (example)‬
‭‬ P
● ‭ ositional: Provided by position.‬
‭●‬ ‭Keyword: Provided by name.‬
‭Example:‬

python‬

def‬‭
‭ greet(name, msg):‬
print‬
‭ (name, msg)‬

greet(‬
‭ "Alice"‬
‭ , msg=‬
‭ "Hello"‬
‭ )‬‭
‭ # keyword‬

‭7. Length of longest word‬


python‬

def‬‭
‭ longest_word(words):‬
return‬‭
‭ max‬
([‬
‭ len‬
‭ (word)‬‭
‭ for‬‭
word‬‭
in‬‭
words])‬

‭8. Output of fun1 (nonlocal/global error)‬


python‬

def‬‭
‭ fun1():‬
x =‬‭
‭ 100‬
def‬‭
‭ fun2():‬
x =‬‭
‭ 200‬
print‬
‭ (‬
‭ "Before calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

print‬
‭ (‬
‭ "Calling fun2 now:"‬
‭ )‬

fun2()‬

print‬
‭ (‬
‭ "After calling fun2:"‬‭
‭ +‬‭
str‬
(x))‬

x =‬‭
‭ 50‬
fun1()‬

print‬
‭ (‭
‭"‬x in main:"‬‭
+‬‭
str‬
(x))‬

‭Output:‬

text‬

Before calling fun2:100‬



Calling fun2 now:‬

After calling fun2:100‬

x in main:50‬

‭9. Actual vs Formal parameters‬


‭ ‬ ‭Actual: Supplied during function call.‬

‭●‬ ‭Formal: Declared in function definition.‬
python‬

def‬‭
‭ add(a, b):‬ ‭
# formal‬
return‬‭
‭ a + b‬
add(‬
‭ 2‬
‭ ,‬‭
‭ 3‭
)‬‬ # actual‬

‭10. Default arguments‬


‭Arguments in a function with default value used if not provided.‬

python‬

def‬‭
‭ func(a, b=‬
10‬
‭ ):‬‭
‭ # b is default‬
return‬‭
‭ a + b‬
func(‬
‭ 5‭
‭)‬‬‭
# uses default 10‬

‭11. Local vs Global variable‬


‭‬ L
● ‭ ocal: Defined inside function, accessible only there.‬
‭●‬ ‭Global: Defined outside, accessible everywhere.‬

‭12. Output for power/calcSquare‬


python‬

def‬‭
‭ power(b, P):‬
r = b ** P‬

return‬‭
‭ r‬
def‬‭
‭ calcSquare(a):‬
a = power(a,‬‭
‭ 2‬)‬

return‬‭
‭ a‬
n =‬‭
‭ 5‬
result = calcSquare(n)‬

print‬
‭ (result)‬

‭String Functions‬

‭Basic String Manipulation‬


sql‬

-- Display first 3 characters of country name‬



SELECT‬‭
‭ LEFT‬
(‭
‭"
‬INDIA"‬
,‬‭
‭ 3‭
)
‬;‬

-- Display last 4 characters‬



SELECT‬‭
‭ RIGHT‬
(‬
‭"Computer Science"‬
‭ ,‬‭
‭ 4‬
);‬

-- Extract middle portion of string‬



SELECT‬‭
‭ MID(‬
"Informatics"‬
‭ ,‬‭
‭ 3‬
,‬‭
‭ 4‭
)
‬;‬
SELECT‬‭
‭ SUBSTR(‬
"Practices"‬
‭ ,‬‭
‭ 3‭
)
‬;‬

-- Get length of string‬



SELECT‬‭
‭ LENGTH(‬
"Informatics Practices"‬
‭ );‬

‭Case Conversion Functions‬


sql‬

-- Convert to uppercase‬

SELECT‬‭
‭ UPPER(‬
"informatics practices"‬
‭ );‬

-- Convert to lowercase‬

SELECT‬‭
‭ LOWER(‬
"COMPUTER SCIENCE"‬
‭ );‬

-- Remove leading/trailing spaces‬



SELECT‬‭
‭ TRIM(‬
"
‭ Hello World "‬
);‬

‭String Search Functions‬


sql‬

-- Find position of substring‬



SELECT‬‭
‭ INSTR(‬
"WELCOME WORLD"‬
‭ ,‬‭
‭ "COME"‬
);‬

-- Check if string contains pattern‬



SELECT‬‭
‭ LOCATE(‬
"tech"‬
‭ ,‬‭
‭ "Computer Technology"‬
);‬

‭Mathematical Functions‬

‭Basic Math Operations‬


sql‬

-- Power function‬

SELECT‬‭
‭ POW(‬
2‭
‭,
‬‬‭
3‭
)
‬;‬
SELECT‬‭
‭ POWER(‬
5‭
‭,
‬‬‭
2‭
)
‬;‬

-- Square root‬

SELECT‬‭
‭ SQRT(‬
25‬
‭ );‬

-- Absolute value‬

SELECT‬‭
‭ ABS(-‬
15‬
‭ );‬

-- Modulus (remainder)‬

SELECT‬‭
‭ MOD(‬
100‬
‭ ,‬‭
‭ 9‭
)
‬;‬
‭Rounding Functions‬
sql‬

-- Round to specific decimal places‬



SELECT‬‭
‭ ROUND(‬
123.2345‬
‭ ,‬‭
‭ 2‭
)
‬;‬
SELECT‬‭
‭ ROUND(‬
342.9234‬
‭ , -‬
‭ 1‬
‭);‬

-- Ceiling and floor‬



SELECT‬‭
‭ CEIL(‬
4.3‬
‭ );‬

SELECT‬‭
‭ FLOOR(‬
4.9‬
‭ );‬

-- Truncate decimal places‬



SELECT‬‭
‭ TRUNCATE‬
(‬
‭123.456‬
‭ ,‬‭
‭ 1‭
)
‬;‬

‭Date and Time Functions‬

‭Current Date/Time‬
sql‬

-- Get current date‬



SELECT‬‭
‭ CURDATE();‬

-- Get current time‬



SELECT‬‭
‭ CURTIME();‬

-- Get current date and time‬



SELECT‬‭
‭ NOW();‬

‭Date Extraction Functions‬


sql‬

-- Extract year, month, day‬



SELECT‬‭
‭ YEAR‬
(‭
‭"
‬1979/11/26"‬
),‬‭
‭ MONTH‬
(‭
‭"
‬1979/11/26"‬
),‬‭
‭ DAY‬
(‭
‭"
‬1979/11/26"‬
);‬

-- Get month and day names‬



SELECT‬‭
‭ MONTHNAME(‬
"1979/11/26"‬
‭ ), DAYNAME(‬
‭ "1979/11/26"‬
‭ );‬

-- Extract time components‬



SELECT‬‭
‭ HOUR‬
(NOW()),‬‭
‭ MINUTE‬
(NOW()),‬‭
‭ SECOND‬
(NOW());‬

‭Aggregate Functions‬

‭Basic Aggregate Operations‬


sql‬

-- Assuming a table STUDENT with columns: StudentID, Name, Marks,‬



Grade‬

SELECT‬‭
‭ COUNT(*)‬‭
FROM‬‭
STUDENT;‬

SELECT‬‭
‭ MAX(Marks)‬‭
FROM‬‭
STUDENT;‬

SELECT‬‭
‭ MIN(Marks)‬‭
FROM‬‭
STUDENT;‬

SELECT‬‭
‭ AVG(Marks)‬‭
FROM‬‭
STUDENT;‬

SELECT‬‭
‭ SUM(Marks)‬‭
FROM‬‭
STUDENT;‬

‭Advanced Aggregate Queries‬


sql‬

-- Count non-null values‬



SELECT‬‭
‭ COUNT(Grade)‬‭
FROM‬‭
STUDENT‬‭
WHERE‬‭
Grade IS NOT‬‭
NULL;‬

-- Group by with aggregate‬



SELECT‬‭
‭ Grade, COUNT(*)‬‭
FROM‬‭
STUDENT‬‭
GROUP‬‭
BY‬‭
Grade;‬

SELECT‬‭
‭ Grade, AVG(Marks)‬‭
FROM‬‭
STUDENT‬‭
GROUP‬‭
BY‬‭
Grade‬‭
HAVING‬
AVG(Marks) >‬‭
‭ 75‬
;‬

‭Practical Exercise Questions‬

‭Question 1: Product Table Operations‬


sql‬

-- Create table‬

CREATE‬‭
‭ TABLE‬‭
PRODUCT (‬
PCode‬‭
‭ VARCHAR‬
(‬
‭10‬
‭ )‬‭
‭ PRIMARY‬‭
KEY‬
,‬

PName‬‭
‭ VARCHAR‬
(‬
‭50‬
‭ ) NOT NULL,‬

UPrice‬‭
‭ DECIMAL‬
(‭
‭1
‬0‬
,‭
‭2
‬‬
),‬

Quantity‬‭
‭ INT‬
);‬

-- Add discount column‬



ALTER‬‭
‭ TABLE‬‭
PRODUCT‬‭
ADD‬‭
DISCOUNT‬‭
DECIMAL‬
(‬
‭10‬
‭ ,‭
‭2
‬‭
)
‬;‬

-- Calculate 10% discount for items > 100‬



UPDATE‬‭
‭ PRODUCT‬‭
SET‬‭
DISCOUNT =‬
CASE‬

WHEN‬‭
‭ UPrice >‬‭
100‬‭
THEN‬‭
UPrice *‬‭
0.10‬
ELSE‬‭
‭ 0‬
END‬
‭ ;‬

‭Question 2: Employee Commission Calculation‬


sql‬

-- Add commission column‬



ALTER‬‭
‭ TABLE‬‭
SALE‬‭
ADD‬‭
Commission‬‭
NUMERIC‬
(‭
‭7
‬‬
,‭
‭2
‬‬
);‬

-- Calculate 12% commission‬



UPDATE‬‭
‭ SALE‬‭
SET‬‭
Commission =‬‭
12‬
/‬
‭100‬‭
‭ * SalePrice;‬

-- Display records where commission > 73000‬



SELECT‬‭
‭ *‬‭
FROM‬‭
SALE‬‭
WHERE‬‭
Commission >‬‭
73000‬
;‬

‭Question 3: Mixed Function Queries‬
sql‬

-- Display current month name‬



SELECT‬‭
‭ MONTHNAME(CURDATE());‬

-- Remove spaces and convert to upper case‬



SELECT‬‭
‭ UPPER(TRIM(‬
"
‭ panorama "‬
));‬

-- Extract 7 characters from 7th position‬



SELECT‬‭
‭ SUBSTR(‬
"INDIA SHINING"‬
‭ ,‬‭
‭ 7‬
,‬‭
‭ 7‭
)
‬;‬

-- Round salary to nearest 100‬



SELECT‬‭
‭ EmpName, ROUND(Salary, -‬
2‬
‭)‬‭
‭ FROM‬‭
EMPLOYEE;‬

-- Display day name of birth date‬



SELECT‬‭
‭ Name, DAYNAME(DOB)‬‭
FROM‬‭
STUDENT;‬

‭Sample Database Queries for Practice‬

‭Student Performance Analysis‬


sql‬

-- Assuming STUDENT table: StudentNo, Name, Subject, Marks, Grade‬


-- Count students by grade‬



SELECT‬‭
‭ Grade, COUNT(*)‬‭
as‬‭
StudentCount‬
FROM‬‭
‭ STUDENT‬
GROUP‬‭
‭ BY‬‭
Grade;‬

-- Average marks by subject‬



SELECT‬‭
‭ Subject, ROUND(AVG(Marks),‬‭
2‭
)
‬‬‭
as‬‭
AvgMarks‬
FROM‬‭
‭ STUDENT‬
GROUP‬‭
‭ BY‬‭
Subject;‬

-- Students with marks above average‬



SELECT‬‭
‭ Name, Marks‬
FROM‬‭
‭ STUDENT‬
WHERE‬‭
‭ Marks > (‬
SELECT‬‭
‭ AVG(Marks)‬‭
FROM‬‭
STUDENT);‬
-- Extract first word from full name‬

SELECT‬‭
‭ LEFT‬
(Name, INSTR(Name,‬‭
‭ ' '‬
) -‬‭
‭ 1‭
)
‬‬‭
as‬‭
FirstName‬‭
FROM‬‭
STUDENT;‬

-- Display names starting with 'A'‬



SELECT‬‭
‭ Name‬‭
FROM‬‭
STUDENT‬‭
WHERE‬‭
Name LIKE‬‭
'A%'‬
;‬

-- Count characters in longest name‬



SELECT‬‭
‭ MAX(LENGTH(Name))‬‭
FROM‬‭
STUDENT;‬

You might also like