Answer to Question #186784 in Python for desmond

Question #186784
. Type the expressions below in python interactively, and try to explain what's
happening in each case:
a. 2 ** 16
2 / 5, 2 / 5.0

b. "spam" + "eggs"
S = "ham"
"eggs " + S
S * 5
S[:0]
"green %s and %s" % ("eggs", S)
c. ('x',) [0]
('x', 'y') [1]

d. L = [1,2,3] + [4,5,6]
L, L[:], L[:0], L[-2], L[-2:]
([1,2,3] + [4,5,6]) [2:4]
[L[2], L[3]]
L.reverse(); L
L.sort(); L
L.index(4)
1
Expert's answer
2021-04-28T15:49:45-0400

a.


2**16  #2 is raised to the power of 16, the output will be 65536
2 / 5 #2 is divided by 5, output:0.4
2 / 5.0 #2 is divided by 5.0, output: 0.4


b.

"spam" + "eggs" #adds the two strings, spam and eggs. Output: spameggs

S = "ham"  #initialize S to 'ham'

"eggs " + S #add 'eggs ' to the value of S. Output: egg ham

S * 5  #multiply the value of S by 5. Output: hamhamhamhamham

c=S[:0] #return the elements of S upto index 0
"green %s and %s" % ("eggs", S) #prints green eggs and ham


c.


('x',) [0]  #output the first element, x
('x', 'y') [1] #output the second element, y


d.


L = [1,2,3] + [4,5,6] #combine the two lists. Output [1,2,3,4,5,6]

L #prints the whole list
L[:] #prints the whole list
L[:0] #prints an empty list
L[-2] #prints the second last element in the list
L[-2:] #prints the last two elements in the list

([1,2,3] + [4,5,6]) [2:4] #add the two lists and prints the elements from index 2 to 4

[L[2], L[3]] #prints a list consisting of element at 2 and 3 indixes. Output [3,4]

L.reverse(); L #reverse the list

L.sort(); L #sort the list

L.index(4) #returns the index of element 4 in the list

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS