您的位置:首页 > 编程语言 > Python开发

2-python学习——hello world

2015-06-23 10:23 1951 查看
"hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例。学习的过程就是
再造轮子
的过程,千万不要以为有人做过的,就不去学习了。

hello world

我们先打开
CPython
解释器。

o@o-pc:~$ python2.7
Python 2.7.10 (default, Jun 17 2015, 14:15:05)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

打开之后就可以在
>>>
的后面输入
python
语句了。

我们先试一下
print
这个命令,看是否成功输出"hello world"

>>> print "hello world"
hello world

因为这是在
python2.7
环境下,如果切换到
python3.x
这就行不通了。不信请看

o@o-pc:~$ python3.4
Python 3.4.3 (default, Mar 26 2015, 22:03:40)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello world"
File "<stdin>", line 1
print "hello world"
^
SyntaxError: Missing parentheses in call to 'print'

python3.4
下报了一个错误,意思是语法错误,调用print的时候缺少
括号


我们使用有括号的版本就好了。

>>> print ("hello world")
hello world

python脚本文件

上面是在
python
的交互界面下输出的
"hello world"
,那么有没有办法不使用这种交互界面,而是写一个
python
脚本文件呢?当然是可以了啦。

我们可以新建一个文件2.py,然后写入


print("hello world")


然后我们使用
python 2.py
来执行它。

o@o-pc:~$ python 2.py
hello world

关于
python
脚本的文件名,并不一定要以
.py
做后缀,这只是比较通用的做法。

如果你想指定这个脚本的解释器,那么可以在脚本的最前面加上一行来指明你所选择的解释器,例如
#! /bin/python3.4



#! /bin/python3.4

print("hello world")


help函数

学习过linux/unix系统编程的人,应该对
manpage
这个东西是比较熟悉的。而
python
中提供了一个类似的东西,那就是
help
函数了。如果不清楚某个函数怎么用,就使用
help(函数名)
来获取相关的文档信息。

python 官方文档中文站

先来获取一下
print
函数的用法

>>> help(print)

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.

print函数

print概述

全是英文的,看起来有困难吧。那就别管它了,我来解释一下。

print
函数可以用来打印一些值,默认是输出到
标准输出


file
参数可以控制输出流到别的位置。

end
参数控制字符串输出后是否自动加
'\n'
,你可以改为别的。

sep
参数用于控制分割符,默认是空格。

flush
参数控制是否强制刷新流。

这样说是不是还不清除,没关系,举个栗子嘛。

---

>>> print(1,"+",2,"=",1+2,sep='\t',end=' ')
1   +   2   =   3 >>> print(1,"+",2,"=",1+2)
1 + 2 = 3
>>>

看上面的输出,分割符换成了
'\t'
,结尾没有加
'\n'
的。对比一下就很清楚了吧。

这是在
python3.4
下做的,如果换做
python2.7
,那么输出会变成

>>> print(1,"+",2,"=",1+2)
(1, '+', 2, '=', 3)

这是两者的区别。

print格式化输出

print
的格式化输出,可以参考C语言的
printf
函数的格式化选项,这是类似的。

看下面的代码

>>> a=1000
>>> print("%d"%a)
1000
>>> b=12.345
>>> print("%f"%b)
12.345000
>>> print("%f"%(a+b))
1012.345000
>>> print("%d"%(a+b))
1012

分析一下。

这里先是定义了一个变量
a
,并赋值为
1000
,然后使用
print
来格式化输出。
%d
表示以整数的方式来输出,后面紧跟的
%a
是取变量
a
的值的意思,和
shell
脚本中的
$
有点类似。

后门又定义了一个变量
b
,赋值为
123.45
,然后以浮点数的形式输出。

最后两个是输出链路这两者的和。注意,
python
中的数据是
向下取整
的。

python
中的变量不像
C/C++
这类强类型的语言,它的变量类型只与其最后一次被赋值有关。看下面的语言,重新给变量
b
赋值了一个字符串
"hello"
,然后输出它。

>>> b="hello"
>>> print("%s"%b)
hello

再看这个

>>> type(a)
<class 'int'>
>>> type(b)
<class 'str'>
>>> b=123
>>> type(b)
<class 'int'>

要注意,格式化字符串一定要用
""
包含起来,并且后面紧跟要输出的变量。有两个格式化选项
%s
%r
比较特殊,无论变量保存的数据类型是什么,都能正常输出。

原因是%s调用的是str()函数把对象转化为str类型,而%r是调用了repr()将对象转化为字符串。

下面是在
python2.7
下进行的,
python3.x
已经不支持这种默认转换了。

>>> import time
>>> d=time.localtime()
>>> print d
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)
>>> print "%s"%d
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)
>>> print "%r"%d
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=47, tm_sec=56, tm_wday=2, tm_yday=168, tm_isdst=0)

python3.x
可以这样做,而且这样做是值得提倡的做法。

>>> print("%s"%(str(d)))
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)

>>> print("%s"%repr(d))
time.struct_time(tm_year=2015, tm_mon=6, tm_mday=17, tm_hour=15, tm_min=44, tm_sec=49, tm_wday=2, tm_yday=168, tm_isdst=0)

print直接输出变量

print可以直接输出变量的,是按照变量的类型来输出的。关于python变量,将在下一篇文档中详述。

>>> a=10.10
>>> b=123
>>> c="nihao"
>>> d='c'
>>> print(a,b,c,d)
10.1 123 nihao c
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: