Computer Science Assignment - Google Docs2
Computer Science Assignment - Google Docs2
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
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
Answer:
Analyzing each character, output is:
text
G*L*TM E
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
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
Answer:
python
for
Name
in
[
"Ramesh"
,
"Suraj"
,
"Priya"
]:
# quotes,
colon
if
Name ==
'S'
:
# if, ==,
colon
print
(Name)
# indentation
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
python
a = random.randint(
1
,
5
)
b = random.randint(
1
,
3
)
c = random.randint(
2
,
6
)
print
(a, b, c)
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)
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} .
Answer:
python
print
(Employee[
"Name"
])
# (i)
Employee[
"Salary"
] =
90000 # (ii)
print
(Employee.values())
# (iii)
Chapter: Functions
def
title_case(sentence):
return
sentence.title()
def
deleteChar(string, ch):
return
string.replace(ch,
""
)
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])
python
def
greet(name, msg):
print
(name, msg)
greet(
"Alice"
, msg=
"Hello"
)
# keyword
def
longest_word(words):
return
max
([
len
(word)
for
word
in
words])
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
def
add(a, b):
# formal
return
a + b
add(
2
,
3
) # actual
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.
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
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
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
Answer:
Analyzing each character, output is:
text
G*L*TM E
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
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
Answer:
python
for
Name
in
[
"Ramesh"
,
"Suraj"
,
"Priya"
]:
# quotes,
colon
if
Name ==
'S'
:
# if, ==,
colon
print
(Name)
# indentation
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
python
a = random.randint(
1
,
5
)
b = random.randint(
1
,
3
)
c = random.randint(
2
,
6
)
print
(a, b, c)
Answer:
Possible: (i) 213 and (iv) 535
Not possible: (ii) and (iii) due to out-of-range values
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)
L
● ist: Ordered, mutable sequence; can change elements.
● Tuple: Ordered, immutable sequence; cannot modify after creation.
● 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} .
Answer:
python
print
(Employee[
"Name"
])
# (i)
Employee[
"Salary"
] =
90000 # (ii)
print
(Employee.values())
# (iii)
Chapter: Functions
def
triangle_area(base, height):
return
0.5
* base * height
2. Title case function
python
def
title_case(sentence):
return
sentence.title()
def
deleteChar(string, ch):
return
string.replace(ch,
""
)
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)
python
def
greet(name, msg):
print
(name, msg)
greet(
"Alice"
, msg=
"Hello"
)
# keyword
def
longest_word(words):
return
max
([
len
(word)
for
word
in
words])
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
def
add(a, b):
# formal
return
a + b
add(
2
,
3
) # actual
python
def
func(a, b=
10
):
# b is default
return
a + b
func(
5
)
# uses default 10
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
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
Answer:
Analyzing each character, output is:
text
G*L*TM E
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
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
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)
Answer:
Possible: (i) 213 and (iv) 535
Not possible: (ii) and (iii) due to out-of-range values
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)
L
● ist: Ordered, mutable sequence; can change elements.
● Tuple: Ordered, immutable sequence; cannot modify after creation.
● 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
def
triangle_area(base, height):
return
0.5
* base * height
def
deleteChar(string, ch):
return
string.replace(ch,
""
)
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)
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
def
longest_word(words):
return
max
([
len
(word)
for
word
in
words])
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
def
add(a, b):
# formal
return
a + b
add(
2
,
3
) # actual
python
def
func(a, b=
10
):
# b is default
return
a + b
func(
5
)
# uses default 10
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
-- Convert to uppercase
SELECT
UPPER(
"informatics practices"
);
-- Convert to lowercase
SELECT
LOWER(
"COMPUTER SCIENCE"
);
Mathematical Functions
-- 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
Current Date/Time
sql
Aggregate Functions
SELECT
MAX(Marks)
FROM
STUDENT;
SELECT
MIN(Marks)
FROM
STUDENT;
SELECT
AVG(Marks)
FROM
STUDENT;
SELECT
SUM(Marks)
FROM
STUDENT;
SELECT
Grade, AVG(Marks)
FROM
STUDENT
GROUP
BY
Grade
HAVING
AVG(Marks) >
75
;
Practical Exercise Questions
-- Create table
CREATE
TABLE
PRODUCT (
PCode
VARCHAR
(
10
)
PRIMARY
KEY
,
PName
VARCHAR
(
50
) NOT NULL,
UPrice
DECIMAL
(
1
0
,
2
),
Quantity
INT
);