''' 字符串的格式化方法一,示例 ''' name="张三" money=102 desc="今天收到{}的学费{}元" string=desc.format(name,money) print(string) #今天收到张三的学费102元 ''' 字符串的格式化方法一,示例 ''' str = '今天在{},好多额呀'.format("北京") print(str) #今天在北京,好多额呀 ''' 字符串的格式化方法二,示例 ''' name001 = '小明' a = 10 str2 = "我叫 %s 今年 %d 岁!" % (name001,a) print (str2) #我叫 小明 今年 10 岁!
''' 循环拿出列表里面的数据,方法一示例 ''' list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] for i in list1: print(i) ''' 执行结果: Google Runoob Taobao Baidu ''' ''' 循环拿出列表里面的数据,方法二示例 ''' list1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] i = 0 while i < len(list1): print(list1[i]) i=i+1 ''' 执行结果为: Google Runoob Taobao Baidu '''
''' 直接遍历字典,这样可以同时获取键和值,但如果只关心值,可以忽略键: ''' my_dict = {'a': 1, 'b': 2, 'c': 3} for key, value in my_dict.items(): print(value) ''' 执行结果: 1 2 3 ''' ''' 直接遍历字典,这样可以同时获取键和值,但如果只关心值,可以忽略键: ''' my_dict8 = {'a': 1, 'b': 2, 'c': 3} for key, value in my_dict8.items(): print(key + ' '+str(value)) ''' 执行结果: a 1 b 2 c 3 ''' ''' 获取字典里面所有的keys ''' dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} keys = dishes.keys() # 动态视图对象 print(keys) #dict_keys(['eggs', 'sausage', 'bacon', 'spam']) print(type(keys)) #<class 'dict_keys'> print(type(list(keys))) #<class 'list'> print(list(keys)) #['eggs', 'sausage', 'bacon', 'spam'] ''' ''' my_dict = {'a': 1, 'b': 2, 'c': 3} print(my_dict.keys()) #dict_keys(['a', 'b', 'c']) print(list(my_dict.keys())) #['a', 'b', 'c'] other_list = list(my_dict.keys()) i = 0 while i < len(other_list): print(other_list[i]) i=i+1 ''' 执行结果: dict_keys(['a', 'b', 'c']) ['a', 'b', 'c'] a b c ''' ''' 获取字典里面所有的values ''' dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} values = dishes.values() #动态视图对象 print(values) #dict_values([2, 1, 1, 500]) print(type(values)) #<class 'dict_values'> print(type(list(values))) #<class 'list'> print(list(values)) #[2, 1, 1, 500]
''' 将字典转换为[(key, value), (key, value), (key, value), (key, value)])格式的集合 ''' dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} itsar = dishes.items() print(itsar) #dict_items([('eggs', 2), ('sausage', 1), ('bacon', 1), ('spam', 500)]) print(type(itsar)) #<class 'dict_items'> print(type(list(itsar))) #<class 'list'> print(list(itsar)) #[('eggs', 2), ('sausage', 1), ('bacon', 1), ('spam', 500)]
import random resd = random.random() print(resd) ''' 执行结果:0.35256366817173646 ''' for i in range(0,10) : print(i) ''' 执行结果: 0 1 2 3 4 5 6 7 8 9 ''' for i in range(0,10,2) : print(i) ''' 执行结果: 0 2 4 6 8 ''' tup1 = ('Google', 'Runoob', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7) print("tup1[0]: ", tup1[0]) #tup1[0]: Google print("tup2[1:5]: ", tup2[1:5]) #tup2[1:5]: (2, 3, 4, 5) numbers001 = list(range(1, 6)) print(numbers001) #[1, 2, 3, 4, 5] numbers002 = tuple(range(1, 6)) print(numbers002) #(1, 2, 3, 4, 5)
123
标签:复习,python,list,基础,value,keys,dict,print,values From: https://www.cnblogs.com/xiaobaibailongma/p/18111081