Python学习笔记(2)
2015-12-25 00:00
471 查看
第一个Python程序:Hello World
第一个程序文件你要搞清楚如何新建py文件,如何运行py,如何查看运行结果
第二个Python程序:学会变量 ,控制 ,操作符 ,print的格式化
《Python基础编程》中对格式化输出的总结: (1). %字符:标记转换说明符的开始 (2). 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充 (3). 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出。 (4). 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出 (5).字符串格式化转换类型 转换类型 含义 d,i 带符号的十进制整数 o 不带符号的八进制 u 不带符号的十进制 x 不带符号的十六进制(小写) X 不带符号的十六进制(大写) e 科学计数法表示的浮点数(小写) E 科学计数法表示的浮点数(大写) f,F 十进制浮点数 g 如果指数大于-4或者小于精度值则和e相同,其他情况和f相同 G 如果指数大于-4或者小于精度值则和E相同,其他情况和F相同 C 单字符(接受整数或者单字符字符串) r 字符串(使用repr转换任意python对象) s 字符串(使用str转换任意python对象)
第三个Python程序:学会字符集,文件操作,字符串操作,时间日期操作
print("Hello World")
第一个程序文件你要搞清楚如何新建py文件,如何运行py,如何查看运行结果
第二个Python程序:学会变量 ,控制 ,操作符 ,print的格式化
__author__ = 'syf121@163.com' # coding: UTF-8 import os import datetime # 测试时间日期 def testdatetime(): print("=测试日期==今天是=") print(datetime.time) # 测试 文件操作 def testfile(): print(os.curdir) # 测试if-else选择 def testif(): i = 0 if i == 0: print("==test if=== i is o") else: print("i is not o") # 测试for循环 def testfor(): i = 0 count = 0 for i in range(5): print("===test for===i is %d " % i) count += i print("count = %d" % count) # 测试数组循环 strs = ['张三', '李四', '王五', '姓名'] for str in strs: print("测试数组for循环==%s" % str) # 测试while循环 def testwhile(): i = 0 while i < 5: print("===测试while循环==%d" % i) i += 1 # 测试print的输出格式 def testprint(): s = "这个一个字符串" print("===test print String== %s" % s) i = 100 print("===test print num==%d" % i) pi = 3.141592653 print('%10.3f' % pi) # 字段宽10,精度3 print("pi = %.*f" % (3, pi)) # 用*从后面的元组中读取字段宽度或精度 pi = 3.142 print('%010.3f' % pi) # 用0填充空白 print('%-10.3f' % pi) # 左对齐 print('%+f' % pi) # 显示正负号 #执行各个测试方法 testdatetime() testfile() testif() testfor() testwhile() testprint()
《Python基础编程》中对格式化输出的总结: (1). %字符:标记转换说明符的开始 (2). 转换标志:-表示左对齐;+表示在转换值之前要加上正负号;“”(空白字符)表示正数之前保留空格;0表示转换值若位数不够则用0填充 (3). 最小字段宽度:转换后的字符串至少应该具有该值指定的宽度。如果是*,则宽度会从值元组中读出。 (4). 点(.)后跟精度值:如果转换的是实数,精度值就表示出现在小数点后的位数。如果转换的是字符串,那么该数字就表示最大字段宽度。如果是*,那么精度将从元组中读出 (5).字符串格式化转换类型 转换类型 含义 d,i 带符号的十进制整数 o 不带符号的八进制 u 不带符号的十进制 x 不带符号的十六进制(小写) X 不带符号的十六进制(大写) e 科学计数法表示的浮点数(小写) E 科学计数法表示的浮点数(大写) f,F 十进制浮点数 g 如果指数大于-4或者小于精度值则和e相同,其他情况和f相同 G 如果指数大于-4或者小于精度值则和E相同,其他情况和F相同 C 单字符(接受整数或者单字符字符串) r 字符串(使用repr转换任意python对象) s 字符串(使用str转换任意python对象)
第三个Python程序:学会字符集,文件操作,字符串操作,时间日期操作
相关文章推荐
- 转载:关于Java、Python、Go编程思想的不同
- MAC安装Python开发环境
- 我的Python学习历程
- 招聘Python 开发员
- [Python标准库]Queue——线程安全的 FIFO 实现
- python中的包管理与导入
- 【Python 第16课】 字符串格式化2
- 【Python 第17课】 类型转换
- 【Python 第18课】 bool类型转换
- 基于python的终端天气查询
- python用sql的limit语句进行分页
- Python 包管理
- PYTHON 学习第九课
- Windows Python IDEL编译器自动忽略'\r'及简单进度条的实现
- Python2.7 NumPy和PIL模块安装
- 零基础入门学习Python(19):对象(4)组合、绑定
- 进击python第一篇:相遇
- python之模块py_compile用法(将py文件转换为pyc文件)
- Python爬虫 - 登录csdn
- 使用Pycharm安装Python第三方库