Press "Enter" to skip to content

Python Notes 5 : Python Statements

Statements

We will deal with a few popular programming statements in here along with their syntax & example usages.

  • if,elif,else Statement

    • A semicolon is to be used after the if statement
    • Indentation needs to be taken care while writing if statements
      • if loc == ‘XYZ’:
        • print ‘This is XYZ’     // Notice the indent of this print statement
      • elif loc == ‘ABC’:
        • print ‘This is ABC’
      • else
        • print ‘This is neither XYZ nor ABC’
  • for loops

    • for loops with Tuple
      • Example 1#
        • tup = (1,2,3,4,5)
        • for t in tup:
          • print t
            • O/P -> 1 2 3 4 5
      • Example 2#
        • l = [(2,4),(6,8),(10,12)]
        • for (t1,t2) in l:
          • print t1
            • O/P -> 2 6 10
    • for loops with dictionaries
      • Example 1# (Python 3)
        • d = {‘k1′:1,’k2′:2,’k3’:3}
        • for k,v in d.items():
          • print (k)
          • print (v)
            • O/P -> k3 3 k2 2 k1 1
      • Example 2# (Python 2)
        • for k,v in d.iteritems():
          • print (k)
          • print (v)
            • O/P -> k3 3 k2 2 k1 1
  • while loops

    • Example #1
      • x=0
      • while x <10:
        • print ‘x is currently’,x
        • x+=1

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *