© Valentina Porcu 2018
Valentina PorcuPython for Data Mining Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4113-4_2

2. Introductory Notes

Valentina Porcu1 
(1)
Nuoro, Italy
 

In this chapter we examine Python objects and operators, and learn how to write comments in our code. Including comments in your code is very important for two reasons. First, they serve as a reminder of our thought processes on the work we did weeks or months after we’ve created a script. Second, they help other programmers understand why we did what we did.

Objects in Python

In Python, any item is considered an object, a container to place data. Python objects include tuples, lists, sets, dictionaries, and containers. Python processing is based on objects.

Each object is distinguished by three properties:
  1. 1.

    A name

     
  2. 2.

    A type

     
  3. 3.

    An ID

     

Object names consist of alphanumeric characters and underscores—in other words, all characters from A through Z, a through z, 0 through 9, and _. “Type” is the type of object, such as string, numeric, or Boolean. “ID” is a number that identifies the object uniquely.

Objects remain inside computer memory and can be retrieved. When no longer needed, a garbage-collecting feature frees up the space.

Some IDs and types are assigned automatically. With regard to names, however, note there are some names that cannot be used for objects.

Reserved Terms for the System

Python has a set of words that is reserved for the system and cannot be used in names for objects or functions. These words are the following:

and, as, assert, break, class, continue, def, del, elif, else, except, exec, False, finally, for, from, global, if, import, in, is, lambda, None, not, or, pass, print, raise, return, True, try, while, with, yield

In addition, object names in R are subject to some rules:
  • They must begin with a letter or underscore.

  • They must contain only letters, numbers, and underscores.

  • They are case sensitive. For example, a test object is not the same as a TEST object or a Test object.

Entering Comments in the Code

In Python, any comment preceded by the # symbol is not read by the program as code; it is ignored. This feature is useful for commenting on code and refreshing our memories later. Comments can be written both in the code and to the side.
# comment no. 1
>>> print("Hello World") # comment no. 2
To write a comment on multiple lines, we can also use three quotation marks, like this:
"""
        comment line 1
        comment line 2
        comment line 3
"""

Types of Data

Python data are various types and are summarized in Table 2-1.
Table 2-1

Python Data Types

Data Type

Example

Integer (int)

1, 20, –19

Float

1.7, 12.54

Complex

657.23e+34

String (str)

"Hello World", 'string1', """ string2 """

List

list = ['a', 'b', 'c' ]

Tuple

tuple = ('Laura', 29, 'Andrea', 4)

Dictionary

dictionary = {'name' : 'Simon', 'key': 'D007'}

To determine object type, we use the type() function:
# we create an x object
>>> x = 1
>>> type(x)
<class 'int'>
# a y object
>>> y = 20.75
>>> type(y)
<class 'float'>
# and a z object
>>> z = "test"
>>> type(z)
<class 'str'>

File Format

After you create a script in Python, you need to save it with a .py extension. Typically, when it comes to complex scripts, you create a script on an editor that you then run. A .py script can be written using any one of the different editors discussed earlier—even a normal text editor—and then can be renamed with .py extension.

Operators

Python includes a series of operators that are divided into several groups:
  • Mathematical

  • Comparison

  • Membership

  • Bitwise

  • Assignment

  • Logical

  • Identity

Beside these operators, there is also a hierarchy that marks the order in which they are used. Operators are very important for arithmetic, or even to extract some data with specific characteristics.

Mathematical Operators

When we open Python, the simplest thing we can do is use it to perform mathematical operations, for which we use the mathematical operators shown in Table 2-2.
Table 2-2

Mathematical Operators

Operator

Description

Example

+

Addition

3 + 2 = 5

Subtraction

10 − 4 = 6

*

Multiplication

4 * 3 = 12

/

Division

20/2 = 10

%

Modulo

21/2 = 1

**

Exponentiation

3 ** 2 = 9

//

Floor

10.5 // 2 = 5.0

Let’s open Python and perform some mathematical operations:
>>> 10+7
17
>>> 15-2
13
>>> 2*3
6
>>> 10/2
5
>>> 3**3
27
>>> 10/3
3
>>> 25//7
3

Comparison and Membership Operators

In Python we also have comparison operators (comparators) and membership operators (Table 2-3).
Table 2-3

Comparison and Membership Operators

Operator

Description

>

Greater than

<

Less than

==

Equal to

>=

Greater than or equal to

<=

Less than or equal to

!=

Different

is

Identity

is not

Non identity

in

Exists in

not in

Does not exist in

These operators are used to test relationships between objects. For example,
# we create two objects
>>> x = 5
>>> y = 10
# let us verify that x is greater than y
>>> x > y
False
# the output is a logical vector that tells us x is not greater than y
# let us see if x is less than y
>>> x < y
True
# this time the answer is affirmative
# we create another object, z, with the same value as x
>>> z = 5
# let us verify with the equality operator whether z is equal to x
>>> z == x
True
# in this case, the output is positive
# let us verify whether z is different from y
>>> z != y
True
# in this case, the output is also positive
# let us create a tuple
>>> v1 = (1,2,3,4,5,6,7)
# and verify whether the number 2 is in the tuple
>>> 2 in v1
True
# let us verify whether the number 8 is NOT in tuple v1
>>> 8 not in v1
True
# let us verify whether the number 7 is NOT in tuple v1
>>> 7 not in v1
False

Python compares text strings lexicographically using, for example, the ASCII value of the characters. We cannot compare strings and numbers because we would throw an error.

Bitwise Operators

The bitwise operators shown in Table 2-4 are useful when specifying more than one condition. An example is when we need to extract data from an object, such as a dataset.
Table 2-4

Bitwise Operators

Operator

Description

& or and

And

| or or

Or

^

Xor

~

Bitwise not

<<

Left shift

>>

Right shift

Bitwise operators can be used together with comparators. Let’s look at some examples:
>>> 3 < 4 and 4 > 3
True
# and also
>>> 3 < 4 & 4 > 3
True
# here is an example with or (|)
>>> 3 < 4 | 4 > 3
True
# at least one of the statements must be valid
>>> 3 == 4 or 4 > 3
True

Assignment Operators

Assignment operators are used to assign a name to a given object (Table 2-5).
Table 2-5

Assignment Operators

Operator

Description

Example

=

Basic assignment operator

x = 5 + 6

+=

Adds an element and assigns the result to the name

x += y (corresponds to x = x + y)

-=

Subtracts an element and assigns the result to the name

x -= y (corresponds to x = x – y)

/=

Divides an element and assigns the result to the name

x /= y (corresponds to x = x/y)

*=

Multiplies an element and assigns the result to the name

x *= y (corresponds to x = x * y)

%=

Modulo and reassignment

x %= y (corresponds to x = x % y)

**=

Exponentiation and reassignment

x **= y (corresponds to x = x ** y)

//=

Floor division and reallocation

x//=y (corresponds to x = x//y)

Let’s look at some examples:
# we create an x object with the value 10
>>> x = 10
# sum x and overwrite the variable x again with the same name
>>> x = x + 5
>>> x
15
# let's try "+="
>>> x += 5
>>> x
20
# and now "-="
>>> x -= 5
>>> x
15
# now let's use the operator "*="
>>> x *= 3
>>> x
45
# and the operator "/="
>>> x /= 3
>>> x
15
# then the operator "**="
>>> x **= 2
>>> x
225
# and finally the operator "//="
>>> x //= 2
>>> x
112

Each time, Python performs the operation and records the result in the x object.

Operator Order

When it comes to mathematical operators, we must be aware that there is an order priority that must be observed when case brackets are not inserted. This is similar to mathematical operations in which multiplication takes precedence over addition. Table 2-6 lists some of the priority rules that govern the order of operations.
Table 2-6

Priority Rules for Operators

Operator

Priority (highest to lowest)

**

Exponentiation has the highest priority

-

Denial

* / // %

Multiplication, division, modulo, floor division

+ -

Addition and subtraction

>> <<

Bitwise right and left

&

Bitwise AND

^ |

Bitwise OR

<=, >, <, >=

Less than, more than, smaller, bigger than

== !=

Equal, different

= += -= *= /= %= **= //=

Assignment operators

is / is not

Comparison

in / not in

Comparison

not / or / and

Comparison

Indentation

Python uses indentation to limit blocks of instructions, which makes the code more readable. Code blocks are thus defined by indentation. Typically an indentation corresponds to four spaces.

Let’s look at an example of indentation in a function:
>>> def multiply_xy(x, y):
...   "'let's multiply x and y
...   "'
...   return(x*y)
>>> multiply_xy(5,6)
30

Quotation Marks

Quotation marks in R are used primarily to define strings. They can be single, double, or triple. Triple quotation marks are used to wrap words and insert comments on multiple lines. An example of this is when we wish to include documentation within a function we are creating.
>>> ex1 = 'single quote'
>>> ex2 = "double quote"
>>> ex3 = """
        text string 1
        text string 2
        text string 3
"""

We examine string management in Chapter 5.

Summary

In this chapter, we studied commenting and operators. When we write code for data analysis, it is important that we include comments not only as a reminder to ourselves but also as a guide for other programmers. Operators, and their ranking, help us create and define our code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.239.44