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

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

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]

'''

123

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值