Python Class: Here’s Logic to Print Student Details

Here is a python class to print student details. The student details are first name, last name, student id.

Python Class

Python supports object-oriented programming. So you can write classes in python fearlessly. One thing to remember is ‘SELF’ argument is a must in the init method.

Here’s how to write and execute Python script

In two steps you will learn:

  1. Python program
  2. Execution of the program

1. Sample Python program

It tells how to write class to display student details.

python script to display student details using class
Sample python program

Here I’ve created two methods – init and str. In addition to that, I have supplied self argument. At the line, number4 defined init methods, and at line number8 defined str.

Both are internal methods. In the init method, I did initialization. In the str method, I used initialized variables to concatenate the string.

Learnings

  1. Let us say, by mistake you typed init method/str method name incorrrectly, then you’ll get Type error
  2. The supplied self argument in the str method helps to get initilized variables
  3. By default in all the methods you need to supply self argument

“The ability to observe without evaluating is the highest form of intelligence.”

― J. Krishnamurti, Author of Freedom from the known

2. Execution of the program

You’ll see the output of the program.

Output of python script. Here you'll get student details displayed.
Command to execute python script

So in the output, you can see concatenated student details (first-name, last-name and student id). Useful for your project and interviews.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.