python基础——基础代码每日复习001

'''
字符串的格式化方法一,示例
'''

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)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

lista = [1,2,3,4,5,6,7]
print(len(lista))
print(lista.index(2))

'''
执行结果:
7
1
'''









print("---------------------------------------------1")










#在 Python 中将列表转换为字符串的多种方法
listb = ["a","b","c","d"]
print("".join(listb))             #abcd

print("---------------------------------------------2")









'''
使用 for 循环将列表转换为字符串
使用 for 循环可以循环访问列表中的每个元素并将其追加到新字符串。
与前面的示例类似,如果我们尝试连接非字符串,这将引发TypeError。为了修复此错误,使用 str() 函数对元素类型进行转换。
我们还可以在 for 循环中包含分隔符来分隔字符串。
'''
list1 = ['Welcome', 'to', 'zbxx.net', 123]
str1 = ''
for item in list1:
    # str1 = str1 + ' ' + str(item)
    str1 = str1+str(item)
print(str1)









print("---------------------------------------------3")












'''
使用列表推导将列表转换为字符串
我们还可以轻松地将列表推导式与jion()方法结合使用将列表转换为字符串。
'''
list1 = ['Welcome', 'to', 'zbxx.net', 123]
list2 = [str(item) for item in list1]
str1 = ' '.join(list2)
print(str1)












print("---------------------------------------------4")










names = ['Bob','Tom','alice','Jerry','Wendy','Smith']
new_names = [name.upper() for name in names ]
print(new_names)
['ALICE', 'JERRY', 'WENDY', 'SMITH']

print("---------------------------------------------5")

names = ['Bob','Tom','alice','Jerry','Wendy','Smith']
new_names = [name.upper() for name in names if len(name)>3]
print(new_names)
['ALICE', 'JERRY', 'WENDY', 'SMITH']






print("---------------------------------------------6")





multiples = [i for i in range(30) ]
print(multiples)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]






print("---------------------------------------------7")





multiples = [i for i in range(30) if i % 3 == 0]
print(multiples)
[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#自定义 len() 函数
def my_len(str):
    length = 0
    for c in str:
       length = length + 1
       print(c)
    return length


#调用自定义的 my_len() 函数
length = my_len("http://c.biancheng.net/python/")
print("字符串长度是:" + str(length))

'''
执行结果:
h
t
t
p
:
/
/
c
.
b
i
a
n
c
h
e
n
g
.
n
e
t
/
p
y
t
h
o
n
/
字符串长度是:30
'''

 

 

 

 

'''

Python 中,根据实际参数的类型不同,函数参数的传递方式可分为 2 种,分别为值传递和引用(地址)传递:
值传递:适用于实参类型为不可变类型(字符串、数字、元组);
引用(地址)传递:适用于实参类型为可变类型(列表,字典);


值传递和引用传递的区别是,函数参数进行值传递后,若形参的值发生改变,不会影响实参的值;而函数参数继续引用传递后,改变形参的值,实参的值也会一同改变。

'''

 

 

 

 

def demo(obj) :
    obj += obj
    print("形参值为:",obj)


a = "C语言中文网"
print("a的值为:",a)


demo(a)
print("实参值为:",a)


'''
a的值为: C语言中文网
形参值为: C语言中文网C语言中文网
实参值为: C语言中文网

'''

 

 

def demo(obj) :
    obj += obj
    print("形参值为:",obj)

a = [1,2,3]
print("a的值为:",a)

demo(a)
print("实参值为:",a)

'''
a的值为: [1, 2, 3]
形参值为: [1, 2, 3, 1, 2, 3]
实参值为: [1, 2, 3, 1, 2, 3]

'''

 

 

 

 

 

 

'''

必备参数:

位置参数,有时也称必备参数,指的是必须按照正确的顺序将实际参数传到函数中,换句话说,调用函数时传入实际参数的数量和位置都必须和定义函数时保持一致。
实参和形参数量必须一致,在调用函数,指定的实际参数的数量,必须和形式参数的数量一致(传多传少都不行)







默认参数:

Python函数默认参数设置——————————def dis_str(str1,str2 = "http://c.biancheng.net/python/"):






关键字参数:

关键字参数是指使用形式参数的名字来确定输入的参数值。通过此方式指定函数实参时,不再需要与形参的位置完全一致,只要将参数名写正确即可。
混合传参时关键字参数必须位于所有的位置参数之后。

'''




#不定长参数
#你可能需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数,和上述2种参数不同,声明时不会命名。




# 可写函数说明
def printinfo(arg1, *vartuple):
    "打印任何传入的参数"
    print("输出: ")

    print(arg1)

    print(vartuple)

    for var in vartuple:
        print(var)

    return


# 调用printinfo 函数
printinfo(10)

printinfo(70, 60, 50)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

def sum_number(*args):

    total = 0
    print(args)

    for num in args:
        total += num

    return total


print(sum_number())    # ()       0


print(sum_number(1,2,3,4,5))     #(1, 2, 3, 4, 5)      15

 

 

 

 

 

 

 

 

 

 

 

 

 

 

def print_person_info(**kwargs):

    print(type(kwargs))
    print(kwargs)
    print("---------------------")
    for key, value in kwargs.items():
        print(f"{key}: {value}")


print_person_info(name="Alice", age=25, country="USA")

# 输出:
# name: Alice
# age: 25
# country: USA

print_person_info(name="Bob", occupation="Engineer")

# 输出:
# name: Bob
# occupation: Engineer

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2024-04-02 17:14  小白龙白龙马  阅读(5)  评论(0编辑  收藏  举报