day2 py总结

一. python基础

1.基础

1.交互式环境中输入表达式

  1. 操作数操作
    **指数运算
    %取模/取余数
    //整除/商数运算
    /除法
    *乘法
    -减法
    +加法
  2. 数据类型————整型、浮点型、字符型

  3. 字符串的连接与控制

    • 两个整形或浮点类型----+是相加运算符
    • 字符串类型-----+是字符串连接
  4. 在变量中保存值

    1. 赋值语句—第一次存入变量被初始化,此后可以引用;变量赋新值、老值被遗忘。

    2. 变量名—”1.数字、字母、下划线 2.空格不能带 3.数字不开头“

      # 第一单元初始函数练习
      
      print("hellow word!")
      print("What is your name ?")
      
      myname = input()
      print('It is good to meet you, '+ myname)
      
      print('It is len is :')
      print(len(myname))
      
      print('What is your age?')
      myAge = input()
      print('You will be ' + str(int(myAge)+1) + ' in a year')
      
  5. 程序剖析

    1. 注释:用来解释程序

    2. print() 函数:参数的引用,’‘表示字符串的起始—允许传入一个整形或字符

    3. input() 函数:用户输入的文本,返回一个字符串

      myname=input()
      
    4. 输出用户的名字:表达式总是可以求值

    5. len() 函数:传递另一个字符串,该函数求值为一个整型值及字串长度

    6. str()函数:字符串转换函数,

      >>> str(29)
      '29'
      >>> print('I am a ' +str(20)+' year a old')
      I am a 20 year a old
      
    7. int()函数:整形转换函数: 整形和浮点型数值数值相等

    8. float()函数:浮点数转换函数

      >>> float(90)
      90.0
      >>> float('90')
      90.0
      >>> float(90.00)
      90.0
      

2.控制流

  1. 布尔值—只含两种值、true和false—还可以用于表达式中或保存在变量中

  2. 比较操作符—比较两个值,求值为一个布尔值

    操作符含义
    ==等于
    !=不等于
    <–仅限于int\float小于–仅限于int\float
    >–仅限于int\float大于–仅限于int\float–仅限于int\float
    <=–仅限于int\float小于等于–仅限于int\float
    >=–仅限于int\float大于等于–仅限于int\float

    ==(等于):表示确定两个值是否相当,两个字符

    =(赋值):表示操作符右边的值赋值给左边的值,单字符

  3. 布尔操作符not>and>or

    1. 二元布尔操作符

      and:同为正则正

      or:有一个正则正

    2. not操作符:只作用于一个布尔值

    3. 混合布尔和比较操作符:

  4. 混合操作符

  5. 控制库元素

  6. 控制流语句

  7. 导入模块

    #常见模块导入
    import random,sys,os,math
    

    例一:

    import random, sys
    
    int_i = random.randint(1, 100)
    print('I am thinking of a number the between 1 and 20')
    
    for guess_f in range(1,7):#guess_f 是你输入的次数总共7次机会
        print('Take a guess')
        guess = int(input())#这是你猜的数
    
        if guess < int_i:
            print('you input number is too low')
        elif guess > int_i:
            print('you input number is too hight')
        else:
            break
    if guess ==int_i:
        print('good, you have input a right a number')
    else:
        print('Sorry you have input '+ str(guess_f)+
              f' and for coreet number is {int_i}')#机会用完,输出次数和正确答案
    

    例二:

    import sys, random
    
    print('ROCK, PAPER, SCISSORS')
    win = 0
    losses = 0
    ties = 0
    
    while True:
        print('%s Win ,%s Losses,%s Ties' % (win, losses, ties))  #输出赢、输、平局的次数
        while True:
            print('Enter your move choice:(r)ock  (p)aper  (s)cissors or (q)uit')
            play_move = input()			#输入你的选择:石头、布、剪子
            if play_move == 'q':
                sys.exit()
            if play_move == 'r' or play_move == 's' or play_move == 'p':		#没有退出时,你的选择
                break							
            print('type one a choice of r,p,s,q.')   		#输入提示,其他按键时触发
    
        if play_move == 'r':
            print("Rock versus...")
        elif play_move == 'p':
            print("Paper versus...")
        elif play_move == 's':
            print("Scissors versus...")
    
        random_N = random.randint(1, 3)			#随机刷新电脑对手的选择,增加随机性
        if random_N == 1:
            computer_move = 'r'
            print('ROCK')
        elif random_N == 2:
            computer_move = 'p'
            print('PAPER')
        elif random_N == 3:
            computer_move = 's'
            print('SCISSORS')
    
        if play_move == computer_move:    #平局时的情况
            print("It's a tie !")
            ties = ties +1
        elif play_move == 'r' and computer_move == 's':  #石头胜剪刀
            print("It's a win !")
            win = win +1
        elif play_move == 's' and computer_move == 'p':  #剪刀胜布
            print("It's a win !")
            win = win +1
        elif play_move == 'p' and computer_move == 'r':	  #布胜石头
            print("It's a win !")
            win = win +1
        elif play_move == 's' and computer_move == 'r':   #剪刀输石头
            print("It's a losses !")
            losses = losses +1
        elif play_move == 'r' and computer_move == 'p':		#石头输布
            print("It's a losses !")
            losses = losses +1
        elif play_move == 'p' and computer_move == 's':		#布输剪刀
            print("It's a losses !")
            losses = losses +1
    

3.函数—函数调用即函数名加(实参),跳到函数的首行开始执行,末行执行后回归调用行

代码消除重复———程序跟短、更易读、更容易更新

  1. def语句和参数

    • **定义:**创建一个函数,

    • **调用:**使用创建的函数,并将执行转至函数代码的开始处

    • **传递:**实参传递给参数

    • 参数:

    • **变元:**结束参数的复制的变量

      def sysHellow(name):
          print(f"hellow,{name}")
      sysHellow('AI')
      
  2. 返回值和return语句

    return语句的命名规则:return关键字 + 函数应该返回的值或表达式

    返回值==函数调用求值的结果

    import random
    
    def getanswer(answerNumber):
        if answerNumber == 1:
            return "It's certain"    #返回值1
        elif answerNumber == 2:
            return "It's decidedly"
        elif answerNumber == 3:
            return "Yes"
        elif answerNumber == 4:
            return "Replay hazy again"
        elif answerNumber == 5:
            return "Ask again Latter"
        elif answerNumber == 6:
            return "Concentrate and ask again"
        elif answerNumber == 7:
            return "NO"
    
    r = random.randint(1,10)  
    print(f"{getanswer(r)}")  #函数调用并打印
    
  3. None值=Nonetype

    • 等价于 null、nil、undefined

    • 不与其他有价值的值混淆,print()函数不返回任何值,返回None

    • 所有函数都有返回值

      >>None==spam
      
      True
      
  4. 关键字和print()函数

    • 关键字参数用于可变单元,由关键字识别

    • print()函数的可选单元end指定 在参数末尾输出什么

    • print()函数的可选单元sep 在参数之间用什么隔开他们—sep替换默认的分割字符串

    • print()函数 自动在传入的 字符串末尾添加换行符

    在这里插入图片描述

  5. 调用栈—python记住每个函数调用后返回那里执行

    • 程序调用一个函数,python在函数调用栈顶部创建一个帧对象
    • 帧对象保存最初函数调用的行号,记住返回的位置
    • 调用函数时又调用另一个函数,python将另一个帧对象放在调用栈中,位于前一个帧对象之上
    • 调用栈的顶部记录的是当前的执行位置,调用函数为空时执行位于所有函数之上的一行上
  6. 局部和全局作用域

    • 全局作用域中的代码不能使用任何局部变量

    • 局部作用域中的代码可以访问全局变量

    • 一个函数的中的局部作用域中的代码不能使用其他局部作用域中的变量

    • 在不同的作用域中可以命名名字相同的变量

    • 全局变量可以在局部作用域中读取

      def spam():
          global eggs
          eggs = 'spam'
      
      
      def bacon():
          eggs = 'bacon'
      
      
      def ham():
          print(eggs)
      
      
      eggs = 42
      spam()
      print(eggs)
      
  7. global语句:函数内部修改为全局变量

  8. 异常处理

    例子:输出星号带

    import time,sys
    
    indent = 0    #缩进间隔
    indentIncreasing = True   #缩进量是否增加
    
    try:
        while True:
            print(' '* indent,end='')  #不换行缩进
            print('*********')   #输出星号带
            time.sleep(0.1)		 #等待时间
    
            if indentIncreasing: 	#往后缩进
                indent = indent +1 	#逐行相差一个
                if indent ==20 :
                    indentIncreasing = False
    
            else:					#往回缩进
                indent = indent -1
                if indent==0:
                    indentIncreasing = True
    
    except KeyboardInterrupt:
        sys.exit()
    

4.列表

  1. 列表数据类型

    • 使用索引取得列表中的单个值

    • 负数索引———逆向检索

    • 利用切片取得子列表———切片[start,end]不包括end的值

    • 用len()函数取得列表的长度———

    • 用索引改变列表中的值

    • 列表连接和列表复制

      spam = [['pig', 'dog', 'cat', 'bat', 'elephant'], [1, 2, 3, 4, 5, 6, 7, 8]]
      print(spam[0][-1])   #检索
      print(spam[1][-1])
      print(spam[:])			#切片
      spams = ['tigger', 'lion', 'dragon', 'mice']
      spam = spams + spam #列表连接
      print(spam)
      spam = spam * 3   #列表复制
      print(spam)
      
      
    • 用del语句从列表中删除值———取消赋值,

  2. 使用列表———创建许多独立的变量

    catnames = []   #空白列表
    while True:
        print('Please input name of your cat '+str(len(catnames)+1)+' Or input Nothing')
        names = input()    #输入增加的名字
        if names == '': 
            break
        catnames = catnames + [names]    #名字列表
    print("The list of name is: ")
    for name in catnames:
        print(name)
    
    
  3. 增强的赋值操作

  4. 方法

  5. 序数数据类型

  6. 引用

  7. 实践项目

5.字典和结构化数据

6.字符串操作

2.自动化任务

1.规则匹配与正则表达式

2.输入验证

3.读写文件

4.组织文件

5.调试

6.从web中抓取数据

names = input()    #输入增加的名字
   if names == '': 
       break
   catnames = catnames + [names]    #名字列表

print("The list of name is: ")
for name in catnames:
print(name)


3. 增强的赋值操作

4. 方法

5. 序数数据类型

6. 引用

7. 实践项目

### 5.字典和结构化数据

### 6.字符串操作

## 2.自动化任务

### 1.规则匹配与正则表达式

1. 

### 2.输入验证

1. 

### 3.读写文件

1. 

### 4.组织文件

1. 

### 5.调试

1. 

### 6.从web中抓取数据

1. 
  • 46
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值