#python test
# coding:utf-8
import json
from MyClass001 import MyClass001
import bisect
import sys
from xml.dom.minidom import parse
import xml.dom.minidom
import xml.sax
import time
import random
#创建int 变量并赋值
a =123
#创建 float 变量并赋值
b=923.63
print(f"result{a}{b}");
#创建 int 数组
c=[1,2,3,4,5,6,7,8,9]
#遍历int数组
for x in c:
print(x);
arrr1 = [1, 4, 2, 5, 3, 7, 9, 0]
#sort 排序
print("sort 排序")
arrr1.sort()
for x in arrr1:
print(x)
#冒泡排序
print("冒泡排序1")
#整型数组
arrr = [1, 4, 2, 5, 3, 7, 9, 0]
# range是范围的意思
for u in range(0, len(arrr), 1):
for n in range(0, len(arrr)-u-1, 1):
if arrr[n]> arrr[n+1]:
temp=arrr[n+1]
arrr[n+1]=arrr[n]
arrr[n]=temp
for c in arrr:
print(c)
#整型数组
arr = [1, 4, 2, 5, 3, 7, 9, 0]
print("冒泡排序2")
for i in range(0, len(arr), 1):
for j in range(0, len(arr)-i-1, 1):
if arr[j] > arr[j+1]:
temp = arr[j+1]
arr[j+1] = arr[j]
arr[j] = temp
#遍历int 数组
for i in arr:
print(i, end="\t")
print("\n一个简单的类实例\n")
#面向对象
class MyClass:
i = 4561235689
def f(self):
return 'hello world'
# 实例化类
x = MyClass()
# 访问类的属性和方法
print("MyClass 类的属性 i 为:", x.i)
print("MyClass 类的方法 f 输出为:", x.f())
# Python 字典类型转换为 JSON 对象
data = {
'no' : 1,
'name' : 'Runoob',
'url' : 'http://www.runoob.com'
}
json_str = json.dumps(data)
print ("Python 原始数据:", repr(data))
print ("JSON 对象:", json_str)
# 将 JSON 对象转换为 Python 字典
data2 = json.loads(json_str)
print ("data2['name']: ", data2['name'])
print ("data2['url']: ", data2['url'])
#读写文件方法0 hello i love chinas
print("读写文件")
try:
f = open('C:\\124.txt', 'r')
print(f.read())
finally:
if f:
f.close()
print("写文件")
try:
f = open('C:\\124.txt', 'a+') # 若是'wb'就表示写二进制文件 w 创建或清除重写,a+ 创建或追加
f.write('\nHello, world!\n')
finally:
if f:
f.close()
#读取文件 方法1
print("读写文件方法1")
try:
f = open('C:\\124.txt', 'r')
lines = f.readlines() #读取全部内容 ,并以列表方式返回
for x in lines:
print(f"{x}\n")
finally:
if f:
f.close()
# 读取文件方法2
print("读取文件方法2")
try:
with open("C:\\124.txt","r") as f:
print(f.read());
except :
#解析xml
#<collection shelf="New Arrivals">
#<movie title="Enemy Behind">
# <type>War, Thriller</type>
# <type>War, Thriller</type>
# <format>DVD</format>
# <year>2003</year>
# <rating>PG</rating>
# <stars>10</stars>
# <description>Talk about a US-Japan war</description>
#</movie>
#<movie title="Transformers">
# <type>Anime, Science Fiction</type>
# <format>DVD</format>
# <year>1989</year>
# <rating>R</rating>
# <stars>8</stars>
# <description>A schientific fiction</description>
#</movie>
# <movie title="Trigun">
# <type>Anime, Action</type>
# <format>DVD</format>
# <episodes>4</episodes>
# <rating>PG</rating>
# <stars>10</stars>
# <description>Vash the Stampede!</description>
#</movie>
#<movie title="Ishtar">
# <type>Comedy</type>
# <format>VHS</format>
# <rating>PG</rating>
# <stars>2</stars>
# <description>Viewable boredom</description>
#</movie>
#</collection>
# 使用minidom解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("c:\\movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
print ("Root element : %s" % collection.getAttribute("shelf"))
# 在集合中获取所有电影
movies = collection.getElementsByTagName("movie")
# 打印每部电影的详细信息
for movie in movies:
print ("*****Movie*****")
if movie.hasAttribute("title"):
print ("Title: %s" % movie.getAttribute("title"))
type = movie.getElementsByTagName('type')[0]
print ("Type: %s" % type.childNodes[0].data)
format = movie.getElementsByTagName('format')[0]
print ("Format: %s" % format.childNodes[0].data)
rating = movie.getElementsByTagName('rating')[0]
print ("Rating: %s" % rating.childNodes[0].data)
description = movie.getElementsByTagName('description')[0]
print ("Description: %s" % description.childNodes[0].data)
#Python int与string之间的转化
#string-->int
#10进制string转化为int
print(int('12'))
#16进制string转化为int
print(int('12', 16))
#int-->string
#int转化为10进制string
print(str(18))
#int转化为16进制string
print(hex(18))
#获取时间信息
print("获取时间信息")
datetime = time.localtime(time.time())
print(time.strftime('%Y-%m-%d',datetime))
strDateTime = time.strftime('%Y-%m-%d %H:%M:%S',datetime)
print(strDateTime)
#类的对象处理
print("类的对象处理")
myclass001=MyClass001;
myclass001.getuserinfo(myclass001)
myclass001.mywork()
myclass001.getramd()
myclass001.getramd()
print("产生随机数字")
print(random.randint(100,1258489))
#退出
print("\n\n按下 e 键后退出。")
l=input()
print(l)
if l == "e":
print("yes")
else:
print("no")
input()