Python Revision Tour - I
Made By: Tarun Baria,PGT CS, JNV Leh
Expressions
Expressions
● Arithmetic Expression
○ Involves numbers
○ Operators: +, -, *, /, //, %
○ e.g. 2 + 5 ** 3, - 8 * 6 / 5
● Relational Expressions
○ Involves comparison of values
○ Operators: >, <, >=, <=, ==, !=
○ e.g. x > y, y <= z, z != x
● Logical Expression
○ Involves logical operations
○ Operators: not, and, or
○ e.g. x or y
● String Expressions
○ Involves Strings
○ “and” + “then
○ “”and” * 2
Evaluating Arithmetic Operations
● Operator Precedence
● Implicit Type Conversion
Operator
Precedence
Remember BODMAS???
Implicit Type
Conversion
Conversion of Datatype
performed by the interpreter
w/o programmer’s intervention
If the operation involves more than
one type of datatype, all values
(operands) of the Expression are
converted into datatype of biggest
operand
Evaluating Logical Expressions
● Precedence of Logical Expressions is lower than Arithmetic Expressions
● Precedence: not, and, or
● Important:
○ or: Only evaluates second argument if first element is False
○ and: Only evaluates second argument if first element is True
Type Casting (Explicit Type Conversion)
● Changing of data type by User (Programmer)
● <datatype> (expression)
Math Library Functions
Math Module
● Python’s standard library has math module which has various important
mathematical functions which we can use in your program.
● To use math library functions, you have to import math module in your
program. After importing, you can use its various functions.
Different functions in math module
Statement Flow Control
if - else - elif | for | while
Compound
Statement
● Compound Statement
represents group of
statements which are executed
as Unit.
● Compound Statements are
written with indentation
● The header (first line) of the
compound statement ends
with a colon (:)
Simple Statement
● Any single executable statement is called
simple statement
● Compound Statements are made of
simple statements
● It does nothing!
● In python, empty statement is pass
statement
Empty Statement
if conditionals
Plain if Conditionals
● if statement checks a particular
condition. If the condition is true,
the block below the if condition is
executed, otherwise it is not
executed.
● Syntax:
Test
Expression
Body of if
False
True
if-else Conditionals
● if statement checks a particular
condition. If the condition is true,
the block below the if condition is
executed, otherwise the block
below else statement is executed.
● Syntax:
Test
Expression
Body of if
False
True
Body of else
If - elif - else Conditional
These conditional are used if you want to use
multiple if conditions.
Syntax:
if <condition>:
if block
elif <condition>:
elif 1 block
elif <condition>
elif 2 block
else:
else block
Homework
Write a program that takes marks scored by a student as input and prints grade
as output. Use following table for giving a Grade:
Marks Scored Grades
91 - 100 A
81 - 90 B
71 - 80 C
61 - 70 D
51 - 60 E
<= 50 Do Better Next Time
Please note that first line of your
program should be comment which
mentions your class, roll number and
name.
Nested if Statements
● If if - else statements are used within the
if - else blocks, it is called nested if-else
statements
Syntax:
if <condition>:
if <condition>:
<inner if block>
else:
<inner else block>
else:
<else block>
Storing Conditions
● Just like you are storing the values in the
program, you can also store the
conditions in python.
e.g. cnd = a == b and b == c
Iteration and
Looping
Statements
Two types of
looping statements ● for loop
● while loop
1. for loop
● for loop is used to iterate
through sequences or to
iterate for pre-known finite
amount of times
● General form of loop:
for <variable> in <sequence>:
<statement(s)_to_repeat>
for loop with range() function
● For loop are used with different
sequences
● for loops are used with range() function
for loop with sequences
range() function
Syntax:
range(i) -> goes from 0 to i-1
range(i, j) -> goes from i to j-1
range(i, j, k) -> goes from i to j-1. Increment is
k units per iteration
e.g.
range (5) = ?
range(5, 10) = ?
range(5, 15, 2) = ?
range(10, 5) = ?
range(20, 10, -1) = ?
2. while loop
● A conditional loop that repeats
the instructions within itself
as long as the conditional
remains true
● AKA entry controlled loop
Working with while loop
Jump statements:
break and
continue
break statement
The break statements the very loop
it lies within.
Execution resumes at the
statement immediately following
the body of the terminated
statement.
Working with break statement
With for loop: With while loop
continue statement
continue statement forces the next
iteration of the loop
When continue statement is
encountered, next iteration is
executed skipping any code in
between.
Working with continue statement
In for loops In while loops
More on loops
(loop else and
nested loops)
loop else statement
● In python, we can add else body in the end of for or while loop.
● The body of else statement is executed when loop terminates normally
meaning,
○ In case of for loop, when the loop has executed the last value in the sequence
○ In case of while loop the condition of the while statement turns false
● If the loop is terminated with break statement, the body of else will not be
executed
Working with loop else statements
loop else with for loop else with while
Nested Loops
● A loop may contain another loop inside
the body. Such loops are called nested
loops.
● If break statement is used in the nested
loops, the break statement terminates the
very loop it is situated in.
Thank You!!!

Python Revision Tour 1 Class XII CS

  • 1.
    Python Revision Tour- I Made By: Tarun Baria,PGT CS, JNV Leh
  • 2.
  • 3.
    Expressions ● Arithmetic Expression ○Involves numbers ○ Operators: +, -, *, /, //, % ○ e.g. 2 + 5 ** 3, - 8 * 6 / 5 ● Relational Expressions ○ Involves comparison of values ○ Operators: >, <, >=, <=, ==, != ○ e.g. x > y, y <= z, z != x ● Logical Expression ○ Involves logical operations ○ Operators: not, and, or ○ e.g. x or y ● String Expressions ○ Involves Strings ○ “and” + “then ○ “”and” * 2
  • 4.
    Evaluating Arithmetic Operations ●Operator Precedence ● Implicit Type Conversion
  • 5.
  • 6.
    Implicit Type Conversion Conversion ofDatatype performed by the interpreter w/o programmer’s intervention If the operation involves more than one type of datatype, all values (operands) of the Expression are converted into datatype of biggest operand
  • 7.
    Evaluating Logical Expressions ●Precedence of Logical Expressions is lower than Arithmetic Expressions ● Precedence: not, and, or ● Important: ○ or: Only evaluates second argument if first element is False ○ and: Only evaluates second argument if first element is True
  • 8.
    Type Casting (ExplicitType Conversion) ● Changing of data type by User (Programmer) ● <datatype> (expression)
  • 9.
  • 10.
    Math Module ● Python’sstandard library has math module which has various important mathematical functions which we can use in your program. ● To use math library functions, you have to import math module in your program. After importing, you can use its various functions.
  • 11.
  • 12.
    Statement Flow Control if- else - elif | for | while
  • 13.
    Compound Statement ● Compound Statement representsgroup of statements which are executed as Unit. ● Compound Statements are written with indentation ● The header (first line) of the compound statement ends with a colon (:)
  • 14.
    Simple Statement ● Anysingle executable statement is called simple statement ● Compound Statements are made of simple statements ● It does nothing! ● In python, empty statement is pass statement Empty Statement
  • 15.
  • 16.
    Plain if Conditionals ●if statement checks a particular condition. If the condition is true, the block below the if condition is executed, otherwise it is not executed. ● Syntax: Test Expression Body of if False True
  • 17.
    if-else Conditionals ● ifstatement checks a particular condition. If the condition is true, the block below the if condition is executed, otherwise the block below else statement is executed. ● Syntax: Test Expression Body of if False True Body of else
  • 18.
    If - elif- else Conditional These conditional are used if you want to use multiple if conditions. Syntax: if <condition>: if block elif <condition>: elif 1 block elif <condition> elif 2 block else: else block
  • 19.
    Homework Write a programthat takes marks scored by a student as input and prints grade as output. Use following table for giving a Grade: Marks Scored Grades 91 - 100 A 81 - 90 B 71 - 80 C 61 - 70 D 51 - 60 E <= 50 Do Better Next Time Please note that first line of your program should be comment which mentions your class, roll number and name.
  • 20.
    Nested if Statements ●If if - else statements are used within the if - else blocks, it is called nested if-else statements Syntax: if <condition>: if <condition>: <inner if block> else: <inner else block> else: <else block>
  • 21.
    Storing Conditions ● Justlike you are storing the values in the program, you can also store the conditions in python. e.g. cnd = a == b and b == c
  • 22.
  • 23.
    Two types of loopingstatements ● for loop ● while loop
  • 24.
    1. for loop ●for loop is used to iterate through sequences or to iterate for pre-known finite amount of times ● General form of loop: for <variable> in <sequence>: <statement(s)_to_repeat>
  • 25.
    for loop withrange() function ● For loop are used with different sequences ● for loops are used with range() function for loop with sequences
  • 26.
    range() function Syntax: range(i) ->goes from 0 to i-1 range(i, j) -> goes from i to j-1 range(i, j, k) -> goes from i to j-1. Increment is k units per iteration e.g. range (5) = ? range(5, 10) = ? range(5, 15, 2) = ? range(10, 5) = ? range(20, 10, -1) = ?
  • 27.
    2. while loop ●A conditional loop that repeats the instructions within itself as long as the conditional remains true ● AKA entry controlled loop
  • 28.
  • 29.
  • 30.
    break statement The breakstatements the very loop it lies within. Execution resumes at the statement immediately following the body of the terminated statement.
  • 31.
    Working with breakstatement With for loop: With while loop
  • 32.
    continue statement continue statementforces the next iteration of the loop When continue statement is encountered, next iteration is executed skipping any code in between.
  • 33.
    Working with continuestatement In for loops In while loops
  • 34.
    More on loops (loopelse and nested loops)
  • 35.
    loop else statement ●In python, we can add else body in the end of for or while loop. ● The body of else statement is executed when loop terminates normally meaning, ○ In case of for loop, when the loop has executed the last value in the sequence ○ In case of while loop the condition of the while statement turns false ● If the loop is terminated with break statement, the body of else will not be executed
  • 36.
    Working with loopelse statements loop else with for loop else with while
  • 37.
    Nested Loops ● Aloop may contain another loop inside the body. Such loops are called nested loops. ● If break statement is used in the nested loops, the break statement terminates the very loop it is situated in.
  • 38.