What is the output?
from collections import namedtuple
Student = namedtuple('Student' , ['name','age','rollNo'])
student1 = Student('Hayden' , 26, 6)
print
print ("Name:", student1.name)
print
print ("Age:", student1.age)
print
print ("Roll Number:", student1.rollNo)
print
Group of answer choices
Name: Hayden Age: 26 Roll Number: 6
Hayden 26 6
6 Hayden 26
Age: 6 Roll Number: Hayden Name: 26
Ans:-
Output of the above program is:- Name: Hayden Age: 26 Roll Number: 6
Explaination:-
The above program is a python program which is implementing namedtuple class from collection. Namedtuple is like a dictionary which contains key and value. The values can be accessed using index or attribute or getattr() method.
In the above program we are using attribute to access the elements.Now, let us go through the lines of code
from collections import namedtuple # this will import the namedtuple from collections in the program
Student = namedtuple('Student' , ['name','age','rollNo']) # this will create student namedtuple where name, age and rollNo are attribute
student1 = Student('Hayden' , 26, 6) # this will add a new student
print
print ("Name:", student1.name) # this method print the name
of student using name attribute
print
print ("Age:", student1.age) # this method print the age
of student using age attribute
print
print ("Roll Number:", student1.rollNo) # this method
print the roll no of student using rollNo attribute
print
So, in the above code, only three statements will be printed and this will be the output of the program, first is name of student using name attribute, second is age of student using age attribute and third is roll number of student using rollNo attribute. Hence, the output will be Name: Hayden Age: 26 Roll Number: 6
Get Answers For Free
Most questions answered within 1 hours.