美文网首页
day005 笔记

day005 笔记

作者: Yehao_ | 来源:发表于2018-07-20 18:32 被阅读0次

for循环补充

如果for后面的变量在循环体中不需要,这个变量命名的时候往往可以使用'_'命名。

for _ in range(1, 5):
    print('--------')

Output:
--------
--------
--------
--------

输入输出函数

输入函数input()

语法:

input(promp=None, /)

input()函数用来获取控制台输入的内容。

输出函数print()

语法:

print(*objects, sep=' ', end='\n', file=sys.stdout)

参数:

  • objects -- 复数,表示可以一次输出多个对象。输出多个对象时,需要用 , 分隔。
  • sep -- 用来间隔多个对象,默认值是一个空格。
  • end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符串。
  • file -- 要写入的文件对象。

列表

列表的数据项不需要具有相同的类型。
创建一个列表,只要把逗号分隔的不同的数据项使用方括号[ ]括起来即可。如下所示:

name = ['Apple', 'Google', 'IBM']

添加列表元素:

names = ['Apple', 'Google', 'IBM']
print('最初的names列表:\n', names)
# 在列表末尾添加新的对象
names.append('Facebook')
print('末尾添加元素后的names列表:\n', names)
# 把元素插入到指定的位置,比如索引号为1的位置
names.insert(1, 'Tencent')
print('指定位置插入元素后的names列表:\n', names)

Output:
最初的names列表:
 ['Apple', 'Google', 'IBM']
末尾添加元素后的names列表:
 ['Apple', 'Google', 'IBM', 'Facebook']
指定位置插入元素后的names列表:
 ['Apple', 'Tencent', 'Google', 'IBM', 'Facebook']

练习:从控制台输入10个学生的成绩,然后保存在一个列表中。

scores = []
for _ in range(10):
    score = input('请输入学生的成绩:')
    scores.append(score)
print(scores)

Output:
请输入学生的成绩:65
请输入学生的成绩:65
请输入学生的成绩:64
请输入学生的成绩:56
请输入学生的成绩:79
请输入学生的成绩:98
请输入学生的成绩:88
请输入学生的成绩:65
请输入学生的成绩:43
请输入学生的成绩:35
['65', '65', '64', '56', '79', '98', '88', '65', '43', '35']

删除列表元素:

numbers = [1, 2, '3', 4]
# 方法1:del 语句.删除索引为0的元素
# 注意:这儿的索引不能越界
del numbers[0]
print(numbers)
# 方法2: pop().删除指定索引元素,默认为-1
# 注意: 这儿的索引不能越界
numbers.pop()
print(numbers)
# 方法3 remove().删除列表中指定元素
# 注意:如果要删除的元素不在列表中,会报错
numbers.remove('3')
print(numbers)

Output:
[2, '3', 4]
[2, '3']
[2]

练习:scores = [23, 45, 45, 78,32,90, 89,1],删除所有小于60分的成绩。

scores = [23, 45, 78, 32, 90, 89, 1]
for score in scores[:]:
    if score < 60:
        scores.remove(score)
print(scores)

Output:
[78, 90, 89]

列表截取与拼接

截取:

fruits = ['Apple', 'Banana', 'Cherry', 'Durian', 'Elderberry', 'Feijoa']
print(fruits[1:5])

Output:
['Banana', 'Cherry', 'Durian', 'Elderberry']

拼接:

languages = ['Java', 'PHP', 'Python']
names = ['Bob', 'Mike']
new_list = languages + names
print(new_list)

Output:
['Java', 'PHP', 'Python', 'Bob', 'Mike']

相关文章

  • day005 笔记

    for循环补充 如果for后面的变量在循环体中不需要,这个变量命名的时候往往可以使用'_'命名。 输入输出函数 输...

  • BM运营官-【心得02】Day005- by Bella

    【必修作业】Day005 by Bella 题目内容:输出第二次课的学习心得 课程过程中,我做了结构化的幕布笔记,...

  • 每日复盘Day005

    Day005 每日复盘(至少每日三件事) 1.辣妈直升机定位课(5)+笔记✔ 2.冥想10分钟✔ 3.读书+录音2...

  • Day005

    a

  • DAY005

    I had a nightmare in this early morning.I dreamt that I h...

  • DAY005

    Today is very cold, the highest temperature is only 10 ce...

  • Day005

    I want to be rich,so I should not lose any money.

  • Day005

    昨晚给男神分享了白羊,结果他分享一首纯音乐给我,瞬间感觉逼格好高啊,哈哈哈哈,不愧是我喜欢的人,真的是优秀啊!!!...

  • 2018-03-27 字体样式

    Day005,40min,共计90min 字体样式属性:font-family, font-size, font-...

  • day005 作业

    1.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+2222...

网友评论

      本文标题:day005 笔记

      本文链接:https://www.haomeiwen.com/subject/kxdfmftx.html