zoukankan      html  css  js  c++  java
  • 基础二

    一、
    1.while循环语句的用法
    while 条件:
    子代模块
    2.while语法说明
    函数名为while循环判断条件,当值为True时,继续执行子代码;当值为False时,终止循环。


    3.嵌套
    a,b=0,2
    while a<=0:
    while a<b:
    print("%s"%((a+1)*b))
    b-=1
    a+=1
    二、for循环语句
    1.for循环基本用法
    for i in range:
    子代模块1
    else:
    子代模块2
    2.for语句基本语法
    i接收range集合中获取的的成员元素,循环一次接收一次。else为当for循环结束时,在执行对应子代模块2。

    3.循环控制语句
    (1)break
    当while或者for循环过程所产生的的操作已经满足业务要求时,可以通过break语句立即停止并跳出循环,避免过度循环次数发生,以提高处理效率。

    (2)continue
    当满足指定条件时,continue使循环到开始处,继续循环,而忽略continue语句后的执行代码行。

    三、复杂条件及处理1.成员运算符in,not i2.身份运算is,is not

    3.运算符优先级

    1
    **
    指数
    2
    ~、+、-
    按位翻转、正数、负数
    3
    *、/、%、//
    乘、除、取模、取整
    4
    +、-
    加法、减法
    5
    >>、<<
    右移、左移
    6
    &
    位于(AND)运算符
    7
    ^|

    8  ==、!=、<、>、>=、<=  比较运算符

    9  ==、%=、/=、//=、-=、*=、**=  赋值运算符

    10  is、is not  身份运算符

    11  in、 not in  成员运算符

    12  not、or、and  逻辑运算符

    一、列表
    1.定义:[],是可变的序列,也是可以存储各种数据类型的集合。用于(,)分隔。
    2.基本操作:
    append 增加元素
    insert 在指定下标下增加元素
    clear 清除
    copy 拷贝
    count 统计数量
    extend 合并两个列表
    index 返回指定元素下标
    pop 删除指定下标元素
    remove 删除指定下标元素
    reverse 反转列表元素
    sort 对列表元素进行排序
    例题:
    1.计算鱼的总数和钱数,打印表格
    fish_record=['1月1日','鲫鱼',18,10.5,'1月1日','鲤鱼',8,6.2,'1月1日','鲢鱼',7,4.7,'1月2日','草鱼',2,7.2,'1月2日','鲫鱼',3,12,'1月2日','黑鱼',6,15,'1月3日','乌龟',1,71,'1月3日','鲫鱼',1,9.8]
    num=0
    price=0
    i=0
    while i<len(fish_record):
    num=num+fish_record[i+2]
    price=price+fish_record[i+2]*fish_record[i+3]
    print('%s,%s,%d,%d'%(fish_record[i],fish_record[i+1],fish_record[i+2],fish_record[i+3]))
    i+=4
    print('总数%d,总资金%d'%(num,price))
    2.while排序
    fish_record=[18,8,7,2,3,6,1,1,2,4]
    i=0
    len1=len(fish_record)
    while i<len1:
    j=1
    while j<len1-i:
    if fish_record[j-1]>fish_record[j]:
    a=fish_record[j-1]
    fish_record[j-1]=fish_record[j]
    fish_record[j]=a
    j+=1
    i+=1
    print(fish_record)
    二、元组
    1.定义:
    元组(),是不可变序列,也是一种可以存储各种数据类型的集合,用()括号表示,元素之间以(,)逗号分隔。 2.元组基本操作:
    count 统计元素个数
    index 返回元素下标
    内置函数
    len() 元素个数统计
    max() 返回元素中最大元素
    min() 返回元素中最小元素
    tuple() 转换元组
    type() 查看对象类型
    del() 删除整个元组
    sum() 对元组对象求

    一、字典基本知识
    1.定义:
    字典是可变的无序集合,同时是一种以键值对为基本元素的可以存储各种数据类型的集合,用{}大括号表示,元素之间用(,)逗号分隔。
    2.字典基本方法:
    clear 字典清空
    copy 拷贝
    fromkeys 使用给定的健建立新的字典
    get 根据指定键,返回相应值
    items 以元组数组形式返回字典中元素
    keys 以浏览器类似列表形式返回字典中元素
    pop 删除指定键的元素
    popitem 随机返回元素,并删除元素
    setdefault 增加键值对
    update 利用一个字典更新另一个字典
    values 以浏览器类似列表形式返回字典中值
    (1)字典元素增加
    赋值增加;setdefault()增加
    (2)字典值查找
    字典名+['查找值']; get()查找;
    (3)修改
    赋值修改;update()修改
    (4)删除
    del()删除;pop()删除;popitem()删除
    3.字典遍历操作
    (1)遍历所有键值对
    items()方法
    dl={'cui':10,'de':4,'ling':9}
    for i in dl.items():
    print(i)
    (2)遍历所有键
    1.利用字典变量循环遍历
    for i in dl:
    print(i)
    2.利用keys()方法
    for i in dl.keys():
    print(i)
    (3)遍历所有值
    1.通过键遍历
    dl={'cui':10,'de':4,'ling':9}
    for i_key in dl:
    print(dl[i_key])
    2.利用values()方法
    dl={'cui':10,'de':4,'ling':9}
    for i_key in dl.values():
    print(i_key)
    4.字典嵌套
    (1)字典嵌入字典
    no1={'张三':35.5,'李四':200,'王五':800}
    no2={'Tom':99.8,'John':183,'Jim':429}
    no3={'阿毛':12,'阿狗':33}
    rest={'1号':no1,'2号':no2,'3号':no3}
    print(rest)
    total=0
    for i in rest.values():
    total=total+sum(i.values())
    print(total)
    (2)列表嵌入字典
    no1=[35.5,200,800]
    no2=[99.8,183,429]
    no3=[12,33]
    res={'1号':no1,'2号':no2,'3号':no3}
    print(res)
    for i ,k in res.items():
    b=sum(k)
    print('%s,%d'%(i,b))
    (3)字典嵌入列表
    no1={'张三':35.5,'李四':200,'王五':800}
    no2={'Tom':99.8,'John':183,'Jim':429}
    no3={'阿毛':12,'阿狗':33}
    res=[no1,no2,no3]
    print(res)
    i=0
    total=0
    d={}
    sum1=0
    while i<len(res):
    d=res[i]
    sum1=sum(d.values())
    total+=sum1
    #print(d)
    print('%d,%f'%(i+1,sum1))
    i+=1
    print('%d'%(total))

    一、函数基本
    1.定义:函数是指通过专门的代码组织,用来实现特定功能的代码段,具有相对独立性,可供其它代码重复调用。
    2.基本语法:
    def 函数名([参数]):
    函数体
    [return 返回值]
    二、自定义函数第一步
    1.不带参数函数
    (1)不带参数格式:
    def 函数名():
    函数体
    (2)实例:求10的因数
    def fan():
    i=1
    b=10
    while i<=b:
    if b%i==0:
    print(i)
    i+=1
    fan()
    2.带参数函数
    (1)带参数函数格式:
    def 函数名(参数):
    函数体
    (2)实例:自己定义整数求因数
    def hun(num):
    i=1
    str1=''
    while i<=num:
    if num%i==0:
    str1 = str(i) + ',' + str1
    i+=1
    print(str1)
    a=[10,20,30,40]
    i=0
    while i<len(a):
    hun(a[i])
    i+=1
    3.待返回值函数
    (1)带返回值格式:
    def 函数名([参数]):
    函数体
    return 返回值
    (2)实例:
    def fan(num):
    i=1
    str1=''
    while i<=num:
    if num%i==0:
    str1=str(i)+','+str1
    i+=1
    return str1
    q=[10,20,30,40]
    i=0
    while i<len(q):
    a=fan(q[i])
    print('%d的因数是%s'%(q[i],a))
    i+=1
    切记:传参数必须要赋值
    4.把函数放到模块中
    (1)import导入模块
    模块名.函数名
    (2)import导入指定函数:form 模块 import函数名
    (3)import导入所有函数:from 模块名import *
    (4)import导入模块指定所有函数:from 模块名 as 别名
    二、自定义函数第二步
    1.位置参数:
    所谓位置参数就是在传递参数时,必须和函数传递的参数一一对应。
    2.函数与变量作用域
    (1)全局变量与局域变量
    全局变量是自赋值定义开始,后续代码都可以访问该变量。
    局域变量是只能在被定义内部被访问。
    例如:
    j=5 # j全局变量
    def fun(i):
    i=i+j # i为局域变量
    return i
    i=9 #i为全局变量
    print(fun(6))
    3.global关键字:如若修改全局变量值,需要先给global说明一下,不若会报错。
    例如:
    j=5
    l=2
    def sum1():
    global j
    j=j+5
    l=4
    return l
    4.闭包(Closure)
    闭包是全局变量和局域变量之间的一种特殊变量。
    例如:
    j=5
    def fun():
    k=2
    def sum():
    i=k+j
    return i
    return sum()
    print('调用fun()的结果是%d'%(fun()))
    5.nonlocal关键字:如果要修改闭包k=2的值首先要通过nonlocal声明一下。
    6.匿名函数
    (1)匿名函数是python使用lambda创建匿名函数。
    (2)匿名函数的特点:
    @lambda没有函数名
    @参数时可选的
    @整个函数在一行内实现
    (3)expression返回函数

    一、类
    1.定义:指把具有相同特性(数据)和行为(函数)的对象抽象为类。
    例如:
    class box():
    def __init__(self,length,wight,height):
    self.length=length
    self.wight=wight
    self.height=height
    def volume(self):
    return self.length*self.wight*self.height
    prints=box(10,10,10)
    print(prints.volume())
    2.属性修改
    class box1():
    def __init__(self):
    self.lenght=0
    self.wight=0
    self.height=0
    a=box1()
    a.lenght=10
    print(a.lenght)
    (2)通过方法对属性修改
    class box1():
    def __init__(self):
    self.lenght=0
    self.wight=0
    self.height=0
    def set(self,lenght1):
    self.lenght=lenght1
    b=box1()
    b.set(19)
    print(b.lenght)
    3.类的继承
    继承定义:就是在继承原有类的功能基础上,增加新的功能,形成新的子类。被继承的叫做父类。

  • 相关阅读:
    Miller-Rabin素性测试
    ###Canny边缘检测算子
    ###SIFT特征
    建立一个免费的网站
    ###C中的extern-static-const关键词
    ###Git使用问题
    ###Fedora下安装Retext
    ###使用phpmailer自动邮件提醒
    Markdown学习
    有线和无线同时连接,选择其一上网
  • 原文地址:https://www.cnblogs.com/cui00/p/12234338.html
Copyright © 2011-2022 走看看