Replacement Rules

x=3+a

?x

/. indicates replacement 
exp1 -> exp2 is a replacement rule; NO assignment is performed!
the right arrow is obtained with the characters "-" and ">".

x/.a->5

x

x/.{3->a}

x

x/.3->a

x/..3->a

x

More replacements. The next one is a BAD IDEA!  
Notice the difference between

x/.{x->-10}

and

(x+10)/.{x->-10}

b=x+10

b/.x->-10

Also,

a

and obviously

a/.x->5

but ...

3+a/.x->10

This is how the replacement rule should be used:

x/.{3+a->10}

Back to meaningful examples.

x

x/.a->x

x+y/.a->y

/. { rule1, rule2, .. }
is a list of replacement rules. 
Rules follow a left to right precedence. Thus, rule1 has precedence over every other rule.

x/.{a->2,a->4}

x/.{a->2,3->4}

Rules are applied from left to right, but simultaneously.

x/.{a->3,3->4}

x+y/.{a->y,y->4}

/.

?Replace

//. is the repeated replacement. 
The rules are applied, one at the time, until the expression does not change anymore.

?//.

?ReplaceRepeated

x+y//.{a->y,y->4}

x+y/.{a->y,y->4}

7+y/.{a->y,y->4}

x+y/.{a->y,y->a}

x+y//.{a->y,y->a}

x+y//.{a->y,y->2a}

??ReplaceRepeated

More replacements

Successive replacement

(x+y/.{a->y})/.y->a

x+y/.{a->y}/.y->a

Multiple rules

x+y/.{{a->y},{y->a}}

Clear[a,b,x,y,z]

Examples

{3a,a^2,3^a,a^a,a!}/.a->z

{3a,a^2,3^a,a^a,a!}/.a->4

The replacement rule may contain a pattern.

{3a,a^2,3^a,a^a,a!}/.a^_->Infinity

{3a,a^2,3^a,a^a,a!}/._^_->Infinity

Clear[x,b]

{3a,a^2,3^a,a^a,a!}/.a^x_->x^a

{3a,a^(2+Pi),3^a,(a+b)^a,a!}/.a_^exp_->exp^a

Use of replacement rules

simplification

log[a b c d]//FullForm

log[a b c d]/.{log[x_ y_]->log[x]+log[y]}

log[a b c d]//.{log[a_ b_]->log[a]+log[b]}

log[a b c d]//.{log[Times[a_, b_]]->log[a]+log[b]}

the solutions of equations are provided in the form of replacement rules

p[x_]:=x^3-Pi x^2-Pi^2 x-3

p[x]

Plot[p[x],{x,-Pi/2,3/2Pi}]

sol=Solve[p'[x]==0,x]

p'[x]

p'[x]/.sol

p[x]/.sol

p[x]/.sol//N

options to Mathematica functions

ParametricPlot[
{Sin[u]u ,Cos[u]u}, 
{u,0,100}
]

Options[ParametricPlot]

ParametricPlot[
{Sin[u]u ,Cos[u]u}, 
{u,0,100},
PlotPoints->125,
Axes->False,
ColorFunction->(ColorData["Rainbow"][#3]&),
MaxRecursion->0,
PlotStyle->Thick
]

a program that accepts replacement rules as optional arguments

options[program]={opt1->val1, opt2->val2};

program[x_,y_,opt___]:=
definition/.{opt}/.options[program]

Examples

l=RandomInteger[20,{8}]

l/.{a_,b_}/;a>b->{b,a}

l

l/.{___,a_,b_,___}/;a>b->{b,a}

l//.
{left___,a_,b_,right___}/;a>b->{left,b,a,right}

A little bit of list analysis. Are there two successive entries (a, b) such that b is 2 + a?

l={1,3,4,6,8}

l/.
{left___,a_,b_,right___}/;b==a+2->{a,b}

Only one result. How to get all the possible results?

?/.

ReplaceList[
{3,1,5,2,8,6},
{l1___,l2_,l3_,l4___}/;l2>l3->{l3,l2}
]

l

ReplaceList[
l,
{left___,a_,b_,right___}/;b==a+2->{a,b}
]

ReplaceList[
{1,2,3,4,5,6},
{x___,y___,z_}->{{x},{y},{z}}
]//ColumnForm

Very useful for debugging

ReplaceList[
{1,3,4,6,8},
{l1___,l2_,l3_,l4___}->{{l1},l2,l3,{l4}}
]//ColumnForm

l={3,6,-1,4,3,2,1,0,3,6}

ReplaceList[
l,
{___,left_,mid__,right_,___}/;left+right==6->{mid}
]//ColumnForm

Again, list analysis. Finding all the non-empty sequences whose left and right elements add up to 6

ReplaceList[
{3,6,-1,4,3,2,1,0,3,6},
{___,l1_,l2__,l3_,___}/;l1+l3==6->{l2}
]//ColumnForm

Encoding

How to transform a list {a,a,a,a,b,b,a} to{{a,4},{b,2},{a,1}} ?

{a,b}[[{1,1,2}]]

randL[n_]:={a,b}[[RandomInteger[{1,2},n]]]

l=randL[25]

ReplaceList[
Map[{#,1}&,l],
{left___,{x_,n1_},{x_,n2_},right___}->{left,{x,n1+n2},right}
];

l

Map[{#,1}&,l]//.
{left___,{x_,n1_},{x_,n2_},right___}->{left,{x,n1+n2},right}

IDEA: {...{a,n},{a,m},...}->{...{a,n+m},...}

ll=Map[{#,1}&,l]

Then .... ?

ll//.{left___,{x_,n_},{x_,m_},right___}->{left,{x,n+m},right}