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))
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()
Output:
Hello, Welcome to Python!
Here:
-
defis the keyword used to define a function. -
greetis 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())
Output:
PYTHON
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)
Here:
-
namerefers to a string object. -
agerefers 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)
Here:
-
Studentis a class. -
s1is an object (instance). -
selfrefers to the current instance. -
self.nameandself.ageare 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()
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)
Output:
ABC School
ABC School
Both objects access the same class attribute.
Changing a class attribute through an object
s1.school = "XYZ School"
print(s1.school)
print(s2.school)
Output:
XYZ School
ABC School
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)
Output:
XYZ School
DEF School
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)
Output:
30
60
Using *args
def add(*args):
print(sum(args))
add(10, 20)
add(10, 20, 30)
Output:
30
60
*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
Commonly used for variables and functions.
Pascal Case
Each word starts with a capital letter.
class StudentDetails:
pass
Commonly used for class names.
Camel Case
The first word starts with a lowercase letter, and subsequent words start with uppercase letters.
studentName = "Sam"
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"
Commonly used for constants.
Leading Underscore
A single underscore at the beginning indicates that a name is intended for internal use.
_internal_value = 100
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:
- Use four spaces for indentation.
- Use snake_case for variables and functions.
- Use PascalCase for class names.
- Use UPPER_SNAKE_CASE for constants.
- Use spaces around operators.
- Separate functions and classes using blank lines.
- Keep code lines within the recommended length.
Example
Without following PEP 8:
def calculateTotal(a,b):
total=a+b
print(total)
Following PEP 8:
def calculate_total(a, b):
total = a + b
print(total)
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)