0% found this document useful (0 votes)
172 views1 page

Python Output and Error Analysis

The document contains 8 questions about Python concepts like dictionaries, tuples, strings, and lists. The questions test knowledge of indexing, slicing, built-in functions, and manipulating dictionary values and keys.

Uploaded by

jeyadev12345
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)
172 views1 page

Python Output and Error Analysis

The document contains 8 questions about Python concepts like dictionaries, tuples, strings, and lists. The questions test knowledge of indexing, slicing, built-in functions, and manipulating dictionary values and keys.

Uploaded by

jeyadev12345
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
  • Python Exercises
  • Python Code Exercises

I.

Answer the following questions:


1. What will be the output for the following Python statements?
D = {“Amit”: 90, “Reshma”: 96, “Suhail”: 92, “John”: 95}
print(“John” in D, 90 in D, sep= “#”)
2. Identify the errors in the following code:
MyTuple1=(1, 2, 3) #Statement1
MyTuple2=(4) #Statement2
[Link](4) #Statement3 print(MyTuple1,
MyTuple2) #Statement4
3. Suppose str= ‘welcome’. All the following expression produce the same result
except one. Which one?
(a) str[ : : -6] (b) str[ : : -1][ : : -6] (c) str[0] + str[-1] (d) str[ : :6]
4. Consider the following code:
dict1 = {‘Mohan’: 95, ‘Ram’: 89, ‘Suhel’: 92, ‘Saritha’: 85}
What suitable code should be written to return a list of values in dict1 ?
5. Find the correct output of the following code:
>>>str1 = ‘India is a Great Country’
>>>[Link](‘a’)
6. What will be the output of the following Python code?
d1={“a”:10, “b”:2, “c”:3}
str1= “ ”
for i in d1:
str1=str1+str(d1[i])+ “ ”
str2=str1[ : -1]
print(str2[: : -1])
7. Given is a Python string declaration:
str1="!!Welcome to Python!!"
Write the output of: print(str1[::-2])
8. Write the output of the code given below:
dict1 = {"name": "Suman", "age": 36}
dict1['age'] = 27
dict1['address'] = "Chennai"
print([Link]())

Common questions

Powered by AI

The 'split' method with 'str1.split('a')' will divide the string 'str1' at each occurrence of the character 'a', excluding 'a' from the resulting segments. The output would be the list '['Indi', ' is ', ' Gre', 't Country']'.

When iterating through 'd1' and concatenating the values '10', '2', and '3' into a string with space separators, the resultant string is '10 2 3 '. Reversing this string (while removing the trailing space) results in '3 2 01'.

The error in 'MyTuple1.append(4)' is that tuples are immutable and do not have an 'append' method; trying to append will result in an AttributeError. The assignment 'MyTuple2=(4)' would create an integer, not a tuple, because it lacks a trailing comma. It should be 'MyTuple2=(4,)' to define a single-element tuple.

Modifying the value for 'age' and adding a new key-value pair ('address', 'Chennai') updates 'dict1', but it does not affect the keys returned by 'dict1.keys()'. The keys remain ['name', 'age', 'address'], reflecting all current key names.

Reversing the string 'str1="!!Welcome to Python!!"' with stride '-2' skips every other character starting from the last one. The output is '!nh otme!'.

The expression 'str[0] + str[-1]' does not produce the same result as the others. It concatenates the first and last characters of the string, resulting in 'we', while the other expressions involve slicing with different steps and would not produce this same outcome.

To retrieve a list of all values in 'dict1', use the 'dict.values()' method which returns a view object of all values. You can convert this view into a list using 'list(dict.values())', resulting in '[95, 89, 92, 85]'.

The 'in' operator checks if a key is present in the dictionary. Therefore, '"John" in D' evaluates to True because 'John' is a key in the dictionary 'D', but '90 in D' evaluates to False because '90' is a value, not a key. The output is 'True#False'.

I. Answer the following questions:  
 
 
 
 
 
 
1. What will be the output for the following Python statements? 
D = {“Amit”

You might also like