Python >> How to Define Function, Class, Enum, Variable, Constant, Array(List)

2022-05-12 Python

Table of Contents

This tutorial shows how to define function, class, enum, variable, constant, array(list) in Python.

Python basic

Python Define Function

Define a function with arguments and return value

def add(a, b):
    return a + b
print(add(1, 2))

Define a function without arguments

def print_something():
    print("Hello World")
print_something()

Define a function with arguments and a default value

def print_something(name="John", age=30):
    print("Hello", name, ", you are", age)
print_something()

Define a function with a variable number of arguments

def print_something(*args):
    print(args)
print_something(1, 2, 3, 4, 5)

Define a function with a variable number of keyword arguments

def print_something(**kwargs):
    print(kwargs)
print_something(name="John", age=30)

Define a function with a variable number of arguments and keyword arguments

def print_something(*args, **kwargs):
    print(args)
    print(kwargs)
print_something(1, 2, 3, name="John", age=30)

Define a function with arguments that type are specified and return type is also specified

def add(a: int, b: int) -> int:
    return a + b
print(add(1, 2))

Python define class

# Define a class with a constructor and a method.
class Person:
    # Constructor
    def __init__(self, name, age):
        # Attributes
        self.name = name
        self.age = age
    
    # Method without arguments
    def say_hello(self):
        print("Hello", self.name)
    
    # Method with arguments
    def say_age(self):
        print("You are", self.age)

# Create an object of the class.
p = Person("John", 36)

# Call the methods.
p.say_hello()
p.say_age()

Hello John
You are 36

Python define enumeration

# Define a enumeration.
class Color:
    RED = 1
    GREEN = 2
    BLUE = 3
    YELLOW = 4
    WHITE = 5
    BLACK = 6
    PURPLE = 7

# Use the enumeration.
print(Color.RED)
print(Color.GREEN)

1
2

Python define variable

Python is a dynamic language. The variable can be assigned any value. The variable type can be refered by the value.

import datetime

# Define a integer.
a = 1

# Define a string
b = "Hello"

# Define a float
c = 1.5

# Define a boolean
d = True

# Define a list
e = [1, 2, 3]

# Define a dictionary
f = {"name": "John", "age": 30}

# Define a tuple
g = (1, 2, 3)

# Define a set
# The values in a set are unique. 
h = {1, 2, 3}

# Define datetime
d = datetime.datetime.now()

# Define a namedtuple
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(1, 2)
print(p) # Result: Point(x=1, y=2)

Python define constant

Python is not a static language.
In a strict sense, you cannot declare a variable or value as constant in Python.
We can define a constant variable by using variable names in uppercase.
But this is just an agreement and acutally the value can be changed.

# Define a constant.
PI = 3.14

Python define array(list)

Python is a dynamic language. The variable can be assigned any value. The variable type can be refered by the value.

# Define a list.
a = [1, 2, 3]

# Define an empty list.
b = []
# Add value to the list
b.append(1)
b.append(2)
print(b) # Result: [1, 2]
# Remove value
b.remove(2)
print(b) # Result: [1]

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us