Servlet NewJavaServlet Test



Yüklə 11,16 Mb.
Pdf görüntüsü
səhifə14/97
tarix07.11.2018
ölçüsü11,16 Mb.
#78896
1   ...   10   11   12   13   14   15   16   17   ...   97

CHAPTER 1 

 LANGUAGE AND SYNTAX 
 

 
Operators  
The operators that are used by Python are very similar to those used in other languages...straightforward 
and easy to use. As with any other language, you have your normal operators such as +, -, *, and /, which 
are available for performing calculations. As you can see from the following examples, there is no special 
trick to using any of these operators. 
Listing 1-7. Performing Integer-based Operations 
>>> x = 9 
>>> y = 2 
>>> x + y 
11 
>>> x - y 

>>> x * y 
18 
>>> x / y 

Perhaps the most important thing to note with calculations is that if you are performing calculations 
based on integer values then you will receive a rounded result. If you are performing calculations based 
upon floats then you will receive float results, and so on. 
Listing 1-8. Performing Float-based Operations 
>>> x = 9.0 
>>> y = 2.0 
>>> x + y 
11.0 
>>> x - y 
7.0 
>>> x * y 
18.0 
>>> x / y 
4.5 
It is important to note this distinction because as you can see from the differences in the results of 
the division (/) operations in Listings 1-7 and 1-8, we have rounding on the integer values and not on the 
float. A good rule of thumb is that if your application requires precise calculations to be defined, then it 
is best to use float values for all of your numeric variables, or else you will run into a rounding issue. In 
Python 2.5 and earlier, integer division always rounds down, producing the floor as the result. In Python 
2.2, the // operator was introduced which is another way to obtain the floor result when dividing 
integers or floats. This operator was introduced as a segue way for changing integer division in future 
releases so that the result would be a true division. In Chapter 3, we’ll discuss division using a technique 
that always performs true division. 
Expressions 
Expressions are just what they sound like. They are a piece of Python code that can be evaluated and 
produces a value. Expressions are not instructions to the interpreter, but rather a combination of values 
www.it-ebooks.info


CHAPTER 1 
■ 
LANGUAGE AND SYNTAX 
 

 
and operators that are evaluated. If we wish to perform a calculation based upon two variables or 
numeric values then we are producing an expression. 
Listing 1-9. Examples of Expressions 
>>> x + y 
>>> x - y 
>>> x * y 
>>> x / y 
The examples of expressions that are shown above are very simplistic. Expressions can be made to 
be very complex and perform powerful computations. They can be combined together to produce 
complex results.   
Functions 
Oftentimes it is nice to take suites of code that perform specific tasks and extract them into their own 
unit of functionality so that the code can be reused in numerous places without retyping each time. A 
common way to define a reusable piece of code is to create a function. Functions are named portions of 
code that perform that usually perform one or more tasks and return a value. In order to define a 
function we use the def statement. 
The def statement will become second nature for usage throughout any Python programmer’s life. 
The def statement is used to define a function. Here is a simple piece of pseudocode that shows how to 
use it. 
Listing 1-10. 
def my_function_name(parameter_list): 
    implementation 
The pseudocode above demonstrates how one would use the def statement, and how to construct a 
simple function. As you can see, def precedes the function name and parameter list when defining a 
function.  
Listing 1-11. 
>>> def my_simple_function(): 
...     print 'This is a really basic function' 
...  
>>> my_simple_function() 
This is a really basic function 
This example is about the most basic form of function that can be created. As you can see, the 
function contains one line of code which is a print statement. We will discuss the print statement in 
more detail later in this chapter; however, all you need to know now is that it is used to print some text to 
the screen. In this case, we print a simple message whenever the function is called. 
Functions can accept parameters, or other program variables, that can be used within the context of 
the function to perform some task and return a value. 
www.it-ebooks.info


CHAPTER 1 

 LANGUAGE AND SYNTAX 
 
10 
 
Listing 1-12. 
>>> def multiply_nums(x, y): 
...     return x * y 
...  
>>> multiply_nums(25, 7) 
175 
As seen above, parameters are simply variables that are assigned when the function is called. 
Specifically, we assign 25 to x and 7 to y in the example. The function then takes x and y, performs a 
calculation and returns the result. 
Functions in Python are just like other variables and they be passed around as parameters to other 
functions if needed. Here we show a basic example of passing one function to another function. We’ll 
pass the multiply_nums function into the function below and then use it to perform some calculations. 
Listing 1-13. 
>>> def perform_math(oper):    
...     return oper(5, 6) 
...  
>>> perform_math(multiply_nums) 
30 
Although this example is very basic, you can see that another function can be passed as a parameter 
and then used within another function. For more detail on using def and functions, please take a look at 
Chapter 4, which is all about functions. 
Classes 
Python is an object-oriented programming language. which means that everything in the language is an 
object of some type. Much like building blocks are used for constructing buildings, each object in Python 
can be put together to build pieces of programs or entire programs. This section will give you a brief 
introduction to Python classes, which are one of the keys to object orientation in this language. 
Classes are defined using the class keyword. Classes can contain functions, methods, and variables. 
Methods are just like functions in that the def keyword is used to create them, and they accept 
parameters. The only difference is that methods take a parameter known as self that refers to the object 
to which the method belongs. Classes contain what is known as an initializer method, and it is called 
automatically when a class is instantiated. Let’s take a look at a simple example and then explain it. 
www.it-ebooks.info


Yüklə 11,16 Mb.

Dostları ilə paylaş:
1   ...   10   11   12   13   14   15   16   17   ...   97




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə