Simple expressions, Python Programming

Simple expressions

A cornerstone of a programming language is the  ability  to compute expressions.  We will start here  with  arithmetic expressions, just to take the  concept.   An expression consists  of a sequence of 'tokens' that show the application of operators to data component. Each expression has a number,  which  may be computed recursively by evaluating primitive expressions, and then using  standard principles to combine their values  to get new variables. Numerals, such -3.7 as or 6  are expressions, whose  numbers  are  numeric constants. Their numeric values  can be integers, within some  range  dictated by the  programming language, or ?oating point  variables. Floating  point  variables are used  to represent non-integer values,  but they  are not related, in many  important types,  from  the  real  numbers.  There  are in?nitely many real numbers within a ?nite  difference, but  only  finite various  ?oating-point numbers exist at all.  In fact, the normal laws of real arithmetic (transitivity, associativity, etc.)  are created in ?oating-point arithmetic, because  the results of any provided sub-computation may not be representable in the given number of bits.

 

We will explain the calculation of expressions in Python by showing short  transcripts of interactive sessions  with the Python shell : the shell is a computer program that

  • Prompts the user for a relation, by typing »>,
  • Reads what the user types in, and changes it into a set of tokens,
  • Parses the tokens into a data structure showing the syntax of the expression,
  • Computes the parsed expression using an interpreter, and
  • Prints out the resulting value

 

So, for example, we might  have this interaction with Python:

>>> 2 + 3

5

>>> (3 * 8) - 2

22

>>> ((3 * 8) - 2) / 11

2

>>> 2.0

2.0

>>> 0.1

0.10000000000000001

>>> 1.0 / 3.0

0.33333333333333331

>>> 1 / 3

0

There are a couple  of things  to look here.  First, we can look how  ?oating point  values only approximately represent real numbers: when  we write in 0.1, the closest Python can take it in ?oating point  is 0.100000000000001.  The end interaction is particularly troubling: it seems like the value of the expression 1 / 3 should be something like 0.3333.  However, in Python, if both registers to the / operator are integer number, then it will operate an integer division.

 

These expressions can be arbitrarily deeply nested combinations of primitives. The laws used  for calculation are essentially the same as the ones you learned in school; the interpreter proceeds by applying the operations in precedence order13,  calculating sub-expressions to get new variables,  and then calculating the expressions those values  takes in, until in one value  results.

 

Posted Date: 8/11/2012 1:32:28 AM | Location : United States







Related Discussions:- Simple expressions, Assignment Help, Ask Question on Simple expressions, Get Answer, Expert's Help, Simple expressions Discussions

Write discussion on Simple expressions
Your posts are moderated
Related Questions
from urllib2 import urlopen # Open http://placekitten.com/ for reading on line 4! response = kittens.read() body = response[559:1000] # Add your ''print'' statement here!

Procedures as first-class objects In Python, unlike  many  other  languages, methods are behave in much  the same way as num­ bers:  they  can be stored as values  of variable

Structured data We will often  want  to work  with  large  groups of data.   Rather  than  providing  each  number its own  value,  we need  to manage the data  into natural s

Internal models As we want to create  more and more complex  machine with  software programs as controllers, we search  that  it is often needful  to create  additional types

1)    Write a python programmecomparing for and while loops like the following. Math Times table For Loop: 2x1=2 2x2=4 ..... 2x12=24 While Loop: 3x1=3 3x

Models It is a new system that is considerably easier than  the system being modelled, but which saves the important points of the original machine. We might create a physical

This assignment involves writing a program to solve word search puzzles. For example in the following word grid it is possible to find the words active, stock, ethernet and java. N

Programming models Just  as there  are several methods for organizing entire  software systems, there  are  different strategies for formally expressing computational processe

Use the function to compute and print the sum of first n prime numbers.

Booleans   Before we talk about  conditionals, we require  to clarify the Boolean  data  type.  It has two values False and True. Typical statement that have Boolean values