0% found this document useful (0 votes)
6 views7 pages

Python Code Snippets and Outputs

The document contains answers to various computer science programming snippets, including outputs for arithmetic operations, loops, conditionals, and function definitions. It also includes matching of number literals and demonstrates the use of built-in functions. The answers are structured in a clear format, providing expected results for each code snippet.

Uploaded by

insub985
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)
6 views7 pages

Python Code Snippets and Outputs

The document contains answers to various computer science programming snippets, including outputs for arithmetic operations, loops, conditionals, and function definitions. It also includes matching of number literals and demonstrates the use of built-in functions. The answers are structured in a clear format, providing expected results for each code snippet.

Uploaded by

insub985
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

12 – COMPUTER SCIENCE

LESSONS – 5, 6 & 7

என்றும் மாணவர்கள் நலனில்

WRITE THE OUTPUT OF THE FOLLOWING SNIPPETS. / பின்வரும் குறிமுறைகளின் வெளியீட்டை எழுதுக.

1) >>> 2  5

2) >>>100//3

3) print(10/3)
print(10//3)

4) a,b=10,3
x1=a if (a/b==3) else b
print(x)
x2=a if (a//b==3) else b
print(x)

5) Match the following: / ப ொருத்துக.


a) 0b1010 - i) Octal Literals
b) 100 - ii) Hexadecimal Literal
c) 0o310 - iii) Binary Literal
d) 0x12c - iv) Decimal Literal

6) >>> print("\"Python\"")

7) a = -5
x="even" if a%2==0 else "odd"
print (a, " is ",x)

8) i=10
while (i<=15):
print (i,end='\t')
i=i+1

..1.. A. PRABHAKAR - 9442979144


9) i=10
while (i<=15):
i=i+1
print (i,end='\t')

10) for x in (1,2,3,4,5):


print("Tech Easy")

11) for x in (1,2,3,4,5):


print(x, end=' ')

12) n = 10
sum = 0
for counter in range(1, n+1):
sum = sum + counter

print("Sum of 1 until %d: %d" % (n, sum))

13) for x in range(5):


if x==2:
continue
print(x, end='')

14) for i in range(1,10,2):


print(i, end='')

15) i=1
while (i<=6):
for j in range (1,i):
print (j,end='\t')
print (end='\n')
i += 1

16) i=1
while (i<=6):
for j in range (1,i):
print (‘$’,end='\t')
print (end='\n')
i += 1

..2.. A. PRABHAKAR - 9442979144


17) i=1
while True:
if i%5 == 0:
break
print(i,end=' ')
i +=1

18) for cs in “tech easy”:


if cs=='e':
pass
print(cs, end='')

19) for sub in “computer science”:


if sub=="e":
continue
print(sub, end='')

20) def hello():


print ("hello - Python")
return
print (hello())

21) def printinfo(name, salary = 3500):


print ("Name: ", name)
print ("Salary: ", salary)
return
printinfo("Mani")

22) def printnos (*nos):


for n in nos:
print(n)
return
printnos (1,2,10,30,7)

23) c = 1
def add():
c = c + 2
print(c)
add()

..3.. A. PRABHAKAR - 9442979144


24) c = 1
def add():
global c
c = c + 2
print (c)
add()

25) x = 0
def add():
global x
x = x + 5
print ("Inside add() function x value is :", x)
add()
print ("In main x value is :", x)

26) print ('A = ',ord(‘A’))


print(chr(97))
print (type(15.2))

27) MyList = [-21,-76,-98,-23]


print ('Maximum of MyList :', max(MyList))

28) n1=17.89
print (round (n1))
print (round (n1,0))
print (round (n1,1))
print (round (n1,2))

29) x=26.7
print([Link](x))
print([Link](x))

30) print(format(66, 'c'))


print(format(10, 'x'))
print(format(10, 'X'))
print(format(0b110, 'd'))
print(format(0xa, 'd'))

..4.. A. PRABHAKAR - 9442979144


31) print(pow(3,2))
print(pow(-3,2))
print(pow(-3,3))

32) print(abs(-8))
print(abs(8))

..5.. A. PRABHAKAR - 9442979144


12 – COMPUTER SCIENCE
LESSONS – 5, 6 & 7 (18/01/2025)

என்றும் மாணவர்கள் நலனில்


ANSWERS
1) 32
2) 33
3) a) 0b1010 - iii) Binary Literal
b) 100 - iv) Decimal Literal
c) 0o310 - i) Octal Literals
d) 0x12c - ii) Hexadecimal Literal
4) "Python"
5) -5 is odd
6) 10 11 12 13 14 15
7) 11 12 13 14 15 16
8) Tech Easy
Tech Easy
Tech Easy
Tech Easy
Tech Easy
9) 1 2 3 4 5
10) Sum of 1 until 10: 55
11) 0134
12) 13579
13) 1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

14) $
$ $
$ $ $
$ $ $ $
$ $ $ $ $

..6.. A. PRABHAKAR - 9442979144


15) 1 2 3 4
16) tech easy
17) computr scinc
18) hello - Python
None
19) Name: Mani
Salary: 3500
20) 1
2
10
30
7
21) ERROR!
22) 3
23) Inside add() function x value is : 5
In main x value is : 5
24) A = 65
a
<class 'float'>
25) Maximum of MyList : -21
26) 18
18.0
17.9
17.89
27) 26
27
28) B
a
A
6
10
29) 9
9
-27
30) 8
8

..7.. A. PRABHAKAR - 9442979144

Common questions

Powered by AI

Using 'global' allows a function to modify a variable defined outside its scope. In Source 1, the 'add' function is able to modify x by declaring 'global x', thereby reflecting changes outside the function, whereas without 'global', the variable is treated as local, leading to error or no change if used uninitialized.

The output will be 3. The expression (a/b == 3) evaluates to False (since 10/3 is not equal to 3), thus x1 is assigned the value of b, which is 3.

The printed result of executing hello() will be "hello - Python" followed by "None". The function contains a print statement and returns the output of calling print, which is None.

The output is 0134. The 'continue' statement skips the execution for x=2, thus omitting it from the printed sequence.

When 'printinfo' is called with 'Mani', it uses the default salary value of 3500, illustrating that parameters with defaults allow function calls to omit specifics if default values are sufficient. Here, 'Name: Mani' and 'Salary: 3500' are printed because the function substitutes default for the omitted 'salary' parameter.

Python rounds -17.89 to -18 when no decimal place is specified. The rounding function follows standard rounding rules, rounding away from zero when the fraction is .5 or greater.

The literal 0b1010 is a binary literal representing the decimal value 10, while 0o310 is an octal literal representing the decimal value 200.

The calculated sum is 55. The for loop iterates through numbers 1 to 10, accumulating their sum in a variable, as shown by 'Sum of 1 until 10: 55'.

The 'pass' statement acts as a no-op placeholder, filling in the need for a syntactically required block without altering the flow. When iterating over 'tech easy', it skips the operation but progresses the loop normally, resulting in the output 'tch asy'.

Using '/' results in 10/3=3.3333, which is a floating point division giving the approximate decimal quotient. Using '//' results in 10//3=3, which is floor division providing the integer quotient only.

You might also like