python
文章平均质量分 66
xijiaoda_liuhao
这个作者很懒,什么都没留下…
展开
-
Python en:Functions
Python en:FunctionsIntroductionFunctions are reusable pieces of programs. They allow you to give a name to a block ofstatements and you can run that block using that name anywhere in your progra原创 2013-04-16 22:49:58 · 1128 阅读 · 0 评论 -
python的id()函数
>>> a = 2.5 >>> b = 2.5 >>> c = b >>> a is c False >>> a = 2 >>> b = 2 >>> c = b >>> a is c True 今天在使用is函数的时候去打印a,b分别被赋值为2.5 和2的情况,发现: >>> a = 2 >>> b = 2 >>> id(a) 2113206原创 2013-04-07 15:58:09 · 2591 阅读 · 0 评论 -
This and That (or This, But Not That!)
This and That (or This, But Not That!)Fun fact: boolean operators can be chained together!It's important to know that boolean operators are not evaluated straight across from left to right all t原创 2013-04-06 20:08:11 · 832 阅读 · 0 评论 -
python中的sequence(序列)
数据类型:sequence (序列)1. sequence(序列)是一组有顺序的元素的集合(严格的说,是对象的集合,但鉴于我们还没有引入“对象”概念,暂时说元素)序列可以包含一个或多个元素,也可以是一个没有任何元素的空序列元素可以是我们之前所说的基本数据类型,可以是另一个序列,还可以是我们以后介绍的其他对象。序列有两种:tuple(定值表; 也有翻译为元组) 和 list原创 2013-04-06 16:11:35 · 18332 阅读 · 1 评论 -
Conditional Statement Syntax
Conditional Statement SyntaxRemember when we showed you that whitespace in Python is significant? If not, write this down: whitespace in Python is significant. In Python, whitespace (tabs or spa原创 2013-04-06 20:17:41 · 556 阅读 · 0 评论 -
Pretty Time
Pretty TimeNice work! Let's do the same for the parts of the time—namely, the hour, minute, and second.As you might guess, we can also use our variable now to print out the time information. I原创 2013-04-03 21:07:20 · 678 阅读 · 0 评论 -
Python的逻辑运算
Python的逻辑运算(and,or,not)与C/C++、Java等语言不太一样。这些语言的逻辑运算返回的值都是bool值,而Python返回的则不同。先说非运算,Python的非运算与这些语言相比,并没有特别的地方。not只有两个返回值,True和False。在Python中,真值为假的对象,包括False,None,数字0,空字符串以及空的容器类型。除此以外的任何对象均为真。接下来是原创 2013-04-04 10:24:03 · 1231 阅读 · 0 评论 -
Compare... Closelier
Compare... CloselierExcellent! It looks like you're comfortable with basic expressions and comparators.But what about... extreme expressions and comparators?(This exercise may seem unnecessary原创 2013-04-04 09:24:23 · 746 阅读 · 0 评论 -
Compare Closely!
Compare Closely!Let's not get ahead of ourselves. First, we'll start with the simplest aspect of control flow:comparators. There are six of them, and we're willing to bet at least a few look fam原创 2013-04-04 09:13:41 · 552 阅读 · 0 评论 -
Hot Date
Hot DateGreat job printing out the date's components! In gearing up for our ultimate goal of printing out mm/dd/yyyy hh:mm:ss, let's tackle adding / slashes to the date's parts.You might thi原创 2013-04-03 20:35:06 · 639 阅读 · 0 评论 -
The datetime Library
The datetime LibraryIn this small project, we'll create a program that experiments with Python's ability to give us the current date and time. This will give you some practice with printing string原创 2013-04-03 20:04:39 · 519 阅读 · 0 评论 -
python的赋值
赋值是语言里最基本的语句,我到现在才发现我之前对python里的赋值的理解一直都是错的。python里的赋值使用的是原对像的引用,而不是拷贝。因此,在进行b=a这样的赋值之后,对b进行操作,是有可能会影响a的值的。例如:>>> a = [1,2,3]>>> b = a>>> b.append(4)>>> print(a,b)[1, 2, 3, 4转载 2013-04-07 15:22:03 · 1033 阅读 · 0 评论 -
探索 Python,第 1 部分: Python 的内置数值类型
Python 是一种敏捷的、动态类型化的、极富表现力的开源编程语言,可以被自由地安装到多种平台上(参阅 参考资料)。Python 代码是被解释的。如果您对编辑、构建和执行循环较为熟悉,则 Python 代码对您来说更简单。但是,请不要搞错:Python 器可以是简单的脚本,也可以是大型的复杂程序。事实上,Python 解释器的最大特点是鼓励探索和简化学习过程。如果您想证明这一点,请使用 Pyt转载 2013-04-07 16:00:45 · 472 阅读 · 0 评论 -
Python 中单引号、双引号和三双引号的区别
Python 中单引号、双引号和三双引号的区别先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 = "hello,world" 如果要写成多行,那么就要使用/ (“连行符”)吧,如 s2 = "hello,/ world" s2与s1是一样的。如果你用3个双引号的话,就可以直接写了,如下: s3 = """hello, wo转载 2013-04-08 15:25:13 · 508 阅读 · 0 评论 -
Python中函数的参数定义和可变参数
Python中函数的参数定义和可变参数刚学用Python的时候,特别是看一些库的源码时,经常会看到func(*args, **kwargs)这样的函数定义,这个*和**让人有点费解。其实只要把函数参数定义搞清楚了,就不难理解了。先说说函数定义,我们都知道,下面的代码定义了一个函数funcAdef funcA():pass显然,函数funcA没有参转载 2013-04-13 10:42:33 · 1269 阅读 · 0 评论 -
UnboundLocalError: local variable ' ' referenced before assignment
在程序中设置的sum_Logical属于全局变量,而在函数calculate中没有sum_Logical的定义,根据python访问局部变量和全局变量的规则:当搜索一个变量的时候,python先从局部作用域开始搜索,如果在局部作用域没有找到那个变量,那样python就在全局变量中找这个变量,如果找不到抛出异常(NAMEERROR或者Unbound-LocalError,这取决于python版本。)原创 2013-04-11 16:20:57 · 4305 阅读 · 0 评论 -
python global和nonlocal
python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量一 globalglobal关键字用来在函数或其他局部作用域中使用全局变量。但是如果不修改全局变量也可以不使用global关键字。gcount = 0def global_test(): gcount+=1 print (gcount)gl原创 2013-04-11 16:00:40 · 4526 阅读 · 0 评论 -
Python的global语句
Python的global语句global语句如果你想要为一个定义在函数外的变量赋值,那么你就得告诉Python这个变量名不是局部的,而是 全局 的。我们使用global语句完成这一功能。没有global语句,是不可能为定义在函数外的变量赋值的。你可以使用定义在函数外的变量的值(假设在函数内没有同名的变量)。然而,我并不鼓励你这样做,并且你应该尽量避免这样做,因为这使得程序转载 2013-04-11 10:38:43 · 709 阅读 · 0 评论 -
Python en:Control Flow
Python en:Control FlowIntroductionIn the programs we have seen till now, there has always been a series of statements andPython faithfully executes them in the same order. What if you wanted原创 2013-04-11 09:09:30 · 705 阅读 · 0 评论 -
Python en:Basics
Python en:BasicsJust printing 'Hello World' is not enough, is it? You want to do more than that - you want totake some input, manipulate it and get something out of it. We can achieve this in Pyth原创 2013-04-09 19:34:53 · 706 阅读 · 0 评论 -
Python en:Operators and Expressions
IntroductionMost statements (logical lines) that you write will contain expressions. A simple exampleof an expression is 2 + 3. An expression can be broken down into operators and operands.Opera原创 2013-04-09 22:56:13 · 1020 阅读 · 0 评论 -
Python 原始字符串(raw strings)
Python 原始字符串(raw strings)的用法你可能已经看到前面关于原始字符串用法的一些例子了。原始字符串的产生正是由于有正则表达式的存在。原因是ASCII 字符和正则表达式特殊字符间所产生的冲突。比如,特殊符号“\b”在ASCII 字符中代表退格键,但同时“\b”也是一个正则表达式的特殊符号,代表“匹配一个单词边界”。为了让RE 编译器把两个字符“\b”当成你想要表达的字符串,而不转载 2013-04-09 15:11:12 · 2674 阅读 · 0 评论 -
Python——Unicode
关于Unicode的详细情况可以参考百度百科:http://baike.baidu.com/view/40801.htm 这里简单的说一下。(下面内容基本上时从《Python.Core.Programming.2ed》上摘的)Unicode是计算机可以支持这个星球上的多种语言的秘密武器,在Unicode之前,用的都是ASCII,ASCII吗非常简单,每个英文字符都用7位二进转载 2013-04-08 16:19:01 · 1099 阅读 · 0 评论 -
Python en:First Steps--part one
Python en:First StepsIntroductionWe will now see how to run a traditional 'Hello World' program in Python. This will teachyou how to write, save and run Python programs.There are two ways of u原创 2013-04-08 17:23:46 · 1082 阅读 · 0 评论 -
Go With the Flow
Go With the Flow(This course assumes familiarity with the material presented in Unit 1: Python Syntaxand Unit 2: Strings & Console Output. From here on out, take for granted that each new course原创 2013-04-04 09:05:26 · 771 阅读 · 0 评论 -
Extracting Information
Extracting InformationNotice how we got an output of the form2012-07-19 12:50:53.180759. That's pretty ugly.Let's examine how to extract portions of the date and time to eventually print out a原创 2013-04-03 20:24:32 · 711 阅读 · 0 评论 -
Getting the Current Date and Time
Getting the Current Date and TimeTo retrieve the current date and time, we can use a function called datetime.now() to get that information.In a later course, you'll learn all aboutfunctions.原创 2013-04-03 20:08:00 · 532 阅读 · 0 评论 -
Welcome to the Flying Circus
Welcome to the Flying CircusPython is a powerful, flexible programming language you can use in web/Internet development, to write desktop graphical user interfaces (GUIs), create games, and much原创 2013-04-01 20:28:04 · 544 阅读 · 0 评论 -
Access by Offset
Access by OffsetGreat work! (By the way, you could also have repaired the string by replacing the single quotes on the ends with double quotes, like this: "Help! Help! I'm being repressed!".)R原创 2013-04-02 16:00:57 · 732 阅读 · 0 评论 -
Step Three: Escape!
Step Three: Escape!Don't get too comfortable: you can't use justany symbol in a string, and some results can only be achieved by special characters. The practice of including these characters in原创 2013-04-02 15:39:38 · 484 阅读 · 0 评论 -
Exponentiation
ExponentiationExcellent job!All the arithmetic operations you've done so far have probably either been intuitive or have resembled work you've done in other programming languages (such as JavaSc原创 2013-04-02 10:19:41 · 515 阅读 · 0 评论 -
A Matter of Interpretation
A Matter of InterpretationIn the introduction to this unit, we mentioned that Python is an interpreted language(解释执行语言) (meaning it runs using an interpreter). For now, think of the interpreter原创 2013-04-02 09:01:54 · 522 阅读 · 0 评论 -
Whitespace Means Right Space
Whitespace Means Right Spacedef spam():eggs = 12return eggs print spam()Notice the error you got when you ran the code in the editor:IndentationError: expected an原创 2013-04-01 23:04:17 · 689 阅读 · 0 评论 -
Data Types
Data TypesGreat! We can now summon the value 10by calling out the name my_variablewhenever we need it.In this case, the data type of my_variableis an integer (a positive or negative whole nu原创 2013-04-01 20:33:51 · 452 阅读 · 0 评论 -
Variables
VariablesOne of the most basic concepts in computer programming is the variable. A variable is a word/identifier that hangs onto a singlevalue. For example, let's say you needed the number 5 f原创 2013-04-01 20:30:43 · 494 阅读 · 0 评论 -
Bringing It All Together
Bringing It All TogetherNice work! So far you've learned about the following in Python:• Variables, which are ways to store values for later use;• Data types (such as integers, floats, andbool原创 2013-04-02 10:56:52 · 775 阅读 · 0 评论 -
python核心编程第六章
6-1.成员操作符in即可判断一个字符或者是一个字符串是不是在另一个字符串中6-2.import stringimport keywordalphas=string.letters+'_'nums=string.digitsdef check(): input=raw_input("identifier to test?") if input[0] not原创 2012-12-13 16:06:43 · 413 阅读 · 0 评论 -
You've Been Reassigned
You've Been ReassignedGreat work. You now know how to declare variables in Python and set them to different values, and you've learned about three different types of values: integers, floats, and原创 2013-04-01 20:37:06 · 486 阅读 · 0 评论 -
What's a Statement?
What's a Statement?You can think of a Python statement as being similar to a sentence in English: it's the smallest unit of the language that makes sense by itself. Just like "I," "like," and "S原创 2013-04-01 20:48:52 · 552 阅读 · 0 评论 -
Single Line Comments
Single Line CommentsYou may have noticed the instructions in the editor that begin with a # (pound or hash) symbol. These lines of code are calledcomments, and they aren't read by the interprete原创 2013-04-02 09:42:19 · 547 阅读 · 0 评论