python默认使用UTF-8编码
一个python3版本的HelloWorld代码如下:
#!/usr/bin/env pythonprint ('Hello World!')
如果此python脚本文件名为:hello.py,则运行此脚本文件的方法有两种:
1、python hello.py
[laolang@localhost python]$ python hello.py Hello World![laolang@localhost python]$
2、修改hello.py的权限,./hello.py
[laolang@localhost python]$ ./hello.py Hello World![laolang@localhost python]$
第一个行称为shebang(shell执行)行,作用是指定了要使用哪个解释器
shebang行通常有两种等式:
#!/bin/bin/python
或
#!/usr/bin/env python
第一种形式使用指定的解释器,第二种等式使用在shell环境中发现的第一个python解释器
对于python2.x 和 python3.x同时安装的情况而言,一个可靠且可行的方法是使用ln命令,在/usr/bin/目录下创建不同名字的链接。比如我只创建了指向python3解释器的python软链接,如果有需要,还可以创建一个指向python2解释器的python2软链接
python的关键要素:
1.输入输出:
首先是输出:print()
在windows上安装python后,会在菜单中看到Python 3.4 Docs Server (pydoc - 64 bit),打开之后,会在浏览器中看到如下页面:
![c27bd7f82d30f1a9073e73b225c102d4.png](https://i-blog.csdnimg.cn/blog_migrate/ee5a6fff3698f2c4249f53b0e0072ba2.jpeg)
其中print是我输入的文本,回车之后会看到如下内容:
![321eebc12e2f446ef4e3ffd075ba1cf6.png](https://i-blog.csdnimg.cn/blog_migrate/18d9fc3e7aba7d0cd413c4c13c961474.jpeg)
我感觉这种方式的帮助文档看起来更好一点。
可以看到其中很多参数都有了默认值,这个解释还是很不错的
输入:input
input(...)input([prompt]) -> string Read a string from standard input. The trailing newline is stripped.If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.On Unix, GNU readline is used if enabled. The prompt string, if given,is printed without a trailing newline before reading.
值得注意的是input返回的是string类型
一个使用了input和print的例子:
#!/usr/bin/env pythonprint ('Hello World!')name=input("input your name:")print("your name is : " + name)
可以看到在python中声明一个变量是时不需要显示的指明其类型,这和js有点类似
2. 内置类型中的int和str
python中,int类型要比C语言的友好的多,我们可以使用很大很大的int类型的数字而不必担心溢出
对于string类型,可以使用[]来取得字符串中某个字符
但是需要提出的是int和string 类型都是不可变的。不过我们可以使用int(str)可str(int)等方式来改变一个数据项的类型
3.对象引用
在python中可以使用=运算符直接将一个变量指向另一个变量,一个实际的例子:
[laolang@localhost python]$ /bin/cat hello.py #!/usr/bin/env pythonprint ('Hello World!')name=input("input your name:")var=nameprint("your name is : " + var)sex=input("input your sex:")var=sexprint("your name is : " + var)age=input("input your age:")var=int(age)print("your age is :