Here is a python program that prints student details. The student details are first name, last name, student id. The tricky of this program is it uses the class.
Python supports object-oriented programming. So you can write classes in python fearlessly. But the point to recollect is supply of self argument (that is mandatory; says current object).
What you’ll see in this post
- Sample Python program: It tells how to write class to display student details.
- Execution of the program: you’ll see the output of the program.
Sample Python program
Here I’ve defined two methods. Those are init and str. And I have supplied self as argument. At line number4, here defined init, at line number8 defined str.
Both are internal methods. In the init method, I did initilization. In the str mthod, I used inilized varibale to concatenate the string.
- Let us say, by mistake you typed init method/str method name incorrrectly, then you’ll get Type error
- The supplied self argument in the str method helps to get initilized variables
- By default in all the methods you need to supply self argument
Execution of the program
So in the result, you can see concatented student details. This is useful for your project ansd interviews.