【Python练习笔记一】

Python练习笔记一

Python支持面向对象的编程范式。Python认为在解决问题的过程中的重点是数据。在Python中,以及在任何其他面向对象的编程语言中,定义一个类来描述数据的外观(状态)和数据能做什么(行为)。因为类的用户只看数据项的状态和行为,所以类类似于抽象的数据类型。数据项在面向对象的范式中称为对象。
对象是类的实例

1. if,else语句用法

def score_1(score):
    if score >= 90:
        print('A')
    elif score >= 80:
        print('B')
    elif score >= 70:
        print('C')
    elif score >= 60:
        print('D')
    else:
        print('F')


if __name__ == '__main__':
    # 添加按键退出功能
    print('提示:按Esc键退出')
    while 1:
        score = int(input('请输入分数: '))
        score_1(score)

2. for循环使用示例

for i in range(5):
    print(i)

print('--------------------------')
for j in range(0, 5):
    print(j)

word_list = ['cat', 'dog', 'rabbit']
letter_list = []
for word in word_list:
    for letter in word:
        letter_list.append(letter)
print(letter_list)

3. 字典

  • 字典是一组关联项,其中每一项由一个键和一个值组成。这个键-值对通常被写成key:value ;
  • 字典在键上没有特定的顺序
  • 可以通过它的键来访问一个值,或者通过添加另一个键-值对来操作字典。取值语法除了使用键值而不是使用项目的索引,看起来很像序列取值,添加新值类似.
  • keys、valuesitems方法都返回包含感兴趣的值的对象。你可以使用list函数把它们转换成列表
a_dict = {
    
    '0': 'person', '1': 'car'}
dict_key = list(a_dict.keys())
dict_value = list(a_dict.values())
key_value = list(a_dict.items())
print(dict_value)
print(dict_key)
print(key_value)
for k in a_dict:
    print(k,)

for k, v in a_dict.items():
    print(k, '-----', v)
for k in a_dict.keys():
    print(k)
print('=======================')
for v in a_dict.values():
    print(v)

## output
['person', 'car']
['0', '1']
[('0', 'person'), ('1', 'car')]
0
1
0 ----- person
1 ----- car
0
1
=======================
person
car

猜你喜欢

转载自blog.csdn.net/zhiqingAI/article/details/126297507
今日推荐