python3基础速通

python3基础速通

while&for循环嵌套

  1. while循环嵌套
n = 1
while n <= 9:
	m = 1
	while m <= n:
		print(f'{n}*{m}={n*m}',end=" ")
		m = m + 1
	n = n + 1
	print()

  1. for循环嵌套
for i in range(1,10):
	for j in range(1,i+1):
		pring(f'{i}*{j}={i*j}',end=" ") #以空格作为结尾
	print()

格式化输出: format()

user_1 = "lion"
user_2 = "anda"
print('{}对{}说:"Hello!"'.format(user_1,user_2))
print(f'{user_1}对{user_2}说:"Hello!"') #更优方法

+号可以连接多个字符串

print('are'+'you'+'ok')

列表[]

  1. 列表增加元素: append(), insert(), extend()
  2. 列表删除元素: pop(),remove()
  3. 列表的修改
my_list=[1,2,'a',1,3]
my_list.append('python')
my_list.insert(1,'python')
my_list.extend([22,33,44])
my_list.pop(1) #删掉一号位置的元素
my_list.remove('a') #适用于元素位置未知

元组()(不可修改的列表)

my_list=(1,2,'a',1,3)

字典{}

键值对组成

user = {
	'name':'Tom',
	'age':18,
	'gender':'male'
	}
user['fav']='打篮球' #正确向字典中添加新的键值对
user['age']=28 #正确修改字典中的键值对
}

函数def

目的:

  • 降低编程难度
  • 增加代码复用
def sum(n.m) #求1-100的加和
	s = 0
	while n <= m:
		s += n
		n += 1
	return s

文件操作

  1. open()函数打开文件
  2. write()文件写入
f = open('write_text.txt',mode='w',encoding='utf-8')#mode w表示写入模式
f = open('write_text.txt',mode='a',encoding='utf-8')#mode a表示追加模式
f.write("A\n")
f.write("B\n")

jieba分词 &wordcloud

import jieba
from wordcloud
s=jieba.lcut('能够将一段中文文本分隔成单个词汇')
cut_list = jieba.lcut(s)
new_str = ' '.join(cut_list)
word_cloud = WordCloud(font_path='msyh.ttc').generate(new_str) #生成词云对象,前font_path指向微软雅黑字体
word_cloud.to_file('text.png') #保存图片

类和对象: 面向对象编程

class Person:
    def __init__(self,name,sex,birthday):
        self.name = name
        self.sex = sex
        self.birthday = birthday
    def say(self,word):
        print(f'{self.name}说:"{word}"')
张三 = Person('张三','male','1996.1.17')
李四 = Person('李四','male','1839.1.20')
张三.say('世界你好!')
李四.say('俺也一样!')

学生管理系统demo

"""
欢迎使用[学生成绩管理系统]
    1.显示所有学生信息
    2.新建学生信息
    3.查询学生信息
    4.修改学生信息
    5.删除学生信息
    0.退出系统
"""
#使用列表模拟学生数据库
data =[
    {
    'id':12441224,
    'name':'张三',
    'sex':'男',
    'address':'佛山'
    },
    {
    'id':67791224,
    'name':'李四',
    'sex':'男',
    'address':'济南'
    },
    {
    'id':15467124,
    'name':'王五',
    'sex':'女',
    'address':'桂林'
    },
]


#美化显示:
def beauty_print(data_list):
    for index,stu in enumerate(data_list): # 获得序号和值
        print(f'序号:{index}',end="\t")
        print(f'姓名:{stu["name"]}',end="\t")
        print(f'id:{stu["id"]}',end="\t")
        print(f'性别:{stu["sex"]}',end="\t")
        print(f'地址:{stu["address"]}')


# 1.显示所有学生信息
def show_all():
    beauty_print(data)


# 2.新建学生信息
def create_stu():
    name = input_name()
    sex = sex_choice()
    address = input('输入地址: ')
    stu = {
        'name':name,
        'sex':sex,
        'address': address
    }
    data.append(stu)


# 2*.新建学生信息优化(输入名字,性别限制)
def input_name():
    while True:
        name=input('输入名字: ').strip()
        if name:
            return name
        else: continue
            
            
def sex_choice():
    while True:
        print('1(男)|2(女)')
        n = input('选择性别: ')
        if n=='1':
            return '男'
        elif n=='2':
            return '女'
        else: continue
            

# 3.查询学生信息
def find_stu():
    name = input('查询学生的名字: ')
    for stu in data:
        if stu['name'] == name:
            print(stu)
            return
    else:
        print('查无此人')


# 4.修改学生信息
def modify_stu():
    name = input('查询学生的名字: ')
    for stu in data:
        if stu['name'] == name:
            stu['id']=input('请输入学生id: ')
            stu['sex'] = input('请输入学生性别: ')
            stu['address'] = input('请输入学生地址: ')
            return
    else:
        tem = input('查无此人,是否要新建[y/n]?')
        if tem == 'y':
            create_stu()
        else:
            return


# 5.删除学生信息
def delete_stu():
    name = input('查询学生的名字: ')
    for stu in data:
        if stu['name'] == name:
            data.remove(stu)


while True:
    print("""***********************
            欢迎使用[学生成绩管理系统]
                1.显示所有学生信息
                2.新建学生信息
                3.查询学生信息
                4.修改学生信息
                5.删除学生信息
                0.退出系统
            ***********************""")
    operation = input('请输入序号: ')
    if operation == '1':
        show_all()
    elif operation == '2':
        create_stu()
    elif operation == '3':
        find_stu()
    elif operation == '4':
        modify_stu()
    elif operation == '5':
        delete_stu()
    elif operation == '0':
        print('退出系统')
        break
posted @ 2021-02-01 23:29  拼搏向上的菜鸟  阅读(108)  评论(0)    收藏  举报