0% found this document useful (0 votes)
7 views

Lec 3

Uploaded by

Natnael Belayneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lec 3

Uploaded by

Natnael Belayneh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Computer

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

• >>> 2 < 3 and 4 > 5 -> False


• >>> not (1 == 1) -> False
• >>> 2 < 3 or 4 > 5 -> True
Operator Precedence
not – gets evaluated first and given the highest priority
and – gets evaluated second.
or – gets evaluated last

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

def function_name(parameter_1, parameter_2, ...):


# body of the function
Function Example
def add_numbers(number_1, number_2):
print(number_1 + number_2)
Calling functions
function_name(argument)
def multiply_numbers(number_1, number_2): Example:
add_numbers(2,3)
print(number_1 * number_2) multiply_numbers(2,3)
get_area_of_triangle(5,4)

def get_area_of_triangle(base, height):


print ((base * height) / 2)
Exercise
Write a function that prints area of a circle given any radius greater than 0
Write a function that print area of a rectangle given any length and width greater than 0
Write a function that prints a square of a number
Write a function that prints a maximum of 4 numbers
Flow of Execution
• Execution always begins at the first statement of the program. Statements are run one at a time,
in order from top to bottom.
• Function definitions do not alter the flow of execution of the program, but remember that
statements inside the function don’t run until the function is called.
• A function call is like a detour in the flow of execution. Instead of going to the next statement,
the flow jumps to the body of the function, runs the statements there, and then comes back to
pick up where it left off.
Demonstration

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy