Python T-01

test01.py

message = "hello world"
print(message)

# 使用单个引号的时候,注意两侧要用不一样的引号包裹,来避免歧义
message = "It's me"
print(message)

"""
title()方法可以使文字信息开头大写
upper() lower()可以使文字全部大写或是小写
"""
print(message.title())
print(message.upper())
print(message.lower())

# 以 + 号合并字符串
first_name = "lost"
last_name = "sakura"
full_name = last_name + " " + first_name
print(full_name)

# \n换行 \t制表符 可直接应用于字符串中
print("\n\t这是一段测试换行和制表符的文字")

# rstrip()可以处理字符串末尾的空白,并删除(暂时的)
print(" rstrip可以处理字符串 末尾的空白  ".rstrip())
# lstrip()可以删除字符串头+的空白
# strip()同时删除头尾的字符串空白

# 整数类可以直接进行加减
# 连续两个 ** 是乘方运算的意思
# 并使用str()函数将非字符串值转为字符串
print("5**2表示5的2次方" + str(5**2))

# 浮点类使用类似于JavaScript一样放松
print("3/2等于" + str(3/2))

# 列表的构件和访问 逗号间隔的时候添加空格
test_list = ['one', 'two', 'three', 'four']
print(test_list)
# 索引从0开始
print(test_list[1])
# 规定索引-1为最后一个元素
print(test_list[-1])
# 使用append()可以向列表末尾追加元素,即使时空列表
test_list.append('five')
print(test_list)
# 使用insert()可以在任意索引插入一个元素,后面的元素全部右移
test_list.insert(3, 'new')
print(test_list)
# 使用del语句可以删除任意已知位置的元素
del test_list[3]
print(test_list)
# pop()在删除列表末尾元素的时候,并返回这个元素
# pop()默认删除最后一个元素,可在括号内指定索引
test_list_popped = test_list.pop()
print(test_list)
print(test_list_popped)
# remove()在已知元素情况下,依据元素删除
test_list.remove('one')
print(test_list)

# sort()方法可以对列表永久性排序 按照首字母顺序 使用时用.点运算符
test_list.sort()
print(test_list)
# sorted()可以进行临时排序 将需要排序的列表放在括号内
print(sorted(test_list))
# reverse()反向排序列表
test_list_num = [1, 2, 3, 4, 5, 6]
print(test_list_num)
test_list_num.reverse()
print(test_list_num)
# len()输出列表长度
print(len(test_list_num))

# 遍历整个列表 for x in xs 需要注意最后要有冒号
for tl_mun in test_list_num:
    print(tl_mun)

"""
range(num1,num2)按顺序创建一组数字 从num1开始,在num2前结束
range()可以用在for in遍历
使用list(range())组合可以创建有顺序的数字列表
"""
test_list_num_2 = list(range(1,6))
for value in range(1,6):
    print("第" + str(value) + "个数字是:" + str(test_list_num_2[value - 1]))

# range()还可以自定义跨度 类似于range(num1,num2,num3) num3即为跨度
test_list_num_3 = list(range(1, 50, 3))
print(test_list_num_3)

# 对数字列表可简单计算 min() max() sum()
print(min(test_list_num_3))
print(max(test_list_num_3))
print(sum(test_list_num_3))

# 列表解析 for结尾没有冒号
squares = [squares_value**2 for squares_value in range(1,11)]
print(squares)

"""
列表切片 可以理解为按需求选取整个列表的部分片段
格式按照list[0索引:1索引]
可以省略其中之一
可以使用负数
"""
test_list_abc = ['a', 'b', 'c', 'd', 'e', 'f']
print(test_list_abc)
print(test_list_abc[2:4])
print(test_list_abc[:4])
print(test_list_abc[-3:])

# 如果要复制一个列表副本,应使用切片方式 不用直接赋值方式
# 这样的方式可以使得两列表互不影响
test_list_abc_s = test_list_abc[:]
test_list_abc_s.append('g')
print(test_list_abc)
print(test_list_abc_s)

# 元祖和列表类似,访问方式一致 但是不可以单一改变元素值
# 要改变,需要整体赋值改变 元祖使用小括号包围 严格限制对单一元素控制
test_list_abc_yz = ('A', "B", 'C', 'D', 'E', 'F')
print(test_list_abc_yz[2])

控制台运行结果:

hello world
It's me
It'S Me
IT'S ME
it's me
sakura lost

    这是一段测试换行和制表符的文字
 rstrip可以处理字符串 末尾的空白
5**2表示5的2次方25
3/2等于1.5
['one', 'two', 'three', 'four']
two
four
['one', 'two', 'three', 'four', 'five']
['one', 'two', 'three', 'new', 'four', 'five']
['one', 'two', 'three', 'four', 'five']
['one', 'two', 'three', 'four']
five
['two', 'three', 'four']
['four', 'three', 'two']
['four', 'three', 'two']
[1, 2, 3, 4, 5, 6]
[6, 5, 4, 3, 2, 1]
6
6
5
4
3
2
1
第1个数字是:1
第2个数字是:2
第3个数字是:3
第4个数字是:4
第5个数字是:5
[1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49]
1
49
425
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
['a', 'b', 'c', 'd', 'e', 'f']
['c', 'd']
['a', 'b', 'c', 'd']
['d', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
C


test02.py

# Python中用:和缩格的方式来替代{}
# if语句
if 1 < 0:
    printf("1小于0")
else:
    print("1大于0")

# 判断两字符串相等时,严格限制大小写
# 可以使用lower()方法来统一大小写后进行判断
# else if 使用 elif
test_str_0 = "abcd"
test_str_1 = "AbcD"
if test_str_1 == test_str_0:
    print("两字符串相等")
elif test_str_0 == test_str_1.lower():
    print("使用lower()方法后两字符串相等")
else:
    print("两字符串不相等")

# 逻辑运算符中的 ||(或) 用 or 代替  &&(与) 用 and 代替
# 小括号依然优先级是最高的一类

# 检查特定的值是否包含在列表中使用 in 关键字
test_list = ['a', 'b', 'c', 'd', 'e', 'f']
if ('h' in test_list) and ('a' in test_list):
    print("'h' 和 'a'均在列表中")
elif ('h' in test_list) or ('a' in test_list):
    print("'h' 和 'a'其中之一在列表中")
else:
    print("没有任何匹配的字母")

# not in 关键字和 in 的作用相反

控制台运行结果:

1大于0
使用lower()方法后两字符串相等
'h' 和 'a'其中之一在列表中


test03.py

# 字典以键值对形式呈现
stu_1 = {'name':'lostsakura', 'age':17, 'sex':'male'}
print("My name is " + stu_1['name'])

# 字典是动态结构,可随时添加键值对
stu_1['height'] = "187cm"
print(stu_1['height'])

# 字典元素的值可以进行覆盖
print("I am " + str(stu_1['age']) + " years old")
stu_1['age'] = 18
print("I am " + str(stu_1['age']) + " years old now")

# 使用 del语句 可以永久目标元素键值对
print(stu_1)
del stu_1['sex']
print(stu_1)

""""
遍历字典
使用items()将字典转化为键值对列表
for k,v in 字典.items():
k,v是两个存储键值对的变量 分别存储key和value
"""
for key, value in stu_1.items():
    print(key + ":" + str(value))
# 使用keys()可以单一遍历键 返回的也是一个列表
index = 0
for keyx in stu_1.keys():
    index  = index + 1
    print("第" + str(index) + "键是 " + keyx)

# 使用values()遍历值 使用方法同上
# set()可以使获取的列表去重复

# 词典可以重叠嵌套

控制台运行结果:

My name is lostsakura
187cm
I am 17 years old
I am 18 years old now
{'name': 'lostsakura', 'age': 18, 'sex': 'male', 'height': '187cm'}
{'name': 'lostsakura', 'age': 18, 'height': '187cm'}
name:lostsakura
age:18
height:187cm
第1键是 name
第2键是 age
第3键是 height


test04.py

# input()用户输入
# 接收参数 作为提示在屏幕上输出
# 程序运行到此处 会暂停 等待用户输入
message = "0"
# message = input("请输入任意字符并按回车结束\n")
print(message)

# 使用input()获取的数据以字符串形式保存
# 如需要数值数据类型,可以使用int()转化
if int(message) == 0:
    print("message是一个数值")
else:
    print("message没有变成一个数值")

# %是求模运算符 求余数

# while循环结构
"""
while message != "quit":
    message = input("输入quit退出循环")
    if message != "quit":
        print(message)
"""

# 使用break可以跳出循环
# 使用continue跳过循环
test_num = 0
while test_num < 10:
    test_num += 1
    if test_num == 3:
        continue
    print(test_num)

    if test_num == 7:
        break

控制台运行结果:

0
message是一个数值
1
2
4
5
6
7


2018/07/23

猜你喜欢

转载自blog.csdn.net/qq282465134/article/details/81183106