Official Documentation

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Trigger.htm

Trigger Events

Identify Trigger Type inside Trigger Class ( Trigger Context Variable )

Simple Apex Trigger

trigger LeadTrigger on Lead (before insert) {

    for(Lead leadRecord : Trigger.new){
        
        if(String.isBlank(leadRecord.LeadSource)){
            leadRecord.LeadSource='Other';
        }
        
    }
    
}

Validation Rule Creation with Apex Trigger

trigger LeadTrigger on Lead (before insert, before update) {

    for(Lead leadRecord : Trigger.new){
        
        if(String.isBlank(leadRecord.LeadSource)){
            leadRecord.LeadSource='Other';
        }
        
        if(String.isBlank(leadRecord.Industry)){
					// error for the record
           leadRecord.addError('APEX Error  : Industry field cannot be blank.'); 
					// error for the field...
					//leadRecord.Industry.addError('APEX Error  : Industry field cannot be blank.'); 
        }
        
    }
    
    
    
}