美文网首页
python序列应用

python序列应用

作者: solar_4869 | 来源:发表于2019-07-22 09:15 被阅读0次

    实验一Python序列应用

    1.2import math  # 导入标准库mathprint(3 +5)

    print(math.sqrt(9))  # 求9的平方根print(3 *

    (2 + 6))

    print(2 /0)

    def main():

    print('Hello

    world')  # 函数打印hello wordmain()

    1.4

    # 1.4.2python 变量x = 3

    print(type(x))  # 打印变量类型x = 'Hello world'

    print(type(x))

    x = [

    1, 2, 3]

    print(type(x))

    print(isinstance(3, int))  # 用内置函数isinstance()来测试对象是否为指定类型print(isinstance('hello world', str))

     

    # python变量运算符x = 3

    print(x ** 2)

    x +=

    # 修改变量值print(x)

    x = [

    1, 2, 3]  # 创建列表对象print(x)

    x[

    1] = 5  # 修改列表元素值print(x)

    print(x[2])  # 输出显示列表指定元素

    # 字符串元组是不可变序列x = (1, 2, 3)

    print(x)

    x[1]=5  #错误,不能修改tuple

    # 多个变量指向同一个值x = 3

    print(id(x))

    y = x

    print(id(y))

    x += 6  # 为一个变量修改值后不影响另一个变量print(id(x))

    print(y)

    print(id(y))

    # python为不同的变量赋值相同值,这个值在内存中只有一份,多个变量指向同一块内存地址x = 3

    print(id(x))

    y =

    3

    print(id(y))

    y =

    5

    print(id(y))

    print(id(x))

    importkeyword  # 查看所有python关键字print(keyword.kwlist)

    #

    and=3

    1.4.3 数字

    # 数字可以任意大a = 9999999999999999

    print(a * a)

    print(a ** 3)

    print(3 * (2 + 5) / 3.0)

    print(math.sqrt(3 ** 2 + 4 ** 2))

    # 复数a = 3 + 4j

    b = 5 + 6j

    c = a + b

    print(c)

    print(c.real)

    print(c.imag)

    print(a.conjugate())  # a的共轭复数print(a * b)

    print(a / b)

    1.4.4字符串# 字符串a = 'abc' + '123'

    print(a)

    a =

    3.6674

    print('%7.3f' % a)  # 格式化print("%d:%c" % (65, 65))  # 10进制:ASCII码print("""My name is %s, and my age is %d""" % ('Wangyuhang', 20))

     

    1.4.5 运算符与表达式# python 除法print(3 / 5)

    print(3 // 5)

    print(3.0 / 5)

    print(3.0 // 5)

    print(13 // 10)

    print(-13 // 10)

    # 计算余数print(3.1 % 2)

    print(6.3 % 2.1)

    print(6 % 2)

    print(6.0 % 2)

    print(6.0 % 2.0)

    print(5.7 % 4.8)

    # 乘法print(3 * 2)

    print(2.0 * 3)

    print((3 + 4j) * 2)

    print((3 + 4j) * (3 - 4j))

    print('1' * 5)

    print("a" * 10)

    print([1, 2, 3] * 3)

    print((1, 2, 3) * 3)

    print(3 * 'a')

    # 表达式a = [1, 2, 3]

    b = [

    4, 5, 6]

    c = a + b

    print(c)

    d =

    list(map(str, c))  # map映射对每个c中元素加str

    print(d)

    print(list(map(math.sin,c)))

     

    # 表达式a = [1, 2, 3]

    b = [

    4, 5, 6]

    c = a + b

    print(c)

    d =

    list(map(str, c))  # map映射对每个c中元素加str

    print(d)

    print(list(map(math.sin,c)))

    print('Hello'+' '+'world')

    print('welcome '*3)

    print(('welcome,'*3).rstrip(',')+'!') #strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。# python 中逗号并不是运算符,只是普通分隔符,因此没有优先级print('a'in 'b','a')

    print('a'in ('b','a'))

    x=

    3.5

    print(x)

    print(3==3,5)

    print(3+5,7)

     

    # 矩阵运算符@

    import numpy  # numpy用于科学计算的python扩展库x = numpy.ones(3)  # ones()函数用于生成全1矩阵,参数表示矩阵大小m = numpy.eye(3) * 3  # eye()函数用于生成单位矩阵m[0, 2] = 5

    m[2, 0] = 3

    print(x @ m)

     

    1.4.6常用内置函数

    # ord() and chr()

    print(ord('A')) #ord用来返回单个字符的Unicode编码或ASCIIprint(chr(65)) # chr用来返回指定ASCII码或Unicode码对应的字符print(chr(ord('A')+1))

    print(str(1)) #str用来将任意类型参数转换为字符串print(str(1234))

    print(str([1,2,3]))

    print(str((1,2,3)))

    print(str({1,2,3}))

    import random

    a=[random.randint(

    1,100) for i in range(10)] # 用列表推导式生成包含十个1100的随机数列表print(a)

    print(max(a),min(a),sum(a))

    print(max(['aa','b'],key=len)) # key为比较规则a=[72,26,80,65,34,86,19,74,52,40]

    print(sum(a)*1.0/len(a)) #Python2求平均值print(sum(a)/len(a))    #Python3求平均值# dir()查看指定模块中包含的所有成员或指定对象类型所支持的操作# help()返回指定模块或函数的说明文档import math

    print(dir(math))       #查看模块中可用对象help(math.sqrt)

    help(math.sin)

    print(dir(3+4j))   # 查看数字类型对象成员print(dir(''))

     

    1.4.7对象的删除

    x=[1,2,3,4,5,6]

    y=

    3

    z=y

    print(y)

    del y

    print(y)

    print(z)

    del z

    print(z)

    del x[1]    #删除列表中指定元素print(x)   

    del x       #删除整个列表print(x)

    x=(1,2,3)

    del x [1]   #del命令无法删除元组或字符串中的指定元素,因为他们属于不可变序列

    del x

    print(x)

     

    1.4.8基本输入输出

    Python2和Python3对输入函数解释略有不同。

    Python2中input函数返回的值类型由输入时所使用的界定符决定raw_input函数返回类型一律为字符串。

    而Python3中不存在raw_input函数且input函数只返回字符串,需要将其转换为相应类型再处理

    以下为Python3代码结果

    它们的输出也不同,Python2使用print语句输出,而python3使用print()函数输出。

    Python2重定向

    Python3重定向

    count.txt中的结果

    Python2在print语句后加逗号表示不换行

    但在Python3要实现不换行要加end=’’

     

    1.4.9 模块的导入与使用

     

    1.5 Python代码编写规范

    依靠代码缩进量来体现代码之间的逻辑关系,同一级别的代码块的缩进量必须相同

     

    1.9 Python编程快速入门

    例1-1 用户输入三位自然数,计算并输出其百位十位个位上的数字

    # 1-1 用户输入三位自然数,计算并输出其百位十位个位上的数字x = input("请输入一个三位数:")

    x=

    int(x)

    a=x//

    100

    b=x//10%10

    c=x%10

    print(a,b,c)

    例1-2 已知三角形两边长及其夹角求第三边长

    import math

    x=

    input('输入两边长和夹角(度):')

    a,b,theta=

    map(float,x.split())

    c=math.sqrt(a**

    2+b**2-2*a*b*math.cos(theta*math.pi/180))

    print('c=:',c)

    例1-3 任意输入三个英文单词,按字典顺序输出

    s=input('x,y,z=')

    l=

    list

    l=s.split()

    l.sort()

    print(l)

    s=

    input('x,y,z=')

    x,y,z=s.split(

    ',')

    x,y,z=

    sorted([x,y,z])

    print(x,y,z)

    例4-1 Python程序框架生成器

    import os

    import sys

    import datetime

    head =

    '#' + '-' * 20 + '\n' + \

    '#Function description:\n' + \

    '#' + '-' * 20 + '\n' + \

    '#Author:Dong Fuguo\n' + \

    '#QQ:306467355\n' + \

    '#Email:dongfuguo2005@126.com\n' + \

    '#' + '-' * 20 + '\n'

    desFile = sys.argv[1]

    if os.path.exists(desFile) or not desFile.endswith('.py'):

    print('%s already exist or is not a Python code file.!' % desFile)

        sys.exit()

    fp =

    open(desFile, 'w')

    today =

    str(datetime.date.today())

    fp.write(

    '#-*-coding:utf-8-*-\n')

    fp.write(

    '#Filename:' + desFile + '\n')

    fp.write(head)

    fp.write(

    '#Date:' + today + '\n')

    fp.write(

    '#' + '-' * 20 + '\n')

    fp.close()

    用IDLE打开newProgram.py

    Python源码剖析[16] —— Pyc文件解析

    相关文章

      网友评论

          本文标题:python序列应用

          本文链接:https://www.haomeiwen.com/subject/pccdnxtx.html