Lec 3
Lec 3
Programming: Lecture
3
Outline
• Boolean Expressions
• Functions
Boolean Expressions
• A Boolean expression is an expression that evaluates to either True or False.
• Examples:
>>> 2 < 3 -> True
>>> 3 < 1 -> False
>>> 4+5 > 2+3-> True
>>> 2 == 2 -> True
>>> 2 == 3 -> False
>>> 2 != 3 -> True
Comparison Operators
Logical Operators
• Boolean expressions can be combined using logical operators.
• and – returns true if both operands of the expressions are True
• or – returns true if either of the operands are True.
• not – returns the opposite of its operand
Example
>>> not 1 == 0 or 2 == 1 and 1 == 1
Functions
• Function is a named sequence of statements that performs a computation.
• A function is a block of code that performs a specific task.
• Functions are used to break down large programs into smaller, more manageable pieces. This
makes code easier to read, understand, and maintain.
Function types
There are two kinds of functions in Python.
• Built-in functions that are provided as part of Python type(), float(), int(),print() ...
• User-defined functions that we define ourselves and then use
Built-in functions
>>> abs(-2) -> 2
>>> max(2,5,3) -> 5
>>> min(2,4,5) -> 2
Calling a function
The syntax to call a function is:
>>> function_name(argument)
• An argument is a value we pass into the function as its input when we call the function
>>> max(2,3,4)
2,3, and 4 are arguments.
Modules
• A module is a file that contains Python code.
• Modules can be used to organize code, to make code reusable, and to import code from other
files.
• To use a module, you import it using the import statement.
>>> import math
>>> math.sqrt(4) -> 2.0
>>> math.sin(30) -> -0.988
>> math.sin(math.radians(30)) -> 0.4999
Function Components
• Name of the function
• Parameters
• The sequence of statements that perform a computation