0% found this document useful (0 votes)
15 views15 pages

Lesson 7 Conditionals

Uploaded by

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

Lesson 7 Conditionals

Uploaded by

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

ARTIFICIAL INTELLIGENCE

PROGRAMMING

LESSON 6: CONTROL STATEMENTS &


FUNCTIONS
Conditionals

• Sometimes you need to run (or not run) a given code block
depending on whether certain conditions are met. In this
case, conditional statements are your ally. These statements
control the execution of a group of statements based on the truth
value of an expression. You can create a conditional statement in
Python with the if keyword and the following general syntax:
• if expr0:
• # Run if expr0 is true
• # Your code goes here...
• elif expr1:
• # Run if expr1 is true
• # Your code goes here...
• elif expr2:
• # Run if expr2 is true
• # Your code goes here...
• ...
• else:
• # Run if all expressions are false
• # Your code goes here...

• # Next statement

• The if statement runs only one code block. In other words,


if expr0 is true, then only its associated code block will run.
After that, the execution jumps to the statement directly below
the if statement.
• The first elif clause evaluates expr1 only if expr0 is false.
If expr0 is false and expr1 is true, then only the code block
associated with expr1 will run, and so on.
• The else clause is optional and will run only if all the
previously evaluated conditions are false.
• You can have as many elif clauses as you need, including
none at all, but you can have only up to one else clause.
• Here are some examples of how this works:
• >>>
• >>> age = 21
• >>> if age >= 18:
• ... print("You're a legal adult")
• ...
• You're a legal adult

• >>> age = 16
• >>> if age >= 18:
• ... print("You're a legal adult")
• ... else:
• ... print("You're NOT an adult")
• ...
• You're NOT an adult
• >>> age = 18
• >>> if age > 18:
• ... print("You're over 18 years old")
• ... elif age == 18:
• ... print("You're exactly 18 years old")
• ...
• You're exactly 18 years old

• In the first example, age is equal to 21, so the condition is true, and Python
prints You're a legal adult to your screen. In the second example, the
expression age >= 18 evaluates to False, so Python runs the code block of
the else clause and prints You're NOT an adult on your screen.
• In the final example, the first expression, age > 18, is false, so the
execution jumps to the elif clause. The condition in this clause is true, so
Python runs the associated code block and prints You're exactly 18 years
old.
Loops

• If you need to repeat a piece of code several times to get a


final result, then you might need to use a loop.
• Loops are a common way of iterating multiple times and
performing some actions in each iteration. Python provides
two types of loops:
• for loops for definite iteration, or performing a set number or
repetitions
• while loops for indefinite iteration, or repeating until a given
condition is met
• Here’s the general syntax to create a for loop:
• for loop_var in iterable:
• # Repeat this code block until iterable is exhausted
• # Do something with loop_var...
• if break_condition:
• break # Leave the loop
• if continue_condition:
• continue # Resume the loop without running the remaining code
• # Remaining code...
• else:
• # Run this code block if no break statement is run

• # Next statement
• This type of loop performs as many iterations as items in iterable. Normally, you
use each iteration to perform a given operation on the value of loop_var.
The else clause is optional and runs when the loop finishes.
The break and continue statements are also optional.
• Check out the following example:
• >>>
• >>> for i in (1, 2, 3, 4, 5):
• ... print(i)
• ... else:
• ... print("The loop wasn't interrupted")
• ...
• 1
• 2
• 3
• 4
• 5
• The loop wasn't interrupted
• When the loop processes the last number in the tuple, the
flow of execution jumps into the else clause and prints The
loop wasn't interrupted on your screen.
• That’s because your loop wasn’t interrupted by
a break statement. You commonly use an else clause in loops
that have a break statement in their code block. Otherwise,
there’s no need for it.
• If the loop hits a break_condition, then the break statement
interrupts the loop execution and jumps to the next statement
below the loop without consuming the rest of the items in iterable:
• >>>
• >>> number = 3
• >>> for i in (1, 2, 3, 4, 5):
• ... if i == number:
• ... print("Number found:", i)
• ... break
• ... else:
• ... print("Number not found")
• ...
• Number found: 3
• If the loop hits a continue_condition, then the continue statement
resumes the loop without running the rest of the statements in the loop’s
code block:
• >>>
• >>> for i in (1, 2, 3, 4, 5):
• ... if i == 3:
• ... continue
• ... print(i)
• ...
• 1
• 2
• 4
• 5

• This time, the continue statement restarts the loop when i == 3. That’s
why you don’t see the number 3 in the output.
While
• You normally use a while loop when you don’t know beforehand how many iterations you need
to complete a given operation. That’s why this loop is used to perform indefinite iterations.
• Here’s the general syntax for a while loop in Python:
• while expression:
• # Repeat this code block until expression is false
• # Do something...
• if break_condition:
• break # Leave the loop
• if continue_condition:
• continue # Resume the loop without running the remaining code
• # Remaining code...
• else:
• # Run this code block if no break statement is run

• # Next statement
• This loop works similarly to a for loop, but it’ll keep iterating until expression is false.
A common problem with this type of loop comes when you provide an expression that
never evaluates to False. In this case, the loop will iterate forever.
• Here’s an example of how the while loop works:
• >>>
• >>> count = 1
• >>> while count < 5:
• ... print(count)
• ... count = count + 1
• ... else:
• ... print("The loop wasn't interrupted")
• ...
• 1
• 2
• 3
• 4
• The loop wasn't interrupted
Functions

• In Python, a function is a named code block that performs


actions and optionally computes the result, which is then
returned to the calling code. You can use the following syntax
to define a function:
• def function_name(arg1, arg2, ..., argN):
• # Do something with arg1, arg2, ..., argN
• return return_value

• The def keyword starts the function header. Then you need
the name of the function and a list of arguments in
parentheses. Note that the list of arguments is optional, but the
parentheses are syntactically required.

You might also like