当前位置:   article > 正文

python基础_petrel_client

petrel_client
print('Petrel')
print(4)
print('Petrel' + '2')
print('I\'m')
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

print('---运算---')
print(9 // 2)  # 9有4个2
print(float('1.2') + 2)
print(2 ** 3)  # 2的3次方
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

print('---变量---')
a, b, c = 1, 2, 3
print(a, b, c)
  • 1
  • 2
  • 3

在这里插入图片描述

print('---whlie---')
while a < 5:
    print(a)
    a = a + 1
  • 1
  • 2
  • 3
  • 4

在这里插入图片描述

print('---for---')
list = [1, 2, 3, 4, 5, 6]
for i in list:
    print(i)

print('---for---')
for j in range(1, 5):
    print(j)

print('---for---')
for j in range(1, 5, 2):
    print(j)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

print('---if---')
x = 1
if x < 1:
    print('x小于1')
elif x > 1:
    print('x大于1')
else:
    print('x等于1')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

print('---函数---')
def function(a, b):
    c = a * b
    print('c的值为', c)
function(2, 2)

print('---函数---')
def sale_car(price, colour, brand='carmy', is_second_hand=True):
    print('price:', price,
          ',colour', colour,
          ',brand', brand,
          ',is_second_hand', is_second_hand)
sale_car(1000, 'red')

print('---函数---')
s = 1
def fun():
    s = 2
    return s + 100
print(s)
print(fun())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述

print('---文件---')
file_a = open('python_test', 'w')
file_a.write('hh')
file_a.close()
file_b = open('python_test', 'a')
file_b.write('\nxx')
file_b.close()
file_c = open('python_test', 'r')
# content = file_c.read()
content_1 = file_c.readlines()
# print(content)
print(content_1)
file_c.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

print('---Class类---')
class MyClass:
    i = 12345
    def f(self):
        return 'hello world'
x = MyClass()
print("MyClass 类的属性 i 为:", x.i)
print("MyClass 类的方法 f 输出为:", x.f())

print('---Class类---')
class Complex:
    def __init__(self, realpart, imagpart):
        self.r = realpart
        self.i = imagpart
x = Complex(3.0, -4.5)
print(x.r, x.i)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

在这里插入图片描述

print('---input---')
a_input = input('请输入:')  # 输入的值为字符型
if a_input == '1':
    print('one')
elif a_input == str(2):
    print('two')
else:
    print('else')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

print('---tuple list---')
a_tuple = (1, 3, 5, 2)
another_tuple = 2, 3, 4, 5
a_list = [4, 1, 2, 5]
for content_2 in a_list:
    print(content_2)
for index in range(len(a_list)):
    print('index=', index, 'number in list=', a_list[index])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

print('---列表---')
a_list.append(6)
print(a_list)
print(a_list[-1])
print(a_list[0:3])
print(a_list.index(1))
print(a_list.count(2))
a_list.sort()
print(a_list)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述

print('---多维列表---')
a = [1, 2, 3, 4, 5]
multi_dim_a = [[1, 2, 3],
               [2, 3, 4],
               [3, 4, 5]]
print(a[1])
print(multi_dim_a[0][1])  # 第0行第1位的数  运行结果:2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

print('---字典---')
d = {'apple': 1, 'pear': 2, 'orange': 3}  # 'key':value
print(d['apple'])  # 运行结果:1
del d['pear']
print(d)  # 运行结果:{'apple': 1, 'orange': 3}
d['banana'] = 20
print(d)  # 运行结果:{'apple': 1, 'orange': 3, 'banana': 20}
d['mango'] = {1: 3, 3: 'a'}  # 字典中可以加字典和函数
print(d)  # 运行结果:{'apple': 1, 'orange': 3, 'banana': 20, 'mango': {1: 3, 3: 'a'}}
print(d['mango'][3])  # 运行结果:a
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

print('---载入模块---')  # import
import time
print(time.localtime())

import time as t  # 可简写
print(t.asctime())

from time import time, localtime  # 仅需模块中的部分功能,这样可以直接输出功能所代表的结果
print(localtime())
print(time)

from time import *  # 导入time中的所有功能
print(localtime())

print('---载入自己的模块---')
import m1
m1.printdata('hello')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

print('---continue break---')
while True:
    b = input('type something:')
    if b == '1':
        break
        # continue
    else:
        pass
    print('still in while')
print('finish run')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

在这里插入图片描述

print('---错误处理try---')
try:
    a = 1 / 0
    print(a)
except Exception as e:
    print('division by zero')
else:
    print(a)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

print('---zip---')
a = [1, 2, 3]
b = [4, 5, 6]
zipped = zip(a, b)
print(zipped)
for i, j in zip(a, b):
    print(i / 2, j * 2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

print('---lambda----')
def sum_fun(x, y):
    return x + y
print(sum_fun(10, 10))
sum = lambda arg1, arg2: arg1 + arg2
print(sum(10, 10))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述

print('---map----')
def square(x):  # 计算平方数
    return x ** 2
map(square, [1, 2, 3, 4, 5])  # 计算列表各个元素的平方
map(lambda x: x ** 2, [1, 2, 3, 4, 5])  # 使用 lambda 匿名函数

# 提供了两个列表,对相同位置的列表数据进行相加
map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
print('---copy$deepcopy---')
import copy
a = [1, 2, 3, 4, ['a', 'b']]  # 原始对象
b = a  # 赋值,传对象的引用
c = copy.copy(a)  # 对象拷贝,浅拷贝
d = copy.deepcopy(a)  # 对象拷贝,深拷贝
a.append(5)  # 修改对象a
a[4].append('c')  # 修改对象a中的['a', 'b']数组对象
print('a = ', a)    # a =  [1, 2, 3, 4, ['a', 'b', 'c'], 5]
print('b = ', b)    # b =  [1, 2, 3, 4, ['a', 'b', 'c'], 5]
print('c = ', c)    # c =  [1, 2, 3, 4, ['a', 'b', 'c']]
print('d = ', d)    # d =  [1, 2, 3, 4, ['a', 'b']]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

print('---pickle---')
import pickle
class Person:
    def __init__(self,n,a):
        self.name=n
        self.age=a
    def show(self):
        print(self.name+"_"+str(self.age))
aa = Person("JGood", 2)
aa.show()   # JGood_2
f=open('E:\\p.txt','wb')
pickle.dump(aa,f,0)
f.close()
#del Person
f=open('E:\\p.txt','rb')
bb=pickle.load(f)
f.close()
bb.show()   # JGood_2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

在这里插入图片描述

print('---set---')
# 去重复字母
x = set('runoob')
y = set('google')
print(x) # {'n', 'b', 'u', 'r', 'o'}
x, y
print(x & y)    # 交集    {'o'}
print(x | y)    # 并集    {'l', 'g', 'n', 'e', 'b', 'u', 'r', 'o'}
print(x - y)    # 差集    {'b', 'n', 'r', 'u'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述
正则表达式
在这里插入图片描述

print('------正则表达式------')

# matching string
pattern1 = "cat"
pattern2 = "bird"
string = "dog runs to cat"
print(pattern1 in string)    # True
print(pattern2 in string)    # False


print('---简单匹配---')
import re
# regular expression
pattern1 = "cat"
pattern2 = "bird"
string = "dog runs to cat"
print(re.search(pattern1, string))  # <re.Match object; span=(12, 15), match='cat'>
print(re.search(pattern2, string))  # None


print('---灵活匹配---')
# multiple patterns ("run" or "ran")
ptn = r"r[au]n"       # start with "r" means raw string
print(re.search(ptn, "dog runs to cat"))    # <re.Match object; span=(4, 7), match='run'>
print(re.search(r"r[A-Z]n", "dog runs to cat"))     # None
print(re.search(r"r[a-z]n", "dog runs to cat"))     # <re.Match object; span=(4, 7), match='run'>
print(re.search(r"r[0-9]n", "dog r2ns to cat"))     # <re.Match object; span=(4, 7), match='r2n'>
print(re.search(r"r[0-9a-z]n", "dog runs to cat"))  # <re.Match object; span=(4, 7), match='run'>
print(re.search(r"r\sn","r\nn r4n"))  # <re.Match object; span=(0, 3), match='r\nn'>
print(re.search(r"\bruns\b"," runs r4n"))  # <re.Match object; span=(1, 5), match='runs'>


print('---按类型匹配---')
# \d : decimal digit
print(re.search(r"r\dn", "run r4n"))           # <re.Match object; span=(4, 7), match='r4n'>
# \D : any non-decimal digit
print(re.search(r"r\Dn", "run r4n"))           # <re.Match object; span=(0, 3), match='run'>
# \s : any white space [\t\n\r\f\v]
print(re.search(r"r\sn", "r\nn r4n"))          # <re.Match object; span=(0, 3), match='r\nn'>
# \S : opposite to \s, any non-white space
print(re.search(r"r\Sn", "r\nn r4n"))          # <re.Match object; span=(4, 7), match='r4n'>
# \w : [a-zA-Z0-9_]
print(re.search(r"r\wn", "r\nn r4n"))          # <re.Match object; span=(4, 7), match='r4n'>
# \W : opposite to \w
print(re.search(r"r\Wn", "r\nn r4n"))          # <re.Match object; span=(0, 3), match='r\nn'>
# \b : empty string (only at the start or end of the word)
print(re.search(r"\bruns\b", "dog runs to cat"))    # <re.Match object; span=(4, 8), match='runs'>
# \B : empty string (but not at the start or end of a word)
print(re.search(r"\B runs \B", "dog   runs  to cat"))  # <re.Match object; span=(8, 14), match=' runs '>
# \\ : match \
print(re.search(r"runs\\", "runs\ to me"))     # <re.Match object; span=(0, 5), match='runs\\'>
# . : match anything (except \n)
print(re.search(r"r.n", "r[ns to me"))         # <re.Match object; span=(0, 3), match='r[n'>
# ^ : match line beginning
print(re.search(r"^dog", "dog runs to cat"))   # <re.Match object; span=(0, 3), match='dog'>
# $ : match line ending
print(re.search(r"cat$", "dog runs to cat"))   # <re.Match object; span=(12, 15), match='cat'>
# ? : may or may not occur
print(re.search(r"Mon(day)?", "Monday"))       # <re.Match object; span=(0, 6), match='Monday'>
print(re.search(r"Mon(day)?", "Mon"))          # <re.Match object; span=(0, 3), match='Mon'>


print('---按类型匹配---')
string = """
dog runs to cat.
I run to dog.
"""
print(re.search(r"^I", string))                 # None
print(re.search(r"^I", string, flags=re.M))     # <re.Match object; span=(18, 19), match='I'>


print('---重复匹配---')
print(re.search(r"ab*", "a"))             # <re.Match object; span=(0, 1), match='a'>
print(re.search(r"ab*", "abbbbb"))        # <re.Match object; span=(0, 6), match='abbbbb'>
# + : occur 1 or more times
print(re.search(r"ab+", "a"))             # None
print(re.search(r"ab+", "abbbbb"))        # <re.Match object; span=(0, 6), match='abbbbb'>
# {n, m} : occur n to m times
print(re.search(r"ab{2,10}", "a"))        # None
print(re.search(r"ab{2,10}", "abbbbb"))   # <re.Match object; span=(0, 6), match='abbbbb'>


print('---分组---')
match = re.search(r"(\d+), Date: (.+)", "ID: 021523, Date: Feb/12/2017")
print(match.group())                   # 021523, Date: Feb/12/2017
print(match.group(1))                  # 021523
print(match.group(2))                  # Date: Feb/12/2017

match = re.search(r"(?P<id>\d+), Date: (?P<date>.+)", "ID: 021523, Date: Feb/12/2017")
print(match.group('id'))                # 021523
print(match.group('date'))              # Date: Feb/12/2017

print('---findall---')
# findall
print(re.findall(r"r[ua]n", "run ran ren"))    # ['run', 'ran']
# | : or
print(re.findall(r"(run|ran)", "run ran ren")) # ['run', 'ran']

print('---replace---')
print(re.sub(r"r[au]ns", "catches", "dog runs to cat"))     # dog catches to cat

print('---split---')
print(re.split(r"[,;\.]", "a;b,c.d;e"))   # ['a', 'b', 'c', 'd', 'e']

print('---compile---')
compiled_re = re.compile(r"r[ua]n")
print(compiled_re.search("dog ran to cat"))  # <re.Match object; span=(4, 7), match='ran'>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/371161?site
推荐阅读
相关标签
  

闽ICP备14008679号