Python- Decision making (revision)



Good Morning students.

In the beginning, I must share that from today onward, our discussion will be divided into two parts:
1. Recap of the concepts  which you have shared are difficult for you and you want  redone.
2. New concept 
___________________________________________________
The Google meet ID for today is:
___________________________________________________

By the end of the session today, you will be able to

  • Identify the basic elements of Python and use those elements in writing a program.
  • Look at one problem and see how it can be solved using variety of  IF statements.
  • Apply the variety of IF constructs to solve a given problem.  

_______________________________________________

Lets take a recap  based on the Introduction to Python


IMPORTANT TERMS

  1. TOKENS
  • They are the smallest unit of a program. 
  • Characters are translated into a sequence of tokens.
  •  There are following tokens in Python:
    • keywords
    • literals
    • identifiers
    • operators
    • punctuators


2. Keywords-
    • These are special words which are reserved by Python .
    • They have specific meaning.
    • We are not allowed to use keywords as variables in Python. 
    • Keywords are case-sensitive.
    • Example- if, elif, and, not, for are all keywords
3. Identifiers-
    • These are user-defined names to represent entities like variables, functions etc.
    • We have rules to follow in order to create valid identifiers
    • Example-
      • In the statement:
                    num1=input("Enter a number"), the variable num1 is an IDENTIFIER
         4. Literals- 

    •  These are the data that is given to a variable or a constant.A constant does not change while a variable gets its name from the fact that its value can change.
    • The various types of literals are
      • Strings- Example- "Geeta", '1234' etc.
      • Numeric- Example-1234, 23.760 etc.
      • Boolean-Example-true,false 
      • Special- 'none'
        5. Operators
    • Operators are  the symbols which perform the operation on some values.(Eg- in a+b, "+" is an operator) 
    • Values are known as operands.(Eg.- in a+b, a an b are operands)
    • types of operators:
      • unary-operator acts on single variable Example- Unary minus(eg. -43), Unary plus(eg. +23 or 23) 
      • binary-operator acts on two variables Example- 23+45, here + is operator acting on 23 and 45 and the result is their sum.
      • tertiary- operator acts on three variables
    6Punctuators- These are symbols that are used in programming languages to organize sentence structures. Example- 
    '  (Single quotes)
    "  (Double quotes)
    ( )  (Paranthesis)
    [ ]  ( Square Brackets)
    { }  (Curly brackets )  etc.
    ___________________________________________________________________________

    After this recap, lets take up the DECISION STATEMENTS with various applications:


    Python  Decision Making Statements

    Decision making statement used to control the flow of execution of program depending upon condition means if the condition is true then the block will execute and if the condition is false then block will not execute.

    Type Of Decision Making Statement in Python

    In Python there are three types of decision making statement.


    1. if Statements In Python

    If statement will execute block of statements only if the condition is true.
    Python 3 Decision Making Statement, if Statement : last night study
    if statements

    Syntax of If Statement In Python

    if(condition):
    statements

    Example of If Statement In Python

    age = 20
    if ( age>= 18):
        print('You can vote')


    Output

    You can vote

    2. if-else Statements In Python

    If else statements will execute one block of statements if condition is true else another block of statement will be executed.
    Python 3 Decision Making Statement, if-else statements : last night study
    if-else statements

    Syntax of if-else Statement In Python

    if(condition):
    statements
    else:
    statements

    Example of if-else Statement In Python

    age =20
    if(age < 18):
       print('you are minor')
    else:
       print('you are adult')


    Output :-

    you are adult


    3. Nested if-else statement

    Nested if-else statement target to another if or else statement
    Python 3 Decision Making Statement, Nested if-else statement : last night study
    Nested if-else statement

    Syntax of Nested if-else In Python

    If (condition):
    statements
    elif (condition):
    statements
    else:
    statements
    When one condition is true its respective block of statement will be executed remaining conditions will be bypassed

    Example of Nested if-else statement In Python

    age =19
    if(age < 18):
       print('You are under 18')
    elif(age == 18):
        print('You are 18')
    else:
       print('You are above 18')
    The Output will be:

    You are above 18

    _________________________________________________________________________________
    EXERCISE
    NOTE
    We know that positive numbers are numbers> zero(0) while negative numbers are numbers< zero(0).
    So, a number entered by a user may be positive, negative or zero.

    Write a program to accept a number from the user and find whether its is positive, negative or zero using  the decision statements.

    This program is to be tried on the computer and also to be written in the register.

    Comments

    Post a Comment

    Popular posts from this blog

    HOLIDAY HW