Python Calculator

Python Calculator

Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.)

Python Calculator

Numbers

The interpreter works as a simple calculator (Python Calculator): you can type an expression at it and it will print the value. Expression syntax is straightforward: the operators +, -, * and / work similar to in most alternative languages (for example, Pascal or C); parentheses (()) may be used for grouping. For example:



    >>> 2 + 2
    4
    >>> 50 - 5*6
    20
    >>> (50 - 5*6) / 4
    5.0
    >>> 8 / 5  # division always returns a floating point number
    1.6

    Integer numbers (such as 2, 4, 20) have type int, float in a numerator (eg 5.0, 1.6). 
    We will see more about numerical types later in the tutorial.
    
    
    Division (/) always returns a float. To split the floor and get integer results 
    (Discarding any partial result) You can use the // operator;
    You can use% to calculate the remainder:
    
    
      >>> 17 / 3  # classic division returns a float
      5.666666666666667
      >>>
      >>> 17 // 3  # floor division discards the fractional part
      5
      >>> 17 % 3  # the % operator returns the remainder of the division
      2
      >>> 5 * 3 + 2  # result * divisor + remainder
      17
      
      It is possible to use the ** operator to calculate powers 1 with Python :
      >>> 5 ** 2  # 5 squared
      25
      >>> 2 ** 7  # 2 to the power of 7
      128
      An equal sign (=) is used to assign a value to a variable. After this, no result is
      displayed before the next interactive prompt:
      
      
      
      
      >>> width = 20
      >>> height = 5 * 9
      >>> width * height
      900
      
      If a variable is "not defined" (a value specified), then trying to use it will give you
      an error:
      
      
      
      >>> n  # try to access an undefined variable
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      NameError: name 'n' is not defined
      
      There is full support for floating-point; Operators with mixed type operands convert
      integer operators to floating points:
      
      
      
      >>> 4 * 3.75 - 1
      14.0
      
      In interactive mode, the last printed expression variable is assigned to _. This means that when you are using Python calculator, it is slightly easier to
      continue calculations, for example:
      
      
      >>> tax = 12.5 / 100
      >>> price = 100.50
      >>> price * tax
      12.5625
      >>> price + _
      113.0625
      >>> round(_, 2)
      113.06
      
      This variable should be treated as read-only. Do not explicitly specify a value for this -
      you will create an independent local variable that will have the same name masking the 
      built-in variable with your magical behavior.
      
      
      

      Strings

      In addition to numbers, Python can also manipulate strings, which can be expressed in
      many ways. They can be enclosed in a single quote ('...') or
      Double Result ("...") with the same result. 2. \ _ can be used to avoid citations:
      
      
      >>> 'spam eggs'  # single quotes
      'spam eggs'
      >>> 'doesn\'t'  # use \' to escape the single quote...
      "doesn't"
      >>> "doesn't"  # ...or use double quotes instead
      "doesn't"
      >>> '"Yes," they said.'
      '"Yes," they said.'
      >>> "\"Yes,\" they said."
      '"Yes," they said.'
      >>> '"Isn\'t," they said.'
      '"Isn\'t," they said.'
      
      In the interactive interpreter, the output string is enclosed in quotes and special
      characters are escaped with a backslash. Although it can sometimes look different from 
      the input (the enclosed quotation may change), the two strings are equivalent. If the 
      string contains a single quote and there are no double quotes, the string is enclosed 
      in double quotes, otherwise, it is enclosed in single quotes.
      The print () function produces a more readable output, except for the enclosed citations 
      and saved and special print Characters:
      
      
      >>> '"Isn\'t," they said.'
      '"Isn\'t," they said.'
      >>> print('"Isn\'t," they said.')
      "Isn't," they said.
      >>> s = 'First line.\nSecond line.'  # \n means newline
      >>> s  # without print(), \n is included in the output
      'First line.\nSecond line.'
      >>> print(s)  # with print(), \n produces a new line
      First line.
      Second line.
      If you do not want characters preceded to be \ interpreted as special characters, you can
      use the raw string by adding r before the first quotation:
      
      
      
      >>> print('C:\some\name')  # here \n means newline!
      C:\some
      ame
      >>> print(r'C:\some\name')  # note the r before the quote
      C:\some\name
      String literals can span multiple lines. One method is using triple-quotes: "" "..." ""
      or '"' ... '".
      The end of the rows is automatically included in the string, but it is possible to stop 
      it by adding a \ "at the end of the row. The following example:
      
      
      print("""\
      Usage: thingy [OPTIONS]
           -h                        Display this usage message
           -H hostname               Hostname to connect to
      """)
      
      Strings can be combined with the + operator (glued together), and repeated with *:
      >>> # 3 times 'un', followed by 'ium'
      >>> 3 * 'un' + 'ium'
      'unununium'
      
      Two or more string literals (ie enclosed between blocks) are automatically leveled next to 
      each other.
      
      
      >>> 'Py' 'thon'
      'Python'
      This feature is particularly useful when you want to break a long strings:
      >>> text = ('Put several strings within parentheses '
      ...         'to have them joined together.')
      >>> text
      'Put several strings within parentheses to have them joined together.'
      
      This only works with two literals though, not with variables or expressions:
      
      
      >>> prefix = 'Py'
      >>> prefix 'thon'  # can't concatenate a variable and a string literal
        File "<stdin>", line 1
          prefix 'thon'
                      ^
      SyntaxError: invalid syntax
      >>> ('un' * 3) 'ium'
        File "<stdin>", line 1
          ('un' * 3) 'ium'
                         ^
      SyntaxError: invalid syntax
      If you want to concatenate variables or a variable and a literal, use +:
      
      
      
      >>> prefix + 'thon'
      'Python'
      
      Strings can be indexed, with the first character index being 0. There is no separate 
      character type; A character is simply a string of one size:
      
      
      
      >>> word = 'Python'
      >>> word[0]  # character in position 0
      'P'
      >>> word[5]  # character in position 5
      'n'
      Indices may also be negative numbers, to start counting from the right:
      
      
      
      >>> word[-1]  # last character
      'n'
      >>> word[-2]  # second-last character
      'o'
      >>> word[-6]
      'P'
      
      Note that since -0 is the same as 0, negative indices start from -1.
      
      
      
      In addition to sequencing, slicing is also supported. Indexing is used to obtain individual 
      characters, slicing allows you to get substrings:
      
      
      >>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
      'Py'
      >>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
      'tho'
      Note how the beginning is always included, and the end is always excluded. This ensures
       that s [: i] + s [i:] is always equal to s:
      
      
      >>> word[:2] + word[2:]
      'Python'
      >>> word[:4] + word[4:]
      'Python'
      
      Slice indices have useful defaults; A omitted first index defaults to zero, an augmented 
      second index default is truncated to the size of the string.
      
      
      >>> word[:2]   # character from the beginning to position 2 (excluded)
      'Py'
      >>> word[4:]   # characters from position 4 (included) to the end
      'on'
      >>> word[-2:]  # characters from the second-last (included) to the end
      'on'
      
      
      One way to remember how the slice works is to think about pointing the indices between 
      characters, with the first character's left edge being 0.. Then the right edge of the last 
      character of a string of n characters is the index n, for example:
      
      
      
       +---+---+---+---+---+---+
       | P | y | t | h | o | n |
       +---+---+---+---+---+---+
       0   1   2   3   4   5   6
      -6  -5  -4  -3  -2  -1
      
      The first line of numbers gives the position of the indices 0… 6 in the string; The second
      line indicates the same negative. Slices from i to j contain all the characters between 
      labeled edges i and j, respectively.
      
      
      
      
      
      
      
      
      
      
      
      
      Python Calculator Python Calculator Reviewed by Sk Jha on October 08, 2019 Rating: 5

      No comments:

      Powered by Blogger.