# Using Classes in Python

In 
Published 2022-12-03

This tutorial give you some example of using Classes in Python.

Defining and using CLASSES in Python is very easy. Here is an example:

Example
#Class definition
class Employee:
 
   def __init__(self, name, position, salary = 1000):
      self.name = name;
      self.position = position;
      self.salary = salary;
 
   def charCountName(self):
      count1 = 0
      for i in self.name:
         count1 = count1 + 1;
 
      return count1;
 
 
emp1 = Employee('Paul', 'accountant');
print(emp1.name)
print(emp1.salary)
 
print('---------------')
emp1 = Employee('Dan', 'manager', 2500);
print(emp1.name)
print(emp1.salary)
 
emp1.name = "John"
print(emp1.name)
print(emp1.salary)
print(emp1.charCountName())

And here you can see the execution of the Python code in PyCharm:

This code define a function named "charCount" into the Employee class. This function could be called with one parameter.

Into the class definition you can see a function named __init__. This is the constructor of the Employee class.

You can access directly the value of a class attribute, but this is not considered a good practice.