当前python有2个版本,python2使用中文注释的时候使用方法如下:
#coding:utf-8
#这是一个中文注释
#python2
print "hello world"
python3使用中文注释的时候不需要强调编码是utf-8因为其编码默认是utf-8
#python3
#这是一个中文注释
#print("hello world")
python变量:
1.python的变量不需要定义可以直接使用,还可以重新使用存储不同类型的变量值。
2.变量命名遵循c的风格,需要区分大小写。
3.del语句可以直接释放资源,变量名删除,引用计数器减1
4.变量内存自动管理回收,垃圾收集
5.指定编码需要在文件头中加入 # -*-coding:UTF-8 -*-或者#coding=utf-8
引号的使用:
#单引号和双引号只能表示单行的字符串
str1 = "a'b'ucd"
str2 = 'ab"c"d'
str3 = "abcdf123"
str4 = ''' 你好
nihuai
nizhenhuai
'''
#三引号 或者三双引号 表示多行字符串
print str1
print str2
print str3
print str4
python函数
1.def定义函数的关键字
2.x和y为形参,不需要类型修饰
3.函数定义行需要跟":"
4.函数体整体缩进
5.函数可以拥有返回值,如果没有返回值,返回None,相当于C中的NULL
def add(x, y):
z = x + y
return z
res = add(3, 5)
print res
python通过
#coding:utf-8
#定义一个函数
def print_value(a, b):
print "print value is called"
print a
print type(a)
print b
print type(b)
return "ABC"
#函数的调用
ret = print_value(100,"bac")
print ret
print type(ret)
输出
print value is called
100
<type 'int'>
bac
<type 'str'>
ABC
<type 'str'>