Function & Class#
Function#
Definition with def keyword#
Format:
def function_name(param1, param2, ...):
# code block
return value # optional
All input parameters are local variable in the function, so the function will return None if you don’t specify a return value.
[1]:
def add(num1, num2):
return num1 + num2
Local & Global Variables#
Local Variables: Variables declared inside a function. They are accessible only within that function.
Global Variables: Variables declared outside any function. They are accessible throughout the code, including inside functions.
[2]:
x = "global"
def my_function():
x = "local"
print(x)
my_function() # Output: local
print(x) # Output: global
local
global
global keyword#
The global keyword is used to declare a variable inside a function as global, it allows the function to modify the variable outside its local scope.
[3]:
x = 10
def modify_global():
x = 20
modify_global()
print(x)
10
[4]:
x = 10 # Global variable
def modify_global():
global x # Declare x as global
x = 20 # Modify the global variable
modify_global()
print(x)
20
Strict Definition (Optional)#
Format: ```python def function_name(param1: type, param2: type, …) -> return_type: # code block return value # optional
[5]:
def add_numbers(a: int, b: int) -> int:
return a + b
result = add_numbers(3, 5) # Output: 8
print(result)
8
Class#
Definition#
Sections with
*are advanced contents.
Variables
instance variable
static variable *
Methods
instance method
built-in method
static method * : only access static variables
Example#
[6]:
class MyClass:
# static variable declaration
static_str = "I am a static variable"
# instance variable declaration
def __init__(self, value: list[int]):
self.val = value
# instance method
def get_val(self):
return self.val
# static method
@staticmethod
def modify_static_variable(new_val: str):
MyClass.static_str = new_val
# built-in method
def __len__(self):
return len(self.val)
# built-in method
def __str__(self):
return f"Value: {", ".join([str(i) for i in self.val])}"
[7]:
obj1 = MyClass([1, 2])
obj2 = MyClass([100, 200])
print(obj1.get_val())
print(obj2.get_val())
[1, 2]
[100, 200]
[8]:
MyClass.modify_static_variable("I am a new static variable")
print(MyClass.static_str)
I am a new static variable
[9]:
len(obj1)
[9]:
2
[10]:
print(obj1)
Value: 1, 2
OOP with Python (Advanced)#
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects,” which are instances of classes. These objects can contain data, in the form of fields (often known as attributes or properties), and code, in the form of methods (functions or procedures).
Here are the core concepts of OOP:
Encapsulation: The bundling of data (attributes) and methods that operate on that data into a single unit.
Inheritance: The mechanism by which one class acquires the properties and behavior of another class.
Polymorphism: The ability to present the same interface for different data types
Abstraction: The concept of hiding the internal implementation details and showing only the necessary features of an object.
Inheritance#
Inheritance allows a class to inherit attributes and methods from another class. The class that inherits is called the child class, and the class being inherited from is the parent class.
Child class could override the methods of the parent class and define new methods.
[11]:
class Parent:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} says hello. I am a parent."
class Child(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age
# override `speak` method
def speak(self):
return f"{self.name} says hello. I am a child."
def introduce(self):
return f"I am {self.name}, and I am {self.age} years old."
[12]:
parent = Parent("Jake")
child = Child("Alice", 10)
print(parent.speak())
print(child.speak())
print(child.introduce())
Jake says hello. I am a parent.
Alice says hello. I am a child.
I am Alice, and I am 10 years old.
Abstract Class#
An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can have abstract methods, which are methods declared without implementation. Abstract classes are defined using the abc module.
[13]:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
return "Woof!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
[14]:
dog = Dog()
print(dog.make_sound())
Woof!
[15]:
cat = Cat()
print(cat.make_sound())
Meow!