您的位置:首页 > 编程语言 > Python开发

Python学习笔记(2)

2015-12-25 00:00 471 查看
第一个Python程序:Hello World
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程序:学会字符集,文件操作,字符串操作,时间日期操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: