Create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
Question
Create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
Solution
Sure, here is how you can create a class named 'Student' with a string variable 'name' and an integer variable 'roll_no'. Then, assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
# Define the class
class Student:
def __init__(self, name, roll_no):
self.name = name
self.roll_no = roll_no
# Create an object of the class Student
student1 = Student("John", 2)
# Now, the object student1 has name as "John" and roll_no as 2
print(student1.name) # Output: John
print(student1.roll_no) # Output: 2
In this code, __init__ is a special method in Python classes, which is automatically called when an object of the class is created. It is used to initialize the attributes of the class.
Similar Questions
Create a class called "Student" with private member variables for name, roll number, and marks in three subjects. Include public member functions to set the student details, calculate the average marks, and display the student detail
ssign and print the roll number, phone number and address of two students having names "Sam" and "John" respectively by creating two objects of the class 'Student'.
Get a count of number of students. Get the input details from each student like 'name', 'address', ‘class number’ and ‘department’ till that particular count is reached.In the names of the student where the pattern “in” occurs. Replace it by ‘$$’.Using static member functions/static variable, assign a unique roll number to each student.Display all the student details. Implement the above process using java class and constructors.Ex: Stalin (pattern “in”)
In a school, each student is identified by a unique combination of a class section and a roll number. Write a program that takes input for the class section (either uppercase or lowercase character) and the roll number (an integer). The program should then output a message indicating the student's roll number in the specified class section.Input format :The first line of input is a character c, representing the class section (A-Z or a-z).The second line of input is an integer d, representing the roll number.Output format :The output displays a message indicating the roll number and class section in the format: "Roll number d in Section c".Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases will fall under the following constraints:1 ≤ d ≤ 50The class section must be a letter in either uppercase or lowercase.Sample test cases :Input 1 :A30Output 1 :Roll number 30 in Section AInput 2 :D50Output 2 :Roll number 50 in Section DInput 3 :z25Output 3 :Roll number 25 in Section z
Create a base class Person and a derived class Student with Person as its base class.Add two methods setname() (which takes the parameter self and name)and getname() which prints the name in the base class.Add two methods in the derived class: setage() (which takes the parameters self and age) which sets the age and getage() which prints the age.Create an instance of Student and name it as s1.Take name and age as inputs from the console.Call the setname() and setage() on this instance by passing the name and age parameters.Call the getname() and getage() on this class, which prints the passed parametersNotice that we have created an object of derived class but it could access the methods of base class.This is inheritance.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.