当前位置 博文首页 > 文章内容

    python基础

    作者: 栏目:未分类 时间:2020-07-17 11:00:46

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    1、列表形式

    people = {'lily','lucy','jack','rose'}

    print(people [0])  打印值

    people.append('meimei')  追加值

    people.insert(0,'lili')  插入值

    del people [0]   删除任意值

    people.pop()  删除末尾的值

    people.pop(0)  删除任意值

    people.romve('lily')  根据值删除元素

    people.sort()  永久排序

    sorted(people)  临时排序

    people.reverse()  倒着打印列表

    len(people)  列表长度

    for user in people:  遍历列表

      print(user)

    for user in people[:3]:  遍历前3名

    new people = people [:]  复制列表

    2、元组形式

    animals = ('duck','bee')

    for animal in animals:  遍历元组

    3、字典形式

    alien_0 = {'color': 'green', 'points': 5}

    alien_0 ['color']  green,访问字典的值

    for key,value in alien_0.items():  遍历所有键值对

    for key in alien_0.keys():  遍历所有键

    for key in sorted(alien_0.keys()):  按顺序遍历所有的键

    for value in alien_0.values():  遍历所有值

    for value in set(alien_0.values()):  遍历值,去重

    4、字典列表

    alien_0 = {'color': 'green', 'points': 5}
    alien_1 = {'color': 'yellow', 'points': 10}
    alien_2 = {'color': 'red', 'points': 15}
    aliens = [alien_0, alien_1, alien_2]

    5、字典存列表

    pizza = {'crust':'thick','toppings':['mushrooms', 'extra cheese']}

    6、字典存字典

    users = {
    'aeinstein': {
    'first': 'albert',
    'last': 'einstein',
    'location': 'princeton',
    },
    'mcurie': {
    'first': 'marie',
    'last': 'curie',
    'location': 'paris',
    },
    }

    for username, user_info in users.items():

      full_name = user_info['first']

    7、在循环中使用continue

    current_number = 0

    while current_number < 10:
       current_number += 1
      if current_number % 2 == 0:
        continue
      print(current_number)

    if 语句检查 current_number

    与2的求模运算结果。如果结果为0(意味着 current_number 可被2整除) ,就执行 continue 语句,
    让Python忽略余下的代码,并返回到循环的开头。如果当前的数字不能被2整除,就执行循环中
    余下的代码,Python将这个数字打印出来:

     8、函数

    def  greet_user(username):

      print("hello " + username)

    greet_user(lily)

    9、传递参数

    (1)位置实参

    def describe_pet(animal_type, pet_name):
      print("\nI have a " + animal_type + ".")
      print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet('harry', 'hamster')     //形参与实参的位置要一一对应

    (2)关键字实参

    def describe_pet(animal_type, pet_name):
      print("\nI have a " + animal_type + ".")
      print("My " + animal_type + "'s name is " + pet_name.title() + ".")
    describe_pet(animal_type='hamster', pet_name='harry')   // 关键字实参

    10、列表副本传递给函数

    function_name ( list_name [:])

    11、传递任意数量的形参

    def make_pizza(*toppings):
      print(toppings)
    make_pizza('pepperoni')
    make_pizza('mushrooms', 'green peppers', 'extra cheese')

    形参名 *toppings 中的星号让Python创建一个名为 toppings 的空元组, 并将收到的所有值都封

    装到这个元组中。函数体内的 print 语句通过生成输出来证明Python能够处理使用一个值调用函
    数的情形,也能处理使用三个值来调用函数的情形。

    12、使用任意数量的关键字实参

    def build_profile(first, last, **user_info):

      profile = {}
      profile['first_name'] = first
      profile['last_name'] = last
      for key, value in user_info.items():
       profile[key] = value
    return profile
    user_profile = build_profile('albert', 'einstein',
    location='princeton',
    field='physics')
    print(user_profile)

    形参 **user_info 中的两个星号让Python创建一个名为 user_info 的空字典,并将收到的所
    有名称 — 值对都封装到这个字典中。

    13、导入整个模块

    import pizza(被导入文件名)

    14、导入特定函数

    from  文件名 import  function_0 ,  function_1 ,  function_2

    15、导入模块中的所有函数

    from  module_name import *

    16、使用 as 给函数指定别名

    from  module_name import  function_name as  fn

    17、使用 as 给模块指定别名

    import  module_name as mn