未加星标

A Whirlwind Tour of Python

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

A Whirlwind Tour of Python
Bikers making a turn

Conceived in the late 1980s as a teaching and scripting language, python has since become an essential tool for many programmers, engineers, researchers, and data scientists across academia and industry. As an astronomer focused on building and promoting the free open tools for data-intensive science, I've found Python to be a near-perfect fit for the types of problems I face day to day, whether it's extracting meaning from large astronomical datasets, scraping and munging data sources from the Web, or automating day-to-day research tasks.

The appeal of Python is in its simplicity and beauty, as well as the convenience of the large ecosystem of domain-specific tools that have been built on top of it. For example, most of the Python code in scientific computing and data science is built around a group of mature and useful packages:

NumPy provides efficient storage and computation for multidimensional data arrays. SciPy contains a wide array of numerical tools such as numerical integration and interpolation. Pandas provides a DataFrame object along with a powerful set of methods to manipulate, filter, group, and transform data. Matplotlib provides a useful interface for creation of publication-quality plots and figures. Scikit-Learn provides a uniform toolkit for applying common machine learning algorithms to data. IPython/Jupyter provides an enhanced terminal and an interactive notebook environment that is useful for exploratory analysis, as well as creation of interactive, executable documents. For example, the manuscript for this report was composed entirely in Jupyter notebooks.

No less important are the numerous other tools and packages which accompany these: if there is a scientific or data analysis task you want to perform, chances are someone has written a package that will do it for you.

To tap into the power of this data science ecosystem, however, first requires familiarity with the Python language itself. I often encounter students and colleagues who have (sometimes extensive) backgrounds in computing in some language―MATLAB, IDL, R, Java, C++, etc.―and are looking for a brief but comprehensive tour of the Python language that respects their level of knowledge rather than starting from ground zero. This report seeks to fill that niche.

As such, this report in no way aims to be a comprehensive introduction to programming, or a full introduction to the Python language itself; if that is what you are looking for, you might check out one of the recommended references listed inResources for Further Learning. Instead, this will provide a whirlwind tour of some of Python's essential syntax and semantics, built-in data types and structures, function definitions, control flow statements, and other aspects of the language. My aim is that readers will walk away with a solid foundation from which to explore the data science stack just outlined.

Installation and Practical Considerations

Installing Python and the suite of libraries that enable scientific computing is straightforward whether you use windows, linux, or Mac OS X. This section will outline some of the considerations when setting up your computer.

Python 2 versus Python 3

This report uses the syntax of Python 3, which contains language enhancements that are not compatible with the 2. x series of Python. Though Python 3.0 was first released in 2008, adoption has been relatively slow, particularly in the scientific and web development communities. This is primarily because it took some time for many of the essential packages and toolkits to be made compatible with the new language internals. Since early 2014, however, stable releases of the most important tools in the data science ecosystem have been fully compatible with both Python 2 and 3, and so this report will use the newer Python 3 syntax. Even though that is the case, the vast majority of code snippets in this report will also work without modification in Python 2: in cases where a Py2-incompatible syntax is used, I will make every effort to note it explicitly.

Installation with conda

Though there are various ways to install Python, the one I would suggest―particularly if you wish to eventually use the data science tools mentioned earlier―is via the cross-platform Anaconda distribution. There are two flavors of the Anaconda distribution:

Miniconda gives you the Python interpreter itself, along with a command-line tool called conda which operates as a cross-platform package manager geared toward Python packages, similar in spirit to the apt or yum tools that Linux users might be familiar with.

Anaconda includes both Python and conda , and additionally bundles a suite of other pre-installed packages geared toward scientific computing.

Any of the packages included with Anaconda can also be installed manually on top of Miniconda; for this reason, I suggest starting with Miniconda.

To get started, download and install the Miniconda package―make sure to choose a version with Python 3―and then install the IPython notebook package:

[~]$ conda install ipython-notebook

For more information on conda , including information about creating and using conda environments, refer to the Miniconda package documentation linked at the above page.

The Zen of Python

Python aficionados are often quick to point out how "intuitive", "beautiful", or "fun" Python is. While I tend to agree, I also recognize that beauty, intuition, and fun often go hand in hand with familiarity, and so for those familiar with other languages such florid sentiments can come across as a bit smug. Nevertheless, I hope that if you give Python a chance, you’ll see where such impressions might come from. And if you really want to dig into the programming philosophy that drives much of the coding practice of Python power users, a nice little Easter egg exists in the Python interpreter―simply close your eyes, meditate for a few minutes, and run import this :

In [1]: import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one--and preferably only one--obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea--let's do more of those!

With that, let’s start our tour of the Python language.

How to Run Python Code

Python is a flexible language, and there are several ways to use it depending on your particular task. One thing that distinguishes Python from other programming languages is that it is interpreted rather than compiled . This means that it is executed line by line, which allows programming to be interactive in a way that is not directly possible with compiled languages like Fortran, C, or Java. This section will describe four primary ways you can run Python code: the Python interpreter , the IPython interpreter , via self-contained scripts , or in the Jupyter notebook .

The Python interpreter

The most basic way to execute Python code is line by line within the Python interpreter . The Python interpreter can be started by installing the Python language (see the previous section) and typing python at the command prompt (look for the Terminal on Mac OS X and Unix/Linux systems, or the Command Prompt application in Windows):

$ python Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7... Type "help", "copyright", "credits" or "license" for more... >>>

With the interpreter running, you can begin to type and execute code snippets. Here we’ll use the interpreter as a simple calculator, performing calculations and assigning values to variables:

>>> 1 + 1 2 >>> x = 5 >>> x * 3 15

The interpreter makes it very convenient to try out small snippets of Python code and to experiment with short sequences of operations.

The IPython interpreter

If you spend much time with the basic Python interpreter, you’ll find that it lacks many of the features of a full-fledged interactive development environment. An alternative interpreter called IPython (for Interactive Python) is bundled with the Anaconda distribution, and includes a host of convenient enhancements to the basic Python interpreter. It can be started by typing ipython at the command prompt:

$ ipython Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7... Type "copyright", "credits" or "license" for more information. IPython 4.0.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra... In [1]: The main aesthetic difference between the Python interpreter and the enhanced IPython interpreter lies in the command prompt: Python uses >>> by default, while IPython uses numbered commands (e.g., In [1]: ). Regardless, we can execute code line by line just as we did before: In [1]: 1 + 1 Out[1]: 2 In [2]: x = 5 In [3]: x * 3 Out[3]: 15

Note that just as the input is numbered, the output of each command is numbered as well. IPython makes available a wide array of useful features; for some suggestions on where to read more, seeResources for Further Learning.

Self-contained Python scripts

Running Python snippets line by line is useful in some cases, but for more complicated programs it is more convenient to save code to file, and execute it all at once. By convention, Python scripts are saved in files with a .py extension. For example, let’s create a script called test.py that contains the following:

# file: test.py print("Running test.py") x = 5 print("Result is", 3 * x)

To run this file, we make sure it is in the current directory and type python filename at the command prompt:

$ python test.py Running test.py Result is 15

For more complicated programs, creating self-contained scripts like this one is a must.

The Jupyter notebook

A useful hybrid of the interactive terminal and the self-contained script is the Jupyter notebook , a document format that allows executable code, formatted text, graphics, and even interactive features to be combined into a single document. Though the notebook began as a Python-only format, it has since been made compatible with a large number of programming languages, and is now an essential part of the Jupyter Project . The notebook is useful both as a development environment and as a means of sharing work via rich computational and data-driven narratives that mix together code, figures, data, and text.

A Quick Tour of Python Language Syntax

Python was originally developed as a teaching language, but its ease of use and clean syntax have led it to be embraced by beginners and experts alike. The cleanliness of Python’s syntax has led some to call it "executable pseudocode", and indeed my own experience has been that it is often much easier to read and understand a Python script than to read a similar script written in, say, C. Here we’ll begin to discuss the main features of Python’s syntax.

Syntax refers to the structure of the language (i.e., what constitutes a correctly formed program). For the time being, we won’t focus on the semantics―the meaning of the words and symbols within the syntax―but will return to this at a later point.

Consider the following code example:

In [1]: # set the midpoint midpoint = 5 # make two empty lists lower = []; upper = [] # split the numbers into lower and upper for i in range(10): if (i < midpoint): lower.append(i) else: upper.append(i) print("lower:", lower) print("upper:", upper) lower: [0, 1, 2, 3, 4] upper: [5, 6, 7, 8, 9]

This script is a bit silly, but it compactly illustrates several of the important aspects of Python syntax. Let’s walk through it and discuss some of the syntactical features of Python.

Comments Are Marked by #

The script starts with a comment:

# set the midpoint

Comments in Python are indicated by a pound sign ( # ), and anything on the line following the pound sign is ignored by the interpreter. This means, for example, that you can have standalone comments like the one just shown, as well as inline comments that follow a statement. For example:

x += 2 # shorthand for x = x + 2

Python does not have any syntax for multiline comments, such as the /* ... */ syntax used in C and C++, though multiline strings are often used as a replacement for multiline comments (more on this inString Manipulation and Regular Expressions).

End-of-Line Terminates a Statement

The next line in the script is

midpoint = 5

This is an assignment operation, where we’ve created a variable named midpoint and assigned it the value 5 . Notice that the end of this statement is simply marked by the end of the line. This is in contrast to languages like C and C++, where every statement must end with a semicolon ( ; ).

In Python, if you’d like a statement to continue to the next line, it is possible to use the \ marker to indicate this:

In [2]: x = 1 + 2 + 3 + 4 +\ 5 + 6 + 7 + 8

It is also possible to continue expressions on the next line within parentheses, without using the \ marker:

In [3]: x = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)

Most Python style guides recommend the second version of line continuation (within parentheses) to the first (use of the \ marker).

Semicolon Can Optionally Terminate a Statement

Sometimes it can be useful to put multiple statements on a single line. The next portion of the script is:

lower = []; upper = []

This shows the example of how the semicolon ( ; ) familiar in C can be used optionally in Python to put two statements on a single line. Functionally, this is entirely equivalent to writing:

lower = [] upper = []

Using a semicolon to put multiple statements on a single line is generally discouraged by most Python style guides, though occasionally it proves convenient.

Indentation: Whitespace Matters!

Next, we get to the main block of code:

for i in range(10): if i < midpoint: lower.append(i) else: upper.append(i)

This is a compound control-flow statement including a loop and a conditional―we’ll look at these types of statements in a moment. For now, consider that this demonstrates what is perhaps the most controversial feature of Python’s syntax: whitespace is meaningful!

In programming languages, a block of code is a set of statements that should be treated as a unit. In C, for example, code blocks are denoted by curly braces:

// C code for(int i=0; i<100; i++) { // curly braces indicate code block total += i; }

In Python, code blocks are denoted by indentation :

for i in range(100): # indentation indicates code block total += i

In Python, indented code blocks are always preceded by a colon ( : ) on the previous line.

The use of indentation helps to enforce the uniform, readable style that many find appealing in Python code. But it might be confusing to the uninitiated; for example, the following two snippets will produce different results:

>>> if x < 4: >>> if x < 4: ... y = x * 2 ... y = x * 2 ... print(x) ... print(x)

In the snippet on the left, print(x) is in the indented block, and will be executed only if x is less than 4 . In the snippet on the right, print(x) is outside the block, and will be executed regardless of the value of x !

Python’s use of meaningful whitespace often is surprising to programmers who are accustomed to other languages, but in practice it can lead to much more consistent and readable code than languages that do not enforce indentation of code blocks. If you find Python’s use of whitespace disagreeable, I’d encourage you to give it a try: as I did, you may find that you come to appreciate it.

Finally, you should be aware that the amount of whitespace used for indenting code blocks is up to the user, as long as it is consistent throughout the script. By convention, most style guides recommend to indent code blocks by four spaces, and that is the convention we will follow in this report. Note that many text editors like Emacs and Vim contain Python modes that do four-space indentation automatically.

Whitespace Within Lines Does Not Matter

While the mantra of meaningful whitespace holds true for whitespace before lines (which indicate a code block), whitespace within lines of Python code does not matter. For example, all three of these expressions are equivalent:

In [4]: x=1+2 x = 1 + 2 x = 1 + 2

Abusing this flexibility can lead to issues with code readability―in fact, abusing whitespace is often one of the primary means of intentionally obfuscating code (which some people do for sport). Using whitespace effectively can lead to much more readable code, especially in cases where operators follow each other―compare the following two expressions for exponentiating by a negative number:

x=10**-2

to

x = 10 ** -2

I find the second version with spaces much more easily readable at a single glance. Most Python style guides recommend using a single space around binary operators, and no space around unary operators. We’ll discuss Python’s operators further inBasic Python Semantics: Variables and Objects.

Parentheses Are for Grouping or Calling

In the following code snippet, we see two uses of parentheses. First, they can be used in the typical way to group statements or mathematical operations:

In [5]: 2 * (3 + 4) Out [5]: 14

They can also be used to indicate that a function is being called. In the next snippet, the print() function is used to display the contents of a variable (see the sidebar that follows). The function call is indicated by a pair of opening and closing parentheses, with the arguments to the function contained within:

In [6]: print('first value:', 1) first value: 1 In [7]: print('second value:', 2) second value: 2

Some functions can be called with no arguments at all, in which case the opening and closing parentheses still must be used to indicate a function evaluation. An example of this is the sort method of lists:

In [8]: L = [4,2,3,1] L.sort() print(L) [1, 2, 3, 4]

The () after sort indicates that the function should be executed, and is required even if no arguments are necessary.

Finishing Up and Learning More

This has been a very brief exploration of the essential features of Python syntax; its purpose is to give you a good frame of reference for when you’re reading the code in later sections. Several times we’ve mentioned Python "style guides," which can help teams to write code in a consistent style. The most widely used style guide in Python is known as PEP8, and can be found at https://www.python.org/dev/peps/pep-0008/ . As you begin to write more Python code, it would be useful to read through this! The style suggestions contain the wisdom of many Python gurus, and most suggestions go beyond simple pedantry: they are experience-based recommendations that can help avoid subtle mistakes and bugs in your code.

Basic Python Semantics: Variables and Objects

This section will begin to cover the basic semantics of the Python language. As opposed to the syntax covered in the previous section, the semantics of a language involve the meaning of the statements. As with our discussion of syntax, here we’ll preview a few of the essential semantic constructions in Python to give you a better frame of reference for understanding the code in the following sections .

This section will cover the semantics of variables and objects , which are the main ways you store, reference, and operate on data within a Python script.

Python Variables Are Pointers

Assigning variables in Python is as easy as putting a variable name to the left of the equals sign ( = ):

# assign 4 to the variable x x = 4

This may seem straightforward, but if you have the wrong mental model of what this operation does, the way Python works may seem confusing. We’ll briefly dig into that here.

In many programming languages, variables are best thought of as containers or buckets into which you put data. So in C, for example, when you write

// C code int x = 4;

you are essentially defining a "memory bucket" named x , and putting the value 4 into it. In Python, by contrast, variables are best thought of not as containers but as pointers. So in Python, when you write

x = 4

you are essentially defining a pointer named x that points to some other bucket containing the value 4 . Note one consequence of this: because Python variables just point to various objects, there is no need to "declare" the variable, or even require the variable to always point to information of the same type! This is the sense in which people say Python is dynamically typed : variable names can point to objects of any type. So in Python, you can do things like this:

In [1]: x = 1 # x is an integer x = 'hello' # now x is a string x = [1, 2, 3] # now x is a list

While users of statically typed languages might miss the type-safety that comes with declarations like those found in C,

int x = 4;

this dynamic typing is one of the pieces that makes Python so quick to write and easy to read.

There is a consequence of this "variable as pointer" approach that you need to be aware of. If we have two variable names pointing to the same mutable object, then changing one will change the other as well! For example, let’s create and modify a list:

In [2]: x = [1, 2, 3] y = x

We’ve created two variables x and y that both point to the same object. Because of this, if we modify the list via one of its names, we’ll see that the "other" list will be modified as well:

In [3]: print(y) [1, 2, 3] In [4]: x.append(4) # append 4 to the list pointed to by x print(y) # y's list is modified as well! [1, 2, 3, 4]

This behavior might seem confusing if you’re wrongly thinking of variables as buckets that contain data. But if you’re correctly thinking of variables as pointers to objects, then this behavior makes sense.

Note also that if we use = to assign another value to x , this will not affect the value of y ―assignment is simply a change of what object the variable points to:

In [5]: x = 'something else' print(y) # y is unchanged [1, 2, 3, 4]

Again, this makes perfect sense if you think of x and y as pointers, and the = operator as an operation that changes what the name points to.

You might wonder whether this pointer idea makes arithmetic operations in Python difficult to track, but Python is set up so that this is not an issue. Numbers, strings, and other simple types are immutable: you can’t change their value―you can only change what values the variables point to. So, for example, it’s perfectly safe to do operations like the following:

In [6]: x = 10 y = x x += 5 # add 5 to x's value, and assign it to x print("x =", x) print("y =", y) x = 15 y = 10

When we call x += 5 , we are not modifying the value of the 5 object pointed to by x , but rather we are changing the object to which x points. For this reason, the value of y is not affected by the operation.

Everything Is an Object

Python is an object-oriented programming language, and in Python everything is an object.

Let’s flesh out what this means. Earlier we saw that variables are simply pointers, and the variable names themselves have no attached type information. This leads some to claim erroneously that Python is a type-free language. But this is not the case! Consider the following:

In [7]: x = 4 type(x) Out [7]: int In [8]: x = 'hello' type(x) Out [8]: str In [9]: x = 3.14159 type(x) Out [9]: float

Python has types; however, the types are linked not to the variable names but to the objects themselves .

In object-oriented programming languages like Python, an object is an entity that contains data along with associated metadata and/or functionality. In Python, everything is an object, which means every entity has some metadata (called attributes ) and associated functionality (called methods ). These attributes and methods are accessed via the dot syntax.

For example, before we saw that lists have an append method, which adds an item to the list, and is accessed via the dot syntax ( . ):

In [10]: L = [1, 2, 3] L.append(100) print(L) [1, 2, 3, 100]

While it might be expected for compound objects like lists to have attributes and methods, what is sometimes unexpected is that in Python even simple types have attached attributes and methods. For example, numerical types have a real and imag attribute that return the real and imaginary part of the value, if viewed as a complex number:

In [11]: x = 4.5 print(x.real, "+", x.imag, 'i') 4.5 + 0.0 i

Methods are like attributes, except they are functions that you can call using a pair of opening and closing parentheses. For example, floating-point numbers have a method called is_integer that checks whether the value is an integer:

In [12]: x = 4.5 x.is_integer() Out [12]: False In [13]: x = 4.0 x.is_integer() Out [13]: True

When we say that everything in Python is an object, we really mean that everything is an object―even the attributes and methods of objects are themselves objects with their own type information:

In [14]: type(x.is_integer) Out [14]: builtin_function_or_method

We’ll find that the everything-is-object design choice of Python allows for some very convenient language constructs.

Basic Python Semantics: Operators

In the previous section, we began to look at the semantics of Python variables and objects; here we’ll dig into the semantics of the various operators included in the language. By the end of this section, you’ll have the basic tools to begin comparing and operating on data in Python.

Arithmetic Operations

Python implements seven basic binary arithmetic operators, two of which can double as unary operators. They are summarized in the following table:

Operator Name Description

a + b

Addition

Sum of a and b

a - b

Subtraction

Difference of a and b

a * b

Multiplication

Product of a and b

a / b

True division

Quotient of a and b

a // b

Floor division

Quotient of a and b , removing fractional parts

a % b

Modulus

Remainder after division of a by b

a ** b

Exponentiation

a raised to the power of b

-a

Negation

The negative of a

+a

Unary plus

a unchanged (rarely used)

These operators can be used and combined in intuitive ways, using standard parentheses to group operations. For example:

In [1]: # addition, subtraction, multiplication (4 + 8) * (6.5 - 3) Out [1]: 42.0

Floor division is true division with fractional parts truncated:

In [2]: # True division print(11 / 2) 5.5 In [3]: # Floor division print(11 // 2)

The floor division operator was added in Python 3; you should be aware if working in Python 2 that the standard division operator ( / ) acts like floor division for integers and like true division for floating-point numbers.

Finally, I’ll mention that an eighth arithmetic operator was added in Python 3.5: the a @ b operator, which is meant to indicate the matrix product of a and b , for use in various linear algebra packages.

Bitwise Operations

In addition to the standard numerical operations, Python includes operators to perform bitwise logical operations on integers. These are much less commonly used than the standard arithmetic operations, but it’s useful to know that they exist. The six bitwise operators are summarized in the following table:

Operator Name Description

a &amp; b

Bitwise AND

Bits defined in both a and b

a | b

Bitwise OR

Bits defined in a or b or both

a ^ b

Bitwise XOR

Bits defined in a or b but not both

a &lt;&lt; b

Bit shift left

Shift bits of a left by b units

a &gt;&gt; b

Bit shift right

Shift bits of a right by b units

~a

Bitwise NOT

Bitwise negation of a

These bitwise operators only make sense in terms of the binary representation of numbers, which you can see using the built-in bin function:

In [4]: bin(10) Out [4]: '0b1010'

The result is prefixed with 0b , which indicates a binary representation. The rest of the digits indicate that the number 10 is expressed as the sum:

1 2 3 + 0 2 2 + 1 2 1 + 0 2 0

Similarly, we can write:

In [5]: bin(4) Out [5]: '0b100'

Now, using bitwise OR, we can find the number which combines the bits of 4 and 10:

In [6]: 4 | 10 Out [6]: 14 In [7]: bin(4 | 10) Out [7]: '0b1110'

These bitwise operators are not as immediately useful as the standard arithmetic operators, but it’s helpful to see them at least once to understand what class of operation they perform. In particular, users from other languages are sometimes tempted to use XOR (i.e., a ^ b ) when they really mean exponentiation (i.e., a ** b ).

Assignment Operations

We’ve seen that variables can be assigned with the = operator, and the values stored for later use. For example:

In [8]: a = 24 print(a)

We can use these variables in expressions with any of the operators mentioned earlier. For example, to add 2 to a we write:

In [9]: a + 2 Out [9]: 26

We might want to update the variable a with this new value; in this case, we could combine the addition and the assignment and write a = a + 2 . Because this type of combined operation and assignment is so common, Python includes built-in update operators for all of the arithmetic operations:

In [10]: a += 2 # equivalent to a = a + 2 print(a)

There is an augmented assignment operator corresponding to each of the binary operators listed earlier; in brief, they are:

a += b

a -= b

a *= b

a /= b

a //= b

a %= b

a **= b

a &amp;= b

a |= b

a ^= b

a &lt;&lt;= b

a &gt;&gt;= b

Each one is equivalent to the corresponding operation followed by assignment: that is, for any operator # , the expression a #= b is equivalent to a = a # b , with a slight catch. For mutable objects like lists, arrays, or DataFrames, these augmented assignment operations are actually subtly different than their more verbose counterparts: they modify the contents of the original object rather than creating a new object to store the result.

Comparison Operations

Another type of operation that can be very useful is comparison of different values. For this, Python implements standard comparison operators, which return Boolean values True and False . The comparison operations are listed in the following table:

Operation Description

a == b

a equal to b

a != b

a not equal to b

a &lt; b

a less than b

a &gt; b

a greater than b

a &lt;= b

a less than or equal to b

a &gt;= b

a greater than or equal to b

These comparison operators can be combined with the arithmetic and bitwise operators to express a virtually limitless range of tests for the numbers. For example, we can check if a number is odd by checking that the modulus with 2 returns 1:

In [11]: # 25 is odd 25 % 2 == 1 Out [11]: True In [12]: # 66 is odd 66 % 2 == 1 Out [12]: False

We can string together multiple comparisons to check more complicated relationships:

In [13]: # check if a is between 15 and 30 a = 25 15 < a < 30 Out [13]: True

And, just to make your head hurt a bit, take a look at this comparison:

In [14]: -1 == ~0 Out [14]: True

Recall that ~ is the bit-flip operator, and evidently when you flip all the bits of zero you end up with 1. If you’re curious as to why this is, look up the two’s complement integer encoding scheme, which is what Python uses to encode signed integers, and think about happens when you start flipping all the bits of integers encoded this way.

Boolean Operations

When working with Boolean values, Python provides operators to combine the values using the standard concepts of "and", "or", and "not". Predictably, these operators are expressed using the words and , or , and not :

In [15]: x = 4 (x < 6) and (x > 2) Out [15]: True In [16]: (x > 10) or (x % 2 == 0) Out [16]: True In [17]: not (x < 6) Out [17]: False

Boolean algebra aficionados might notice that the XOR operator is not included; this can of course be constructed in several ways from a compound statement of the other operators. Otherwise, a clever trick you can use for XOR of Boolean values is the following:

In [18]: # (x > 1) xor (x < 10) (x > 1) != (x < 10) Out [18]: False

These sorts of Boolean operations will become extremely useful when we begin discussing control flow statements such as conditionals and loops.

One sometimes confusing thing about the language is when to use Boolean operators ( and , or , not ), and when to use bitwise operations ( & , | , ~ ). The answer lies in their names: Boolean operators should be used when you want to compute Boolean values (i.e., truth or falsehood) of entire statements. Bitwise operations should be used when you want to operate on individual bits or components of the objects in question.

Identity and Membership Operators

Like and , or , and not , Python also contains prose-like operators to check for identity and membership. They are the following:

Operator Description

a is b

True if a and b are identical objects

a is not b

True if a and b are not identical objects

a in b

True if a is a member of b

a not in b

True if a is not a member of b

Identity operators: is and is not

The identity operators, is and is not , check for object identity . Object identity is different than equality, as we can see here:

In [19]: a = [1, 2, 3] b = [1, 2, 3] In [20]: a == b Out [20]: True In [21]: a is b Out [21]: False In [22]: a is not b Out [22]: True

What do identical objects look like? Here is an example:

In [23]: a = [1, 2, 3] b = a a is b Out [23]: True

The difference between the two cases here is that in the first, a and b point to different objects , while in the second they point to the same object . As we saw in the previous section, Python variables are pointers. The is operator checks whether the two variables are pointing to the same container (object), rather than referring to what the container contains. With this in mind, in most cases that a beginner is tempted to use is , what they really mean is == .

Membership operators

Membership operators check for membership within compound objects. So, for example, we can write:

In [24]: 1 in [1, 2, 3] Out [24]: True In [25]: 2 not in [1, 2, 3] Out [25]: False

These membership operations are an example of what makes Python so easy to use compared to lower-level languages such as C. In C, membership would generally be determined by manually constructing a loop over the list and checking for equality of each value. In Python, you just type what you want to know, in a manner reminiscent of straightforward English prose.

Built-In Types: Simple Values

When discussing Python variables and objects, we mentioned the fact that all Python objects have type information attached. Here we’ll briefly walk through the built-in simple types offered by Python. We say "simple types" to contrast with several compound types, which will be discussed in the following section.

Python’s simple types are summarized in.

Table 1-6. Python scalar types Type Example Description

int

x = 1

Integers (i.e., whole numbers)

float

x = 1.0

Floating-point numbers (i.e., real numbers)

complex

x = 1 + 2j

Complex numbers (i.e., numbers with a real and imaginary part)

bool

x = True

Boolean: True/False values

str

x = 'abc'

String: characters or text

NoneType

x = None

Special object indicating nulls

We’ll take a quick look at each of these in turn.

Integers

The most basic numerical type is the integer. Any number without a decimal point is an integer:

In [1]: x = 1 type(x) Out [1]: int

Python integers are actually quite a bit more sophisticated than integers in languages like C . C integers are fixed-precision, and usually overflow at some value (often near 2 31 or 2 63 , depending on your system). Python integers are variable-precision, so you can do computations that would overflow in other languages:

In [2]: 2 ** 200 Out [2]: 1606938044258990275541962092341162602522202993782792835301376

Another convenient feature of Python integers is that by default, division upcasts to floating-point type:

In [3]: 5 / 2 Out [3]: 2.5

Note that this upcasting is a feature of Python 3; in Python 2, like in many statically typed languages such as C, integer division truncates any decimal and always returns an integer:

# Python 2 behavior >>> 5 / 2 2

To recover this behavior in Python 3, you can use the floor-division operator:

In [4]: 5 // 2 Out [4]: 2

Finally, note that although Python 2. x had both an int and long type, Python 3 combines the behavior of these two into a single int type.

Floating-Point Numbers

The floating-point type can store fractional numbers. They can be defined either in standard decimal notation, or in exponential notation:

In [5]: x = 0.000005 y = 5e-6 print(x == y) True In [6]: x = 1400000.00 y = 1.4e6 print(x == y) True

In the exponential notation, the e or E can be read "…times ten to the…", so that 1.4e6 is interpreted as 1.4 × 10 6 .

An integer can be explicitly converted to a float with the float constructor :

In [7]: float(1) Out [7]: 1.0 Floating-point precision

One thing to be aware of with floating-point arithmetic is that its precision is limited, which can cause equality tests to be unstable. For example:

In [8]: 0.1 + 0.2 == 0.3 Out [8]: False

Why is this the case? It turns out that it is not a behavior unique to Python, but is due to the fixed-precision format of the binary floating-point storage used by most, if not all, scientific computing platforms. All programming languages using floating-point numbers store them in a fixed number of bits, and this leads some numbers to be represented only approximately. We can see this by printing the three values to high precision:

In [9]: print("0.1 = {0:.17f}".format(0.1)) print("0.2 = {0:.17f}".format(0.2)) print("0.3 = {0:.17f}".format(0.3))

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: PythonLinuxC++JavaWindowsInc.mathematica
分页:12
转载请注明
本文标题:A Whirlwind Tour of Python
本站链接:http://www.codesec.net/view/480141.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(49)