2-python学习——hello world
2015-06-23 10:23
1951 查看
"hello world"是编程界一个经久不衰的例子,几乎所有语言的学习教程都把它当做第一个程序的范例。学习的过程就是
打开之后就可以在
我们先试一下
因为这是在
在
我们使用有括号的版本就好了。
我们可以新建一个文件2.py,然后写入
print("hello world")
然后我们使用
关于
如果你想指定这个脚本的解释器,那么可以在脚本的最前面加上一行来指明你所选择的解释器,例如
#! /bin/python3.4
print("hello world")
python 官方文档中文站
先来获取一下
这样说是不是还不清除,没关系,举个栗子嘛。
---
看上面的输出,分割符换成了
这是在
这是两者的区别。
看下面的代码
分析一下。
这里先是定义了一个变量
后门又定义了一个变量
最后两个是输出链路这两者的和。注意,
再看这个
要注意,格式化字符串一定要用
原因是%s调用的是str()函数把对象转化为str类型,而%r是调用了repr()将对象转化为字符串。
下面是在
再造轮子的过程,千万不要以为有人做过的,就不去学习了。
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" 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 官方文档中文站
先来获取一下
>>> 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概述
全是英文的,看起来有困难吧。那就别管它了,我来解释一下。标准输出。
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格式化输出
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,然后使用
%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
相关文章推荐
- [转] Python风格规范
- 1 python学习——python环境配置
- 端午节前一周周记
- Python 函数式编程学习
- Python中线程编程之threading模块的使用详解
- Python os模块常用方法1
- python
- python3.4学习笔记(三) idle 清屏扩展插件
- python3.4学习笔记(二) 类型判断,异常处理,终止程序
- python特殊函数(id, dir, type, isinstance, issubclass, is)
- web python -- WSGI接口POST请求
- web python -- WSGI接口GET请求
- python相关面试题
- web python -- WSGI接口响应
- Python数据类型之数字类型、字符串类型
- python日志输出—logging直接代码配置
- 首部讲Python爬虫电子书 Web Scraping with Python
- 关于Python报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法
- python watchdog
- python 使用post登录网易