1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 适合开发 // 首选是网络应用,包括网站、后台服务等等 // 其次是许多日常需要的小工具,包括系统管理员需要的脚本任务等等 // 另外就是把其他语言开发的程序再包装起来,方便使用。 # 缺点 // 运行慢、代码不能加密 (相对于编译型的语言) # 下载安装 勾上Add Python 3.5 to PATH -> Install Now https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe // 命令行进入输入python进入python环境(mac|linux输入python3) # python解释器有很多 CPython是C语言开发的官方的,使用最广泛 CPython IPython PyPy Jython IronPython # 输出hello world python hello.py #!/usr/bin/env python3 #告诉Linux/OS X系统,这是一个Python可执行程序 # -*- coding: utf-8 -*- #按照UTF-8编码读取源代码 print('hello world', '可以多个输出哦') #print需要顶格写 ','会自动变成一个空格 print(1+2+3) # 交互 name = input('please key your name:') print('hello:', name) # '#'是用来注释的,python语言大小写敏感 a = 100 if (a > 1): # 当语句以:结尾时,缩进语句视为代码块,约定使用4空格缩进 print('a') else: print('b') # r''可以让引号中的特殊字符不需要转义 print(r'\t test') print('\\t test') # 多行输出 print('''第一行 第二行 第三行''') # True False(注意大小写) and or not(类似于&& || !) # None空值 # PI = 3.14 python中没有常量,通常约定大写为常量(其实它还是个变量) # 精确除 10/3=3.3333333333333335 # 地板除 10//3=3 # 取模 10%3=1 # ord函数获取字符的整数表示 chr函数把编码转换为对应的字符 test = ord('A') print(test) test = chr(test) print(test) test = ord('中') print(test) test = chr(test) print(test) print('\u4e2d') #知道字符的整数编码,可以直接用十六进制 # bytes类型 b'' b"" x = b'ABC' print(x) print('ABC'.encode('ascii')) print('中文'.encode('UTF-8')) print(b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')) # 格式化方式和C语言一致 输出hello world 100遍 print('hello %s %d遍' % ('world', 100)) # 记不起来类型的话,可以用%s统一处理,转换为字符串 %%正常输出% print('比率为:%s%%' % 99) #比率为:99% |