DEV Community

Saravanan Cn
Saravanan Cn

Posted on

# My Python Learning Journey – Understanding Python Fundamentals 🐍

Introduction

Today, I continued my Python learning journey and explored some important Python fundamentals.

As a beginner, I learned how objects, functions, methods, variables, and attributes work. I also explored function overloading, naming conventions, and PEP 8.

Let's look at what I learned today with simple examples.

1. What is an Object?

An object is an instance of a class.

In Python, almost everything is an object, including integers, strings, lists, and functions.

Example:

name = "Python"
age = 25

print(type(name))
print(type(age))
Enter fullscreen mode Exit fullscreen mode

Here, "Python" is a string object and 25 is an integer object.

2. What is a Function?

A function is a reusable block of code that performs a particular task.

We use the def keyword to define a function.

Example:

def greet():
    print("Hello, Welcome to Python!")

greet()
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Welcome to Python!
Enter fullscreen mode Exit fullscreen mode

Here:

  • def is the keyword used to define a function.
  • greet is the function name.
  • greet() calls the function.

3. What is a Method?

A method is a function associated with an object or class.

Example:

name = "python"

print(name.upper())
Enter fullscreen mode Exit fullscreen mode

Output:

PYTHON
Enter fullscreen mode Exit fullscreen mode

Here, upper() is a string method that converts lowercase letters into uppercase letters.

4. What is a Variable?

A variable is a name that refers to an object.

Example:

name = "Sam"
age = 25

print(name)
print(age)
Enter fullscreen mode Exit fullscreen mode

Here:

  • name refers to a string object.
  • age refers to an integer object.
  • = is the assignment operator.

5. What is an Attribute?

An attribute is a name associated with an object or class.

Attributes can store data or represent behavior.

Example:

class Student:

    def __init__(self, name, age):
        self.name = name
        self.age = age

s1 = Student("Sam", 25)

print(s1.name)
print(s1.age)
Enter fullscreen mode Exit fullscreen mode

Here:

  • Student is a class.
  • s1 is an object (instance).
  • self refers to the current instance.
  • self.name and self.age are instance attributes.
  • __init__() is an initializer method.

6. What is an Instance?

An instance is an individual object created from a class.

Example:

class Student:
    pass

s1 = Student()
s2 = Student()
Enter fullscreen mode Exit fullscreen mode

Here, s1 and s2 are two separate instances of the Student class.

A simple way to remember:

  • Class = Blueprint
  • Instance = Individual object created from that blueprint.

7. Class Attributes vs. Instance Attributes

Class Attribute

A class attribute is defined inside a class but outside its methods. It is shared by instances by default.

Instance Attribute

An instance attribute belongs to an individual object.

Example:

class Student:

    school = "ABC School"

    def __init__(self, name):
        self.name = name

s1 = Student("Sam")
s2 = Student("David")

print(s1.school)
print(s2.school)
Enter fullscreen mode Exit fullscreen mode

Output:

ABC School
ABC School
Enter fullscreen mode Exit fullscreen mode

Both objects access the same class attribute.

Changing a class attribute through an object

s1.school = "XYZ School"

print(s1.school)
print(s2.school)
Enter fullscreen mode Exit fullscreen mode

Output:

XYZ School
ABC School
Enter fullscreen mode Exit fullscreen mode

Here, Python creates an instance attribute named school for s1. It does not change the original class attribute.

Changing the class attribute

Student.school = "DEF School"

print(s1.school)
print(s2.school)
Enter fullscreen mode Exit fullscreen mode

Output:

XYZ School
DEF School
Enter fullscreen mode Exit fullscreen mode

The s1 object continues using its own instance attribute, while s2 accesses the updated class attribute.

8. What is Function Overloading in Python?

Function overloading means using the same function name with different parameters.

Python does not support traditional function overloading directly. If we define multiple functions with the same name, the latest definition replaces the previous one.

We can achieve similar behavior using default arguments and *args.

Using default arguments

def add(a, b, c=0):
    print(a + b + c)

add(10, 20)
add(10, 20, 30)
Enter fullscreen mode Exit fullscreen mode

Output:

30
60
Enter fullscreen mode Exit fullscreen mode

Using *args

def add(*args):
    print(sum(args))

add(10, 20)
add(10, 20, 30)
Enter fullscreen mode Exit fullscreen mode

Output:

30
60
Enter fullscreen mode Exit fullscreen mode

*args allows a function to accept multiple positional arguments.

9. Naming Conventions in Python

Naming conventions are guidelines used to name variables, functions, classes, and constants.

Snake Case

Words are separated by underscores.

student_name = "Sam"

def calculate_total():
    pass
Enter fullscreen mode Exit fullscreen mode

Commonly used for variables and functions.

Pascal Case

Each word starts with a capital letter.

class StudentDetails:
    pass
Enter fullscreen mode Exit fullscreen mode

Commonly used for class names.

Camel Case

The first word starts with a lowercase letter, and subsequent words start with uppercase letters.

studentName = "Sam"
Enter fullscreen mode Exit fullscreen mode

It is valid Python, but snake_case is preferred for variables and functions.

Upper Snake Case

All letters are uppercase, separated by underscores.

MAX_SIZE = 100
DATABASE_NAME = "StudentDB"
Enter fullscreen mode Exit fullscreen mode

Commonly used for constants.

Leading Underscore

A single underscore at the beginning indicates that a name is intended for internal use.

_internal_value = 100
Enter fullscreen mode Exit fullscreen mode

This is a convention, not strict access control.

10. What is PEP 8?

PEP stands for Python Enhancement Proposal.

PEP 8 is the Python style guide that provides recommendations for writing clean, readable, and consistent Python code.

Some important guidelines:

  1. Use four spaces for indentation.
  2. Use snake_case for variables and functions.
  3. Use PascalCase for class names.
  4. Use UPPER_SNAKE_CASE for constants.
  5. Use spaces around operators.
  6. Separate functions and classes using blank lines.
  7. Keep code lines within the recommended length.

Example

Without following PEP 8:

def calculateTotal(a,b):
    total=a+b
    print(total)
Enter fullscreen mode Exit fullscreen mode

Following PEP 8:

def calculate_total(a, b):
    total = a + b
    print(total)
Enter fullscreen mode Exit fullscreen mode

The second example is easier to read and follows common Python style recommendations.

Conclusion

Today, I explored several important Python concepts, including objects, functions, methods, variables, attributes, instances, and class attributes.

I also learned about function overloading, naming conventions, and PEP 8.

Understanding these fundamentals is helping me build a strong foundation in Python.

I'm continuing my learning journey step by step and sharing my progress with the developer community.

Top comments (0)