python基础

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(" I 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(" I 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

原文地址:https://www.cnblogs.com/cyying/p/13273128.html