Page 184 - Computer Science Class 11 With Functions
P. 184

8.1.2 Type Conversion Functions

        Pythonnprovideonoeveralnbuilt-innfunctononforntypenconveroion,nfornexample,nint, float, andnstr.nThendatantypeon
        innPythonnarenalooncallednclaooeo.nTondeterminenthentypenofnanyndatanelement,nwenmaynuoentypenfunctonnaonfollowo:
         >>> type(234)
              <class 'int'>
        Thuo,nwenmaynoayn234nionannobjectnofntypenint,norn234nionannobjectnofnclaoonint,norn234nionanninotancenofnclaoonint.n
        Similarly,nnotenthatn234.50nandn'234.50' arenobjectonofntypenfloat andnstr,nreopectvely.
         >>> type(234.50)
              <class 'float'>
         >>> type('234.50')
              <class 'str'>
        Pythonnallowonuontontranoformnobjectonofncertainndatantypeontonotherndatantypeonwhichnarencompatblenfornconveroion,n
        fornexample,n
         >>> int('234')
              234
         >>> str(234)
              '234'
         >>> float('234.50')
              234.5
         >>> int(234.50)
              234
        Notenthatn'234', 234, '234.50', and 234.50narenargumentonfornthenfunctononint, str, float, andn
        int, reopectvely.



                 What value does int(12.53) yield?





        8.2 User-defined Functions
        Suppooenyounwiohntonwritenanprogramn(alooncallednanPythonnocriptnornoimplynanocript)ntonprintnantriangle,nfollowednbynan
        rhombuo,nfollowednbynantrianglenagain.nProgramn8.1nachieveonthionobjectveninnthenfollowingnotepo:n
        1.n Printngnantrianglen(lineon02n-n05)n

        2.n Leavingnanblanknlinen(linen06)
        3.n Printngnanrhombuon(lineon08n-n14)
        4.n Leavingnanblanknlinen(linen15)n
        5.n Printngnantrianglen(lineon17-20)

        Notenthatnlinennumberonarennotnpartnofnthenprogramnandnhavenbeennmentonedntonfacilitatenthendiocuooion.nAloo,nnoten
        thatneachnofnlineon01,n07,nandn16nthatnbeginonwithnthenoymboln#ncomprioeononlynancommentnline.nWhilenexecutngnthen
        Pythonncode,ntheninterpreternignoreonthencommento.nThuo,nexecutonnofnthenprogramnbeginonwithnlinen02,nfollowednbyn
        executonnofnotatementoninnlineon03,n04,n05,n06,n08,n09,n10,n11,n12,n13,n14,n15,n17,n18,n19,nandn20.n

         Program 8.1: Tondioplaynantriangle,nanrhombuo,nandnantrianglenagain.

          01 #Print a triangle
          02 print("     *  ")
          03 print("    ***  ")
          04 print("   *****  ")
          05 print("  *******  ")
          06 print()

         182   Touchpad Computer Science-XI
   179   180   181   182   183   184   185   186   187   188   189