python默认使用UTF-8编码 python
一个python3版本的HelloWorld代码以下: shell
#!/usr/bin/env python print ('Hello World!')若是此python脚本文件名为:hello.py,则运行此脚本文件的方法有两种:
一、python hello.py windows
[laolang@localhost python]$ python hello.py Hello World! [laolang@localhost python]$二、修改hello.py的权限,./hello.py
[laolang@localhost python]$ ./hello.py Hello World! [laolang@localhost python]$第一个行称为shebang(shell执行)行,做用是指定了要使用哪一个解释器
python的关键要素: python3.x
1.输入输出: 浏览器
首先是输出:print() 编码
在windows上安装python后,会在菜单中看到Python 3.4 Docs Server (pydoc - 64 bit),打开以后,会在浏览器中看到以下页面: code
其中print是我输入的文本,回车以后会看到以下内容: 对象
我感受这种方式的帮助文档看起来更好一点。 ip
能够看到其中不少参数都有了默认值,这个解释仍是很不错的 文档
输入: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 python print ('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 python print ('Hello World!') name=input("input your name:") var=name print("your name is : " + var) sex=input("input your sex:") var=sex print("your name is : " + var) age=input("input your age:") var=int(age) print("your age is : ",sep=' ',end='') print(var) [laolang@localhost python]$ ./hello.py Hello World! input your name:xiao dai ma your name is : xiao dai ma input your sex:nan your name is : nan input your age:24 your age is : 24 [laolang@localhost python]$能够看到其中var变量引用了不一样的变量,其指向的内容和值的类型也随之改变
HelloWorld暂时到这里