Exit[]

Assignments

(* this is a comment, it's not going to be evaluated *)

a = 4;                                 (* integer *) 
b = 5.5 + Sqrt[-4.4];(* complex number *) 
c = "element";               (* string *)
d = function[ 4 ];        (* function call *)  
e = Pi;                              (* symbol *)
f = {1,2};                     (* a list *)

b

?d

d

Clear[a,b,c,d,e,f,x,y,z]

?d

x=a+3

?x

b=Pi/E

= is the assignment
:= is the delayed assignment

y=E b;
z:=E b;

{y,z}

b=E; {y,z}

?y

?z

Clear[a,b,c,d,e,f,x,y]

f[x_]:=x^2

?f

f        function name
x_     argument  (pattern)
:=     delayed assignment
x^2  definition

f[3]

f[a]

f[{1,2,3}]

f["paolo"]

f[{3,b}]

exp[Pi]

f[3,b]

Syntax

fun[argument]
fun@argument
argument//fun

fun[arg1,arg2]
arg1~fun~arg2 
Apply[fun,{arg1,arg2}]
fun@@{arg1,arg2}

sol=Integrate[Sin[2 x] Cos[x/2], {x, 0, 1}]

N[sol]
sol//N
N@sol

M=Table[Sin[(i+j)^-1],{i,4},{j,3}]

(M=Table[Sin[(i+j)^-1],{i,4},{j,3}])//MatrixForm

m=(Sqrt[Table[Sin[(i+j)^-1],{i,4},{j,3}][[2]]]//N)

How to define a function for different datatypes?

g on real numbers :  identity function 
g on integers : factorial
g on lists : Length

?Switch

Head[{3,4,5}]

FullForm[{3,4,5}]

Head[Pi]

Clear[g]

g[x_]:=
Switch[ Head[x],
Integer, x!,
Real, x,
List, Length[x],
_, Print["something else"]; (* Abort[] ?  Hold[g[x]] *)
]

g[3.1415]

Head[3.1415]

g[Pi]

g[5]

g[{1,2,{3,4},{}}]

g["s"]

?x

g[x]

Patterns with data type

Clear[g]

g[x_Real]:=x

g[x_Integer]:= x!

g[x_List]:= Length[x]

g[x]

g[Pi]

g[N[Pi]]

?g

Types: Integer, Real, Complex, Symbol, List

Clear[f,x]

f[x_]:=Sin[x]/x

Plot[f[x],{x,-5,5}]

f[0]

?f

Limit[Sin[x]/x,x->0]

->

?Limit

f[0]:=1;

?f

Clear[f]

f[0]

f[0]:=Limit[Sin[x]/x,x->0]

?f

f[2]

f[x_]:=Sin[x]/x

f[2]

?f

f[0]

Better idea?

:= vs =

f[0]=Limit[Sin[x]/x,x->0]

?f

More examples

Clear[h]

h[x_,y_]:=x+y

{h[1,2],
h[2,2],
h[2,{2}],
h[Pi,{1,2}],
h[{Pi},{1,2}],
h[{1,2},{1,2}],
h[Pi,E,1,2]
}//ColumnForm

h[x_,x_]:=x

?h

{h[1,2],
h[2,2],
h[2,{2}],
h[Pi,{1,2}],
h[{Pi},{1,2}],
h[{1,2},{1,2}],
h[Pi,E,1,2]
}//ColumnForm

?Random

?h

h[x,{1,2}]

h[x_,{1,2}]:=  {1,2}[[Random[Integer]+1]]

?h

{h[1,2],
h[2,2],
h[2,{2}],
h[Pi,{1,2}],
h[{Pi},{1,2}],
h[{1,2},{1,2}],
h[Pi,E,1,2]
}//ColumnForm

Clear[g]

g[x_,{1,2}]:=  {1,2}[[Random[Integer]+1]]

g[x_,x_]:=x

?g

g[{1,2},{1,2}]

Clear[g]

g[x_,x_]:=x

g[x_,{1,2}]:=  {1,2}[[Random[Integer]+1]]

?g

g[{1,2},{1,2}]

NOTE: Both the order and the generality of the definitions are crucial. 
The more specific a definition, the earlier is tested: 
a definition for x_Integer is tested before a definition for x_.

ff[x_]:=x^2

ff[x_Integer]:=x

?ff

ff[4]

ff[4.]

gg[x_Integer]:=x

gg[x_]:=x^2

gg[4]

?gg

gg[x_Paolo]:= Pi

?gg

gg[Paolo[5]]

For overlapping definitions with the same level of generality, the earlier the definition is evaluated the earlier it gets tested.

Clear[ff,gg]

ff[x_/;x<4]:=x

ff[x_/;x>2]:=x^2

Plot[ff[t],{t,0,6}]

ff[4]

gg[x_/;x>2]:=x^2

gg[x_/;x<4]:=x

Plot[gg[t],{t,0,6}]

Plot[{gg[t],ff[t]},{t,0,6}]

gg[2]

?ff

?gg

g[x_,y_/;x>y]:=x

{g[3,2],g[3,4]}

Clear[f,g]

f[x_,2x_]:=x

{
f[Pi,2 Pi],
f[2,2 2],
f[x,2 x],
f[Sin[1], 2 Sin[1]]
}//ColumnForm

FullForm[2 x_]
FullForm[2Pi]
FullForm[2 2]
FullForm[2 x]
FullForm[2 Sin[1]]

{
f[Pi, Pi 2],
f[2,2 2],
f[x,x 2],
f[Sin[1], Sin[1] 2]
}//ColumnForm

g[x_,y_]/; y==2x:=x

g[2,4]