• Python基础笔记


    一、字符串

    1.在字符串中插入变量的值,可在前引号前加字母f,再将要插入的变量放在花括号内

    first_name = "c"
    last_name = "y"
    full_name = f"{first_name} {last_name}"
    

    format

    first_name = "c"
    last_name = "y"
    full_name = "{} {}".format(first_name,last_name)
    

    2. 删除空白
    删除末尾空白rstrip()
    删除开头空白lstrip()
    删除两边空白strip()

    二、数

    **表示乘方

    三、列表

    1.-1:表示访问最后一个元素,-2表示方位倒数第二个元素…
    2.删除元素
    del name[0]表示删除name列表的第一个元素
    pop()表示删除最后一个元素
    pop(1)表示删除第二个元素
    remove()根据值删除元素
    3.for循环

    magicians = ['alice','david','carolina']
    for magician in magicians:
        print(magician)
    

    4.创建数值列表
    (1)range()
    range(1,5)表示1,2,3,4
    range(6)表示0,1,2,3,4,5
    range(2,11,2)表示2,4,6,8,10
    (2)list()
    number = list(range(1,6)) #list可直接将range的值转化为列表
    (3)列表解析
    squares = [value**2 for value in range(1,11)]
    5.使用列表
    (1)
    切片 [1:3]表示1,2,
    [:3]表示0,1,2
    [1:]表示1到最后一个
    [-3:]
    (2)复制切片
    friend1 = friend[:]表示复制friend所有元素给friend1
    friend1 = friend表示指向同一个列表的两个向量,没有复制的概念

    四、元组

    不可变的列表被称为元祖
    dimensions = (200,50)
    dimensions = (3,)

    五、if语句

    if-elif-else

    六、字典(结构体)

    1.删除键值对

    alien_0 = {'color': 'green', 'points': 5} 
    del alien_0['points'] 
    

    2.遍历字典

    user_0 = { 
    'username': 'efermi', 
    'first': 'enrico', 
    'last': 'fermi', 
    } 
    for key, value in user_0.items(): 
        print(f"\nKey: {key}") 
        print(f"Value: {value}")
    

    3.集合set中每个元素必须是独一无二的
    可使用{}直接创造set集合

    七、函数

    1.传递实参
    (1)关键字实参

    def describe_pet(animal_type, pet_name):
        ......
    describe_pet(animal_type='hamster', pet_name='harry') 
    

    (2)传递任意数量的实参

    def make_pizza(*toppings): 
    """*代表接收任意数量的列表"""
        print(toppings) 
    make_pizza('pepperoni') 
    make_pizza('mushrooms', 'green peppers', 'extra cheese')
    

    (3)使用任意数量的关键字实参

    def build_profile(first, last, **user_info): 
    """创建一个字典,其中包含我们知道的有关用户的一切。**代表接收任意数据的字典""" 
        user_info['first_name'] = first 
        user_info['last_name'] = last 
        return user_info 
    user_profile = build_profile('albert', 'einstein', 
        			location='princeton', 
        			field='physics') 
    print(user_profile)
    

    2.函数调用
    (1)

    import pizza 
    pizza.make_pizza(16, 'pepperoni') 
    pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
    

    (2)只调用某个函数

    from pizza import make_pizza 
    make_pizza(16, 'pepperoni') 
    make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese') 
    

    (3)使用as 给函数指定别名

    from pizza import make_pizza as mp 
    mp(16, 'pepperoni') 
    mp(12, 'mushrooms', 'green peppers', 'extra cheese')
    

    (4)使用as 给模块指定别名

    import pizza as p 
    p.make_pizza(16, 'pepperoni') 
    p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
    

    (5)导入模块中的所有函数

    from pizza import * 
    make_pizza(16, 'pepperoni') 
    make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
    
  • 相关阅读:
    Docker 基本管理
    华为软件测试笔试真题之变态逻辑推理题
    Maxima and minima
    12306购票辅助工具
    OpenAI官方吴达恩《ChatGPT Prompt Engineering 提示词工程师》(3)摘要
    2024050401-重学 Java 设计模式《实战代理模式》
    Ubuntu 20.04 安装 SmartGit
    LeSS敏捷框架高效生产力实践
    快乐数(C++解法)
    【JVM学习笔记】内存回收与内存回收算法 就哪些地方需要回收、什么时候回收、如何回收三个问题进行分析和说明
  • 原文地址:https://blog.csdn.net/weixin_40385285/article/details/127111351