#本文章仅用于记录本人学习过程,当作笔记来用,如有侵权请及时告知,谢谢!
学习地址:
https://www.bilibili.com/video/BV19t411m7uU?p=4
使用的环境:VS CODE
P1:
最简单的hello world!
使用print()函数
P2:
name.title()首字母大写
name.upper()全部大写
name.lower()全部小写
以及换行符的使用
message = "hello world"
print(message)
message = "hey world"
print(message)
"""
'''
name = "adhauif dsiufaniu"
print(name.title()) # 首字母大写
print(name.upper()) # 全部大写
print(name.lower()) # 全部小写
'''
'''
first_name = "hu"
last_name = "Steve"
full_name = first_name + " " + last_name
print(full_name)
print("Hello," + " " + full_name.title() + "!")
#print('language:\n\tpython\n\tc\n\tjava')
P3:
#删除空白
'''
favorite_language = "python "
print(favorite_language.rstrip())
favorite_language = " python"
print(favorite_language.lstrip())
P4:
age = 23
message = "Happy " + str(age) +"rd birthday!"
print(message)
'''
#P4
"""
#输出列表
list1 = ['a', 'b', 'c']
print(list1) #['a', 'b', 'c']
#若只想访问元素
print(list1[0]) #a
print(list1[0].title()) #A
#list【-1】
print(list1[-1]) #c
#修改列表名
list1 = ['a', 'b', 'c']
list1[0] = 'A'
print(list1)
#['A', 'b', 'c']
#在列表末尾添加元素
#使用append()函数
list1.append('D')
print(list1)
#['A', 'b', 'c', 'D']
#在列表中插入元素
#使用insert(位置,‘⭐’)函数
list2 = ['1', '2 ', '3']
list2.insert(0,'yuansu')
print(list2)
#['yuansu', '1', '2 ', '3']
P5:
#从列表中删除元素
#del语句
list1 = ['a', 'b', 'c']
print(list1) #['a', 'b', 'c']
del list1[0]
print(list1) #['b', 'c']
#pop()也可以删除列表末尾的元素,还可以将被删除的元素保存至某个变量中
list2 = ['a', 'b', 'c']
print(list2) #['a', 'b', 'c']
mou = list2.pop()
print(list2) #['a', 'b']
print(mou) #c
#pop()很像栈 可以指定pop(数字)弹出任意元素
list3 = ['a', 'b', 'c']
print(list3.pop(1).upper())#B
print(list3) #['a', 'c']
#还可以使用remove()删除某个指定元素
list3.remove('a')
print(list3) #['c']
#方法sort()对列表进行永久排序
list4 = ['b', 'a', 'c', 'e', 'd']
list4.sort()
print(list4) #['a', 'b', 'c', 'd', 'e']
#逆向排序 用reverse = True / 1
list4.sort(reverse = True)
print(list4) #['e', 'd', 'c', 'b', 'a']
list4.sort(reverse = 1)
print(list4) #['e', 'd', 'c', 'b', 'a']
#方法sorted()则对列表进行临时排序
print(sorted(list4)) #['a', 'b', 'c', 'd', 'e']
print(list4) #['e', 'd', 'c', 'b', 'a']
#方法reserve()倒着打印列表 【永久】 想变回去则再使用一次reverse()就好
print(list4) #['e', 'd', 'c', 'b', 'a']
list4.reverse()
print(list4) #['a', 'b', 'c', 'd', 'e']
list4.reverse()
print(list4) #['e', 'd', 'c', 'b', 'a']
#函数len()可以确定列表的长度
print(len(list4)) #5
P6:
#遍历整个列表
alphas = ['a', 'b', 'c']
for zimu in alphas: #把alphas中的元素存到zimu这个变量中
print(zimu)
# a
# b
# c
#创建数值列表
#使用函数range()
for value in range(1,5):
print(value) #不会打印数字5
"""
1
2
3
4
"""
#使用range()创造数字列表
numbers = list(range(1,5))
print(numbers) #[1, 2, 3, 4]
numbers = list(range(1,16,2))
print(numbers) #[[1, 3, 5, 7, 9, 11, 13, 15]]
squares = []
for value in range(1,11):
square = value ** 2
squares.append(square)
print(squares)
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
#对数字列表进行简单的统计计算
digits = [1,2,3,4,5,6,7,8,9]
print(min(digits), max(digits), sum(digits))
#1 9 45
#列表解析
digit = [values ** 2 for values in range(1,5)]
print(digit)
#[1, 4, 9, 16]
P7:
#切片:访问列表的部分元素
players = ['a', 'b', 'c', 'd', 'e']
print(players[0:2]) #['a', 'b']
print(players[:2]) #['a', 'b']
print(players[1:]) #['b', 'c', 'd', 'e']
print(players[-3:]) #['c', 'd', 'e'] 最后三个元素
#复制列表
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = list1[:] #复制列表同时省略起始索引和终止索引,仅复制元素,是存在两个列表的
#list2 = list1 则是赋值,同一个列表
print(list1,list2) #['a', 'b', 'c', 'd', 'e'] ['a', 'b', 'c', 'd', 'e']
#定义元组:元组是不可修改的列表
list3 = (1,2,3)
print(list3[1])
#2
#如果想修改元组,直接重新赋值
list3 = (3,2)
print(list3[1])
#2
#遍历元组中的元素 也用for循环
for digit in list3:
print(digit)
"""
3
2
"""