Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
community creator

Class objects vs. variables

Bhanu Anagani

Class

  • Python is an Object-Oriented Programming Language.
  • A Class is used to create User-Defined Data Structures.
  • Classes have data members and define methodsidentify the behavior and actions that can be performed with its data members.
  • Object is the unique instance of a class.

How to create a class and object

class ClassExample:
  def __init__(self, collegeName):
    self.collegeName = collegeName

  def display(self):
    print("My college name is " + self.collegeName)

object = ClassExample("GITAM")
object.display()
Example for class

Variables

  • A variable is a container used to store data values.
  • There is no need to specify a data type to declare a variable.
  • It will consider the corresponding data types according to the value that you are assigning to the variable.
code = 12
name = "GITAM"
branch = "CSE"
location = "Visakhapatnam"
print(str(code) + " " + name + " " + branch + " " +location)
Sample program for variables

Casting

  • Casting is used to convert a variable value from one data type to another.
  • It is also used to specify the type of the variable.
rollno = str(121710303001)
name = "Aditya"  
cgpa = int(7.87)    
per = float(9)

print("Rollno : " , rollno )
print("Name :" , name)
print("CGPA :" , cgpa)
print("Percentage : ", per)
Example for type casting

How to check the datatype of a variable

You can get the data type of a variable using the type() function.

rollno = 121710303001
name = "Aditya"  
cgpa = 7.87
  
print(type(rollno))
print(type(name))
print(type(cgpa))
Program to display the type of variable

RELATED TAGS

python
community creator
RELATED COURSES

View all Courses

Keep Exploring