python-1 python基础知识

python第一课代码笔记

hello world

[root@heartbeat-data-1 python]# vim hello1.py   

#!/usr/bin/env python

print ('hello world!')

[root@heartbeat-data-1 python]# python hello1.py

hello world!
 注意

#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器;#!/usr/bin/env python这种用法是为了防止操作系统用户没有将python装在默认的/usr/bin路径里。当系统看到这一行的时候,首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。#!/usr/bin/python相当于写死了python路径;#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法

 

[root@heartbeat-data-1 python]# vim hello2.py   

def main():

print("hello")

main()

IndentationError: expected an indented block 缩进错误

[root@heartbeat-data-1 python]# vim hello2.py   

def main():

  print("hello")

main()

变量

[root@heartbeat-data-1 python]# vim plus.py   
x = 2
y = 3
print (x + y)
[root@heartbeat-data-1 python]# python plus.py
5
[root@localhost python]# vim plus.py   
def main():
  x = 2
  y = 4  print(x + y)
main()

# python plus.py6

大写的一般是常量,小写的是变量

[root@heartbeat-data-1 python]# vim test.py   
x = 2
y = 3
z = x
x = 5
print ('Z:',z)
print ('X:',x)
[root@heartbeat-data-1 python]# python test.py
Z: 2
X: 5
[root@heartbeat-data-1 python]# vim test1.py   
a = 'ABC'
b = a
a = 'XYZ'
print(b)
print(a)
[root@heartbeat-data-1 python]# python test1.py
ABC
XYZ
单行注释:#,多行注释:’’’ 三个引号
#print ‘ddd’
[root@localhost python]# vim zhushi.py   
print'''
print 'ddd'
print 'ddd'
print 'ddd'
------------------'''

[root@localhost python]# python zhushi.py

print 'ddd'

print 'ddd'

print 'ddd'

------------------

相关推荐