# What is Python (2)

#### Function

Function is a named sequence of statements that performs a computation. Users can use built-in functions as well as user-defined functions.

Build-in functions: Python 3 build-in functions

- print( )
- type function
- type( )
- type(32) = <type ‘int’>
- type conversion
- int(), float(), str()
- input( )

Import Modules

- import XX
- import math
- math.lot10(), math.sin(), math.pi, math.sqrt()
- from XX import XX
- from math import pi
- from math import * # Not a good practice

*Define a new function*

`def function_name(parameter1, parameter2,...):`

body area

(return XXX)

To call this function, use function_name(param1, param2,…)

Notes:

- Need “:” after def, exactly four indents in the body area
- Variables and parameters within a function are local, which means they only exist inside the function.

#### Conditions

Relational operators

- x == y
- x != y
- x > y
- x < y
- x >= y
- x < = y

Logical operator

- and
- or
- not

*Application*

if condition1: if condition2: XXX elif condition3: XXX else: XXX

Note: “:” after if, and four indents

#### Iteration

#### if statement

`def countdown(n): `

if n < 0:

print(Done!)

else:

print(n)

countdown(n-1)

#### while statement

`while condition_is_true:`

do_something

To jump out of the loop, use break:

`while condition_is_true:`

break

#### for statement

`for x in list|dictionary:`

do_something