KEMBAR78
Python notes for students to develop and learn | PPTX
UNIT III
CONTROL FLOW, FUNCTIONS
CONTROL FLOW, FUNCTIONS
 Conditionals: Boolean values and operators,
conditional (if), alternative (if-else), chained
conditional (if-elif-else);
 Iteration: state, while, for, break, continue,
pass;
 Fruitful functions: return values, parameters,
 scope: local and global, composition, recursion;
 Strings: string slices, immutability, string functions
and methods, string module; Lists as arrays.
 Illustrative programs: square root, gcd,
exponentiation, sum the array of numbers, linear
search, binary search.
BOOLEAN VALUES:
Boolean:
 Boolean data type have two values. They
are 0 and 1.
 0 represents False
 1 represents True
 True and False are keyword.
Example:
>>> 3==5
False
>>> 6==6
True
>>> True+True
2
>>> False+True
1
>>> False*True
0
OPERATORS:
 Operators are the constructs which can
manipulate the value of operands.
 Consider the expression 4 + 5 = 9. Here,
4 and 5 are called operands and + is
called operator.
Types of Operators:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic operators:
• They are used to perform mathematical operations like
addition, subtraction, multiplication etc.
Operator Description Example
a=10,b=20
+ Addition Adds values on either side of the
operator.
a + b = 30
-
Subtraction
Subtracts right hand operand from left
hand operand.
a – b = -10
Operator Description Example
a=10,b=20
+ Addition Adds values on either side of the
operator.
a + b = 30
- Subtraction Subtracts right hand operand from
left hand operand.
a – b = -10
* Multiplication Multiplies values on either side of
the operator
a * b = 200
/ Division Divides left hand operand by right
hand operand
b / a = 2
Operator Description Example
a=10,b=20
% Modulus Divides left hand operand by right
hand operand and returns
remainder
b % a = 0
** Exponent Performs exponential (power)
calculation on operators
a**b =10 to
the
power 20
// Floor Division - The division of
operands where the result is the
quotient in which the digits after the
decimal point are removed
5//2=2
Comparison (Relational) Operators:
• Comparison operators are used to compare values.
• It either returns True or False according to the condition.
Operator Description Example
a=10,b=20
== If the values of two operands are equal,
then the condition becomes true.
(a == b) is
not true.
!= If values of two operands are not equal,
then condition becomes true.
a – b = -10(a!
=b) is true
Operator Description Example
a=10,b=20
> If the value of left operand is greater than the
value of right operand, then condition
becomes true.
(a > b) is not
true.
< If the value of left operand is less than the
value of right operand, then condition
becomes true.
(a < b) is true.
>= If the value of left operand is greater than or
equal to the value of right operand, then
condition becomes true.
(a >= b) is not
true.
<= If the value of left operand is less than or
equal to the value of right operand, then
condition becomes true.
(a <= b) is
true.
Assignment Operators:
Operator Description Example
= Assigns values from right side
operands to left side operand
c = a + b assigns
value of a + b into
c
+= Add AND It adds right operand to the left
operand and assign the result to
left operand
c += a is
equivalent
to c = c + a
-= Subtract
AND
It subtracts right operand from the
left operand and assign the result
to left operand
c -= a is
equivalent
to c = c - a
Operator Description Example
*= Multiply
AND
It multiplies right operand with
the left operand and assign the
result to left operand
c *= a is
equivalent to c
= c * a
/= Divide
AND
It divides left operand with the
right operand and assign the
result to left operand
c /= a is
equivalent to c
= c / ac /= a is
equivalent to c
= c / a
Operator Description Example
%= Modulus
AND
It takes modulus using two operands
and assign the result to left operand
c %= a is
equivalent to c = c
% a
**= Exponent
AND
Performs exponential (power)
calculation on operators and assign
value to the left operand
c **= a is
equivalent to c = c
** a
//= Floor Division It performs floor division on
operators and assign value to the left
operand
c //= a is
equivalent to c =
c // a
Logical Operators:
Logical operators are and, or, not operators.
Operator Meaning Example
and True if both the
operands are true
x and y
or True if either of the
operands is true
x or y
not True if operand is
false (complements
the operand
not x
Bitwise Operators:
• Let x = 10 (0000 1010 in binary) and y = 4 (0000
0100 in binary)
Operator Meaning Example
& Bitwise AND x & y = 0 (0000 0000)
I Bitwise OR x i y = 14 (0000 1110)
- Bitwise NOT -x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 40 (0010 1000)
Membership Operators:
Evaluates to find a value or a variable is in the
specified sequence of string, list, tuple,
dictionary or not.
To check particular element is available in the
list or not.
Operators are in and not in.
Membership Operators:
Operator Meaning Example
in True if value/variable is
found in the sequence
5 in x
not in True if value/variable is
not found in the sequence
5 in not x
Example:
x=[5,3,6,4,1]
>>> 5 in x
True
>>> 5 not in x
False
Identity Operators:
• They are used to check if two values (or variables)
are located on the same part of the memory.
Operator Meaning Example
Is True if the operands are
identical (refer to the same
object)
X is true
Is not True if the operands are
not identical (do not refer
to the same object)
X is not true.
Example:
x = 5
y = 5
a = 'Hello'
b = 'Hello‘
print(x is not y) // False
print(a is b)//True
CONDITIONALS
 Conditional if
 Alternative if… else
 Chained if…elif…else
 Nested if….else
Conditional (if):
• conditional (if) is used to test a condition, if the
condition is true the statements inside if will be
executed.
Syntax:
if (confition 1) :
statement 1

Python notes for students to develop and learn

  • 1.
  • 2.
    CONTROL FLOW, FUNCTIONS Conditionals: Boolean values and operators, conditional (if), alternative (if-else), chained conditional (if-elif-else);  Iteration: state, while, for, break, continue, pass;
  • 3.
     Fruitful functions:return values, parameters,  scope: local and global, composition, recursion;  Strings: string slices, immutability, string functions and methods, string module; Lists as arrays.  Illustrative programs: square root, gcd, exponentiation, sum the array of numbers, linear search, binary search.
  • 4.
    BOOLEAN VALUES: Boolean:  Booleandata type have two values. They are 0 and 1.  0 represents False  1 represents True  True and False are keyword.
  • 5.
    Example: >>> 3==5 False >>> 6==6 True >>>True+True 2 >>> False+True 1 >>> False*True 0
  • 6.
    OPERATORS:  Operators arethe constructs which can manipulate the value of operands.  Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
  • 7.
    Types of Operators: 1.Arithmetic Operators 2. Comparison (Relational) Operators 3. Assignment Operators 4. Logical Operators 5. Bitwise Operators 6. Membership Operators 7. Identity Operators
  • 8.
    Arithmetic operators: • Theyare used to perform mathematical operations like addition, subtraction, multiplication etc. Operator Description Example a=10,b=20 + Addition Adds values on either side of the operator. a + b = 30 - Subtraction Subtracts right hand operand from left hand operand. a – b = -10
  • 9.
    Operator Description Example a=10,b=20 +Addition Adds values on either side of the operator. a + b = 30 - Subtraction Subtracts right hand operand from left hand operand. a – b = -10 * Multiplication Multiplies values on either side of the operator a * b = 200 / Division Divides left hand operand by right hand operand b / a = 2
  • 10.
    Operator Description Example a=10,b=20 %Modulus Divides left hand operand by right hand operand and returns remainder b % a = 0 ** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20 // Floor Division - The division of operands where the result is the quotient in which the digits after the decimal point are removed 5//2=2
  • 11.
    Comparison (Relational) Operators: •Comparison operators are used to compare values. • It either returns True or False according to the condition. Operator Description Example a=10,b=20 == If the values of two operands are equal, then the condition becomes true. (a == b) is not true. != If values of two operands are not equal, then condition becomes true. a – b = -10(a! =b) is true
  • 12.
    Operator Description Example a=10,b=20 >If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true. < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true. <= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.
  • 13.
    Assignment Operators: Operator DescriptionExample = Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c += Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a -= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c - a
  • 14.
    Operator Description Example *=Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a /= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
  • 15.
    Operator Description Example %=Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a **= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a
  • 16.
    Logical Operators: Logical operatorsare and, or, not operators. Operator Meaning Example and True if both the operands are true x and y or True if either of the operands is true x or y not True if operand is false (complements the operand not x
  • 17.
    Bitwise Operators: • Letx = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary) Operator Meaning Example & Bitwise AND x & y = 0 (0000 0000) I Bitwise OR x i y = 14 (0000 1110) - Bitwise NOT -x = -11 (1111 0101) ^ Bitwise XOR x ^ y = 14 (0000 1110) >> Bitwise right shift x>> 2 = 2 (0000 0010) << Bitwise left shift x<< 2 = 40 (0010 1000)
  • 18.
    Membership Operators: Evaluates tofind a value or a variable is in the specified sequence of string, list, tuple, dictionary or not. To check particular element is available in the list or not. Operators are in and not in.
  • 19.
    Membership Operators: Operator MeaningExample in True if value/variable is found in the sequence 5 in x not in True if value/variable is not found in the sequence 5 in not x
  • 20.
    Example: x=[5,3,6,4,1] >>> 5 inx True >>> 5 not in x False
  • 21.
    Identity Operators: • Theyare used to check if two values (or variables) are located on the same part of the memory. Operator Meaning Example Is True if the operands are identical (refer to the same object) X is true Is not True if the operands are not identical (do not refer to the same object) X is not true.
  • 22.
    Example: x = 5 y= 5 a = 'Hello' b = 'Hello‘ print(x is not y) // False print(a is b)//True
  • 23.
    CONDITIONALS  Conditional if Alternative if… else  Chained if…elif…else  Nested if….else
  • 24.
    Conditional (if): • conditional(if) is used to test a condition, if the condition is true the statements inside if will be executed. Syntax: if (confition 1) : statement 1