PYTHON_Datatype(PRACTICE)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How do you remove a reference to a variable?

using del keyword Explanation : You can delete a reference to an object using the del keyword.

What is the output of the function shown below? hex(15)

0xf Explanation : The function hex() is used to convert the given argument into its hexadecimal representation, in lower case. Hence the output of the function hex(15) is 0xf.

Write the output of the following code. L=[1,2,3,4,5]for i in L:print(i,end=" ")i=i+1

1, 2, 3, 4, 5

What is the output of the following code? lst=[11, 100, 101, 999, 1001] answer_1=lst[-1] print(answer_1)

1001 Explanation : In this code, we are using negative indexing, and so we are getting last element from that list.

What will set1|set2 do?

A new set will be created with the elements of both set1 and set2

How will you convert real numbers to complex numbers?

Complex()

How will you delete a key from Dictionary?

Using del keyword Explanation : we can use del keyword to delete a key from the dictionary.

Find the correct output of the following String operations. str1='Welcome'print(str1[:6] + ' to India')

Welcom to India Explanation : Here, we use slicing to get some part of string and then we use + operator for string combining.

What do you think my_library[104] would point to ? Assume that a dictionary has data in the form of { key1:value1, key2:value2...} my_library ={103 : "Alice in Wonderland",104 : "The Turning Point",113 : "Wings on Fire",134 : "Harry Potter"104 : "World of Dream"}

World of Dream Explanation : It will print the 104 value.

What is the output of the following code? l=[1,0,2,0,'hello','',[]]l2=list(filter(bool,l))print(l2)

[1,2,'hello']

What is the output of the following list operation? aList = [10, 20, 30, 40, 50, 60, 70, 80]print(aList[2:5])print(aList[:4])print(aList[3:])

[30, 40, 50][10, 20, 30, 40][40, 50, 60, 70, 80] Explanation : Python list collection is ordered and changeable. The list also allows duplicate members. To get a sublist out of the list, we need to specify the range of indexes. To get a sublist, we need to specify where to start and where to end the range.

What will be the correct output for the following code? listnum = [45,67,12,14,56,87]list =listnum[::-1]print (list)

[87, 56, 14, 12, 67, 45] Explanation : Here,we are using negative indexing to reverse a list.

What is the output of following code snippet? l1 = [1, 2, 3]l2 = l1l3 = l1.copy()l4 = list(l1)l1[0] = [7]print(l1, l2, l3, l4)

[[7], 2, 3] [[7], 2, 3] [1, 2, 3] [1, 2, 3]

What is the data type of print(type(0xFF))?

int

The minsplit parameter to split() specifies the minimum number of splits to make to the input string.

False

What is the output of the following code? print(bool(0), bool(3.14159), bool(-3))

False True True Explanation : If we pass zero value to bool() constructor, it will treat it as False. Any non-zero value is boolean True.

If string1="Welcome to my Dream World" What does string1.find("m") return?

It returns the index position of first occurance of "m" in the string string1.

Which character will have same index value when you access elements from the beginning and from the end(ignore the negative sign) in the following string? Data = "Welcome to my Dream World"

'o'

Predict the output of the following code? x = 80def func1():x = 25print(x)func1()print(x)

25 80 Explanation : A variable declared outside of all functions has a GLOBAL SCOPE. Thus, it is accessible throughout the file. And variable declared inside a function is a local variable whose scope is limited to its function.

What is the output of the following list operation? sampleList = [10, 20, 30, 40, 50]print(sampleList[-2])print(sampleList[-4:-1])

40[20,30,40] Explanation : Use the range of negative indexes to search from the end of the list.

What is the output of the following code? a = 7b = -8x = complex(a,b)print(x)print(x.real)print(x.imag)

7-8j 7.0 -8.j

Predict the output of the following code? set1 = {10, 20, 30, 40, 50}set2 = {30, 40, 50, 60, 70}print(set1.intersect(set2))

AttributeError Explanation : 'set' object has no attribute 'intersect'.

What is the boolean data type?

Built-in data type

What is the output of below code? tuple1 = (10, 20, 30, 40, 50)tuple1.pop(2)print(tuple1)

Error Explanation : The reason is Tuple is immutable in python i.e we cannot change the values of a tuple.

What is the output of the following code? print( (1.1 + 2.2) == 3.3 )

FALSE Explanation : (1.1 + 2.2) it is not equal to 3.3, it is 3.3000000000000003.Use the round() function to compare exact values.

What is the output of the following code? dictlang = {'To': 6, 'GO': 89, 'Python': 4,'Rules':10}cpydict = dictlang.copy()print(id(cpydict) == id(dictlang))

FALSE Explanation : After copying the dictionary values into another dictionary,it will change the id values.

State True or False: We can convert the complex into int datatype.

FALSE Explanation : Explanation:We can convert any type to int type, but we cannot change complex to int type.

State True or False: A set be accessed by index?

FALSE Explanation : Set can't be accessed by index, and if your trying to access a set by index, it will throw an error.

State True or False:"We can convert complex numbers to any other number type".

FALSE Explanation : We can't convert complex numbers to any other number type as python will give you TypeError.

delete is a keyword in python ?

FALSE Explanation : del is the keyword in python not delete.

What is the output of the following code? set1={8,9}set2={5,6}set3=set()i=0j=0for i in set1:for j in set2:set3.add((i,j))i+=1j+=1print(set3)

{(9, 5), (9, 6), (10, 6), (8, 5)}

What is the output of the following code? my_list = ["Hello", "Python"]print("-".join(my_list))

Hello-Python Explanation : The join() method will join all items in a list into a string, using a hyphen character as a separator.

Which of the following two Python codes will give same output? (i) print(tupl[:-1])(ii) print(tupl[0:5])(iii) print(tupl[0:4])(iv) print(tupl[-4:]) If tupl=(5,3,1,9,0)

i, iii

What is the output of the following program? print(type(0xFF))

int Explanation : We can represent an integer in binary,octal and hexadecimal formats. 0b or 0B for Binary and base is 2 0o or 0O for Octal and base is 8 0x or 0X for Hexadecimal and base is 16

How will you convert float value 12.6 to integer value?

int() Explanation : Float value can be converted to an integer value by calling int() funtion.

In Python 3, what is the output of type(range(5))? (What data type it will return).

range Explanation : In python 3, the range() function returns range object,not list.

what will be the output of the below code? type(range(5))

range() Explanation : In Python 3, the range() function returns range object, not list.

What is the output of following code? tuple1 = (10,)print(tuple1 * 3)

(10, 10, 10) Explanation : We can use * operator to repeat the values of tuple n number of times.

Predict the output of the code. tuple1 = 25, 20, 15, 10, 5 a, b, c, d, e = tuple1print(b)

20 Explanation : In python, we can create a tuple without parentheses. Also tuple unpacking is possible.

What will be the output of the following code? tuple=(45,2,90,5,36,65)output=sum(tuple)print("sum of the tuple:",output)

243

What error will occur when you execute the following code? MANGO = APPLE

NameError Explanation : Mango is not defined hence the name error

What will be the output of the following code? def func1():y = 50return yfunc1()print(y)

NameError Explanation : You will get an error as NameError: name 'y' is not defined. To access the function's return value we must accept it using an assignment operator like this.

What is the output of the following code? a=50type(a)

No output Explanation : Because we didn't use the print() function to print the type of the variable.

Which method is used to exchange one string with another string?

None of the above Explanation : In python, we can use replace() function to replace one character with another character.

How to add an element to a Tuple?

None of the above Explanation : Tuples are immutable in python we cannot change the values of items directly using append() or extend().

What is the output of Following code snippet? from numbers import Numberfrom decimal import Decimalfrom fractions import Fractionprint(isinstance(2.0, Number))print(isinstance(Decimal('2.0'), Number))print(isinstance(Fraction(2, 1), Number))print(isinstance("2", Number))

True True True False

Choose the correct way to access value 20 from the following tuple. Tuple = ("Software",[10, 20, 30], (5, 15, 25))

Tuple[1:2][1] Explanation : If we want to access the value 20 from tuple we can use slicing.

What is the output of following code snippet? set1={1,2,3,4,5,6,7,8,9,10}print(set1[5])

Type Error Explanation : TypeError: 'set' object is not subscriptable

How will you remove items 10, 20, 30 from the set at once? set1 = {10, 20, 30, 40, 50}

difference_update() Explanation : We can use difference_update() method to remove items at once.

What is the output of the following code? str1 = "Revature"print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])

eva Rvat tur Rvatur Rvatur

Predict the output of the following code. str="Software engineer"print(str[2 : 10 : 2])

fw Explanation : In the above program, we are using `slicing` operator and this is used to cut the strings based on the values.

Please select the correct expression to reassign a global variable "x" to 20 inside a function fun1(). x = 50def fun1():# your code to assign global x = 20fun1()print(x)

global x x=20 Explanation : Option C is the correct answer,because other declarations are not the valid syntax for global variable declarations.

Predict the output of the code. print(['hello','morning'][bool('')])

hello Explanation : In the above code, as nothing is passed to boolean function therefore it is printing "hello". If anything is passed to the boolean function it will print "morning".

What is the output of following code snippet? Data = ['Sudip','Shubham','Priya','Aman']print(Data[-1][-1])

n

What is the output of the following code? s1="IndiaTraining people"s2=""s3=""for x in s1:if(x=="s" or x=="n" or x=="p"):s2+=xprint(s2,end=" ")print(s3)

nnnpp Explanation : In the above program, it will check whether s ,n and p characters are in the string or not. If it is there, it will print those characters.

Predict the output of following code. list1 = ['saran', 'shalini', 'sandy']print (max(list1))

shan Explanation : max() function in python returns the element with the highest value from an iterable. But if the elements of iterable[list, tuple, etc] are strings, then it compares alphabetically and returns the maximum from them. In this code, the first letter of each word is 's', so it checks the 2nd letter of each word and as 'h' is a greater value then 'a', it returns shan as output.

What is the output of following code snippet? aTuple = ("Mangoes")print(type(aTuple))

str

How will you add list of items into a Set?

update() Explanation : In python, we can use the update() method of a set, to add list of items into a set.

What is the output of following code snippet? list1=[1,4,2,6,10,2,28,16,2,15,10]x=list1.pop(2)print(set([x]))

{2} Explanation : In the above, x variable is storing the removed value of 2nd index from the list which is 2 and last print statement is returning the removed item as a set.

What is the output of following code snippet? aTuple = (100, 200, 300, 400, 500)aTuple[1] = 800print(aTuple)

TypeError

What is the output of following code snippet? str = "Learning"print(str[10])

IndexError

What is the output of following program? d={(3,4,8):4,(5,6,9):3}print('dictionary output:' ,d[3,4,9])

KeyError Explanation : It will throw a key error because, keys are not found in the dictionary.

del statement can delete which of the following from the List?

Single Element

How do you know if every word in a string that starts with a capital letter?

istitle() Explanation : In python, istitle() function is used to check whether the given string starts with a capital letter or not.

Predict the output of following code. list1 = ["Hello", [2, 5, 7, 9, 3]]print(list1[0][-3])print(list1[1][2])

l7 Explanation : First, print statement picks the 0th index value which is "Hello", after that for -3 index iterates over hello from the last and prints the 'l' letter from "Hello". The 2nd print statement picks the 1st index value from the list which itself is a list of [2, 5, 7, 9, 3] values, after that it finds the element at index 2 from that list and print its value which is 7. Hence, l and 7 will be printed.

Which of the following is used to create an empty set?

set() Explanation : {} is of class dictionary (print(type({})) try this code for more clarity) and set() is empty set.

What is the output of the following code? input_string ="Consultancy"reversed_str =''.join(reversed(input_string))print(reversed_str)

ycnatlusnoC Explanation : In python, to reverse a string, we can use built-in function reversed() or else we can use negative indexing ([::-1]).


Set pelajaran terkait

Chapter 34: Digestive Systems and Nutrition

View Set

ServeSafe Chapter 9: The Flow of Food: Service

View Set

BAR. All Subject MBE Questions (that I got wrong)

View Set