python练习代码保存

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dreamzuora/article/details/88540523
#coding=utf-8
print('中国人')
"""
Python 保留字:and, exec, not, assert, finally, or, break, for, pass, class, from, print, 
continue, global, raise, def, if, return, del, import, try, elif, in, while, else, is, with,
except, lambda, yield
"""
"""
:type
"""
counter = 100 #赋值整型变量
miles = 1000 #浮点型
name = "alan" #字符串
print(counter + miles)
a = b = c = 1
print(a)
str = "hello word!"
print(str * 2)#字符串输出次数
print(str + "test") #输出连接的字符串
print("str[:4]" + str[:4])
print("str[0:4]" + str[0:4])
print("str[:-1]" + str[:-1])
# print("str[-1:0]" + str[-6:-3])
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print(tuple + tinytuple)
#tuple[2] = 1000 #元组是不允许更新的,而列表是允许更新的
'''
python中标准的数字类型有以下五种:
一.数字 Number
  python支持四种不同的数字类型:
    1.int 2.float 3.complex (复数:3 + 4j)  
二.字符串 String
    str = "hello word!"
三.列表 List:有序的对象集合
    list = [ 'a', 'b', 'c', 'd']
四.元组 Tuple:用()标识,内部元素用逗号隔开,但是元组不同二次赋值,相当于只读列表
    tuple = ( 'a', 'b', 'c')
五.字典
'''
print("-------------")
dict = {}
dict['one'] = 'this is one'
dict['two'] = 'this is two'
tinydict = {'name': 'alan','num': 6379, 'flag': 'true'}
print(dict['one'])
keys = tinydict.keys();
'''
注意:python输出用逗号‘,’分隔,'+'表示的是字符串连接,会报类型转换错误
'''
for key in keys:
    print("->:", tinydict[key])
print("values: ",tinydict.values() )

print("-------------")
a = 2
b = 3
c = a ** b # a 的 b 次幂
c1 = a * b
print("运算: ", c)
print('%.3f' %(b/2)) #带小数点
print(b/3) #取整除
x = 1
y = -1
print(x == y)
print("逻辑运算符")
x = True
y = False
print(x and y)
print(x or y)
print(not x)
print("成员运算符")
a = 1
myList = [1, 2, 3, 4]
print(a in myList) #x在序列中
b = -1
print(b not in myList) #b不在序列中
print("python 身份运算符") #用于比较两个对象的存储单元 is or not is
a = 201
b = 20
print(id(a),"-",id(b)) #id函数,用来返回对象地址

print("循环语句")
numbers = [1, 2, 3, 4]
even = []
odd = []
while len(numbers) > 0 :
    number = numbers.pop()
    print("number = ", number, " len = ", len(numbers))
    if(number % 2 == 0):
        even.append(number)
    else:
        odd.append(number)
print("奇数: ", odd)
print("偶数:", even)
print("---------")
for letter in 'python':
    print("当前字母: ", letter)
names = ['alan', 'dream', 'zuora']
for index in range(len(names)): #按照索引输出
    print(names[index])
print("pass 语句") #pass 是空语句,为了保持程序结构的完整性
for letter in 'python':
    if letter == 'h':
        pass
        print("这是 pass 块")
    print("当前字母: ", letter)
print("查看包中内容")
import math
print(dir(math))
import cmath
print(dir(cmath))
print(cmath.pi)
print("字符串的内建函数")
str = 'hello word'
str1 = 'word'
print(str.capitalize()) #首字母大写
print(str.count(str1)) #str1在str中出现的次数
str = str.encode('gb2312') #编码
print(str)
str = str.decode('gb2312') #解码
print(str)
print("检测字符串中是否包含子字符串")
print(str.find("word"))
print("文件IO")
# str = input("请输入: ")
# print("内容: " + str)
print("读取文件")
file_name = "test.txt"
f = open(file_name, "r+", encoding= "utf-8")
# cnt = f.read()
# print(cnt)
# f.readline()#读取一行
lines = f.readlines()
# print("lines: ", lines)
# for line in lines:
#     print("一行行读取: ", line)
# f.close()
# del f
print("写入文件")
f = open(file_name, 'a+', encoding='utf-8')
f.writelines(lines)
f.close()
print("异常处理")
try:
    f = open(file_name, 'w', encoding='utf-8')
    f.write("这是一个测试文件,用于测试异常")
except IOError:
    print("Error: 没有找到文件或读取文件失败")
else:
    print("写入成功")
try:
    1 / 0
except ZeroDivisionError:
    print("除数不能为零")
#zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表
my_list = [1, 'zuora', 3, 'alam']
my_list1 = [2, 'dream']
my_list2 = [3, 'alan']
zipped = zip(my_list, my_list1, my_list2)
for dict_ in zipped:
    print(dict_)
from People import  People
p1 = People(1, 'alan')
p2 = People(2, 'dream')
p3 = People(3, 'zuora')
people_list = [p1, p2, p3]
zipped = zip(people_list)
for dict_ in zipped:
    print(dict_)

猜你喜欢

转载自blog.csdn.net/dreamzuora/article/details/88540523
今日推荐