Class objects vs. variables

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()

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)

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)

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))

Free Resources