标签:
第一个源程序
#!/usr/bin/python
# Filename :
helloworld.pyprint ‘Hello world‘
执行:
$ python helloworld.py 或者 $ ./helloworld.py
因为系统知道它必须用源文件第一行指定的那个解释器来运行程序
这样的字符串‘This is a string‘、"It‘s a string!"
在Python中有4种类型的数——整数、长整数、浮点数和复数。
2是一个整数的例子。3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。(-5+4j)和(2.3-4.6j)是复数的例子。 它们的值可以变化,即你可以使用变量存储任何东西。
变量是标识符的例子。 标识符 是用来标识 某样东西 的名字。在命名标识符的时候,你要遵循这些规则:
标识符的第一个字符必须是字母表中的字母(大写或小写)或者一个下划线(‘ _ ’)。
标识符名称的其他部分可以由字母(大写或小写)、下划线(‘ _ ’)或数字(0-9)组成。
标识符名称是对大小写敏感的。例如,myname和myName不是一个标识符。注意前者中的小写n和后者中的大写N。
有效 标识符名称的例子有i、__my_name、name_23和a1b2_c3。
无效 标识符名称的例子有2things、this is spaced out和my-name。
变量可以处理不同类型的值,称为数据类型。基本的类型是数和字符串,我们已经讨论过它们了。在后面的章节里面,我们会研究怎么用类创造我们自己的类型。
在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。
这意味着同一层次的语句必须有相同的缩进。每一组这样的语句称为一个块。
你需要记住的一样东西是错误的缩进会引发错误。例如:
i = 5
print ‘Value is‘, i # Error! Notice a single space at the start of the
lineprint ‘I repeat, the
value is‘, i
当你运行这个程序的时候,你会得到下面的错误:
File "whitespace.py", line 4
print ‘Value is‘, i
# Error! Notice a single space at the start of the line
^
SyntaxError:
invalid syntax
它告诉你, 你不能随意地开始新的语句块 (当然除了你一直在使用的主块)。何时你能够使用新块,将会在后面的章节,如控制流中详细介绍。
如何缩进
不要混合使用制表符和空格来缩进,因为这在跨越不同的平台的时候,无法正常工作。我
强烈建议 你在每个缩进层次使用 单个制表符 或 两个或四个空格
。
选择这三种缩进风格之一。更加重要的是,选择一种风格,然后一贯地使用它,即 只 使用这一种风格。
如果你有一个如2 + 3 * 4那样的表达式,是先做加法呢,还是先做乘法?我们的中学数学告诉我们应当先做乘法——这意味着乘法运算符的优先级高于加法运算符。
我建议你使用圆括号来分组运算符和操作数,以便能够明确地指出运算的先后顺序,使程序尽可能地易读。例如,2 + (3 * 4)显然比2 + 3 * 4清晰。
#!/usr/bin/python
# Filename:
expression.pylength = 5breadth = 2area = length * breadthprint ‘Area is‘,
areaprint ‘Perimeter
is‘, 2 * (length +
breadth)
$ python expression.py
Area is 10
Perimeter is 14
注意Python如何打印“漂亮的”输出。尽管我们没有在‘Area
is‘和变量area之间指定空格,Python自动在那里放了一个空格,这样我们就可以得到一个清晰漂亮的输出,而程序也变得更加易读(因为我们不需要担心输出之间的空格问题)。这是Python如何使程序员的生活变得更加轻松的一个例子。
#!/usr/bin/python
# Filename: if.py
number = 23guess =
int(raw_input(‘Enter an integer :
‘))if guess ==
number: print ‘Congratulations, you guessed it.‘ # New
block starts here print "(but you do not win any prizes!)" # New
block ends hereelif guess <
number: print ‘No, it is a
little higher than that‘ # Another
block # You can do whatever you want in a
block ...else: print ‘No, it is a little lower than that‘
# you must have guess > number to reach
hereprint ‘Done‘# This last statement is always
executed, after the if statement is executed
$ python if.py
Enter an integer : 50
No, it is a
little lower than that
Done
$ python if.py
Enter an integer : 22
No,
it is a little higher than that
Done
$ python if.py
Enter an integer :
23
Congratulations, you guessed it.
(but you do not win any
prizes!)
Done
#!/usr/bin/python
# Filename:
while.pynumber = 23running = Truewhile
running: guess = int(raw_input(‘Enter an integer :
‘)) if guess ==
number: print ‘Congratulations, you guessed it.‘ running =
False # this causes the while
loop to stop elif guess <
number: print ‘No, it
is a little higher than that‘ else: print
‘No, it is a little lower than that‘ else: print ‘The while loop is over.‘ # Do
anything else you want to do hereprint
‘Done‘
(源文件:code/while.py)
$ python while.py
Enter an integer : 50
No, it is a
little lower than that.
Enter an integer : 22
No, it is a little higher
than that.
Enter an integer : 23
Congratulations, you guessed it.
The
while loop is over.
Done
标签:
原文地址:http://www.cnblogs.com/jiangzhaowei/p/4313932.html