KEMBAR78
Python Conditional_Statements_and_Functions | PPTX
Learning Objectives
By the end of this lesson, you will be able to:
Describe expressions
Illustrate conditional statements
Explain loops
Describe functions
Conditional Statements and Functions
Conditional Expressions
Conditional expressions are used for comparison. Conditional statements supported by Python
are:
Equal
a==b
Not equal
a!=b
Less than
a<b
Less than or equal
a<=b
Greater than
a>b
Greater than or
equal
a>=b
Membership Expressions
Membership expressions are used to validate the membership of a value. It tests for
membership in a sequence, such as strings, lists, or tuples. The different membership
expressions in Python are:
Expression:
▪ Checks for equality
▪ Compares members
Conditional Statements
if, elif, else
Conditional statements execute a group of statements only based on some condition. In
Python, if ,elif, and else are the conditional keyword statements in use.
if: Executes a set of statements only if the condition is true otherwise, statement is
skipped.
Condition
Indentation
matters
if, elif, else
elif: elif or else if is to check multiple conditions.
else: If the condition is not true under if or elif, then the code jumps to else condition.
Jumps to
else
Loops
for Loop
Loops execute a group of statements as long as a condition is True. Python has two basic loops: for
loop and while loop.
for loop: This is also called iterative loop in Python. For loops can iterate over a sequence of
numbers using the "range" and "xrange" functions.
Range function to
set sequence limits
for iteration
Sequence Iteration Using for Loop
String
iteration
using for loop
List iteration
using for
loop
Tuple iteration
using for loop
Sequence IterationUsingfor Loop
dictionary
iteration using
for loop
Sets iteration
using for
loop
Sequence IterationUsingfor Loop
Sequence IterationUsingfor Loop
while Loop
while loop: while loops execute a set of statements as long as a condition is true.
Functions
Functions
A function is a block of code which only runs when it is called.
Defining
function
Passing
variables
Function
Call
Functions
A return statement ends the execution of the function call and returns the result. It is the value
of the expression following the return keyword to the caller.
In-Built Functions in Python
Python has a large library of in-built functions. Some of them are discussed here.
eval(): It evaluates the passed string as a Python expression and returns the result.
lamba(): helps to create one-line functions in Python.
In-Built Functions in Python
map() function returns a list of results after applying the given function to each item of a given
iterable.
• map(fun, iter) : Syntax
• fun : It is a function to which map passes each element of given iterable.
• iter : It is an iterable which is to be mapped.
• You can pass one or more iterable to the map() function.
In-Built Functions in Python
round() function rounds a number to give precision in decimal digits. If the number of decimal
places has not been specified, the decimal number is rounded off to a whole number.
Rounds number
to 2 decimal
places
In-Built Functions in Python
all() function returns false if any one of the elements of the iterable is zero and true if all the
elements of the iterable are nonzero.
Returns True
as non-zero
sequence
Returns False as
zero is present
in sequence
Knowledge Check
“a” is “apple”, will give output
as:
a. a
b. False
c. True
d. Will show error
Knowledge
Check
1
“a” is “apple”, will give output
as:
a. a
b. False
c. True
d. Will show error
Knowledge
Check
1
The correct answer is b
“is “ is a membership operator in Python which returns ‘True’ for exact
value.
a.
b.
Knowledge
Check
2
Let a=“XY“ then the expression: for i in
a:
print(a,end="t")
will give output
as:
X
Y
XY
X
Y
c. X
Y
d. XY
XY
a.
b.
Let a=“XY“ then the expression: for i in
a:
print(a,end="t")
will give output
as:
X
Y
XY
X
Y
c. X Y
d. XY
XY
Knowledge
Check
2
The correct answer is d
for loop will iterate the sequence two times and ends each iteration with a
tab.
Knowledge
Check
3
eval() function evaluates:
a. Expression passed as a string
b. Expression only if it contains digits
c. Expression containing digits and strings
d. Expression to sort the strings
eval() function evaluates:
Knowledge
Check
3
The correct answer is a
eval() evaluates the expression passed as a string and returns the
result.
a. Expression passed as a string
b. Expression only if it contains digits
c. Expression containing digits and strings
d. Expression to sort the strings
The output of: max(False,-4,-1)
a. -1
b. True
c. False
d. -4
Knowledge
Check
4
The output of: max(False,-4,-1)
a. -1
b. True
c. False
d. -4
Knowledge
Check
4
The correct answer is c
The function max() is being used to find the maximum value from -4, -1, and false. Since, false amounts to the
value zero, Hence, False is the right answer.
Key Takeaways
Conditional statements use conditional operators.
For loop is an iterative loop in Python.
Functions limit the use of loops.

Python Conditional_Statements_and_Functions

  • 1.
    Learning Objectives By theend of this lesson, you will be able to: Describe expressions Illustrate conditional statements Explain loops Describe functions
  • 2.
  • 3.
    Conditional Expressions Conditional expressionsare used for comparison. Conditional statements supported by Python are: Equal a==b Not equal a!=b Less than a<b Less than or equal a<=b Greater than a>b Greater than or equal a>=b
  • 4.
    Membership Expressions Membership expressionsare used to validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples. The different membership expressions in Python are: Expression: ▪ Checks for equality ▪ Compares members
  • 5.
  • 6.
    if, elif, else Conditionalstatements execute a group of statements only based on some condition. In Python, if ,elif, and else are the conditional keyword statements in use. if: Executes a set of statements only if the condition is true otherwise, statement is skipped. Condition Indentation matters
  • 7.
    if, elif, else elif:elif or else if is to check multiple conditions. else: If the condition is not true under if or elif, then the code jumps to else condition. Jumps to else
  • 8.
  • 9.
    for Loop Loops executea group of statements as long as a condition is True. Python has two basic loops: for loop and while loop. for loop: This is also called iterative loop in Python. For loops can iterate over a sequence of numbers using the "range" and "xrange" functions. Range function to set sequence limits for iteration
  • 10.
    Sequence Iteration Usingfor Loop String iteration using for loop
  • 11.
    List iteration using for loop Tupleiteration using for loop Sequence IterationUsingfor Loop
  • 12.
    dictionary iteration using for loop Setsiteration using for loop Sequence IterationUsingfor Loop
  • 13.
  • 14.
    while Loop while loop:while loops execute a set of statements as long as a condition is true.
  • 15.
  • 16.
    Functions A function isa block of code which only runs when it is called. Defining function Passing variables Function Call
  • 17.
    Functions A return statementends the execution of the function call and returns the result. It is the value of the expression following the return keyword to the caller.
  • 18.
    In-Built Functions inPython Python has a large library of in-built functions. Some of them are discussed here. eval(): It evaluates the passed string as a Python expression and returns the result. lamba(): helps to create one-line functions in Python.
  • 19.
    In-Built Functions inPython map() function returns a list of results after applying the given function to each item of a given iterable. • map(fun, iter) : Syntax • fun : It is a function to which map passes each element of given iterable. • iter : It is an iterable which is to be mapped. • You can pass one or more iterable to the map() function.
  • 20.
    In-Built Functions inPython round() function rounds a number to give precision in decimal digits. If the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Rounds number to 2 decimal places
  • 21.
    In-Built Functions inPython all() function returns false if any one of the elements of the iterable is zero and true if all the elements of the iterable are nonzero. Returns True as non-zero sequence Returns False as zero is present in sequence
  • 22.
  • 23.
    “a” is “apple”,will give output as: a. a b. False c. True d. Will show error Knowledge Check 1
  • 24.
    “a” is “apple”,will give output as: a. a b. False c. True d. Will show error Knowledge Check 1 The correct answer is b “is “ is a membership operator in Python which returns ‘True’ for exact value.
  • 25.
    a. b. Knowledge Check 2 Let a=“XY“ thenthe expression: for i in a: print(a,end="t") will give output as: X Y XY X Y c. X Y d. XY XY
  • 26.
    a. b. Let a=“XY“ thenthe expression: for i in a: print(a,end="t") will give output as: X Y XY X Y c. X Y d. XY XY Knowledge Check 2 The correct answer is d for loop will iterate the sequence two times and ends each iteration with a tab.
  • 27.
    Knowledge Check 3 eval() function evaluates: a.Expression passed as a string b. Expression only if it contains digits c. Expression containing digits and strings d. Expression to sort the strings
  • 28.
    eval() function evaluates: Knowledge Check 3 Thecorrect answer is a eval() evaluates the expression passed as a string and returns the result. a. Expression passed as a string b. Expression only if it contains digits c. Expression containing digits and strings d. Expression to sort the strings
  • 29.
    The output of:max(False,-4,-1) a. -1 b. True c. False d. -4 Knowledge Check 4
  • 30.
    The output of:max(False,-4,-1) a. -1 b. True c. False d. -4 Knowledge Check 4 The correct answer is c The function max() is being used to find the maximum value from -4, -1, and false. Since, false amounts to the value zero, Hence, False is the right answer.
  • 31.
    Key Takeaways Conditional statementsuse conditional operators. For loop is an iterative loop in Python. Functions limit the use of loops.