​​​​Python基础学习--(13)

一、引言

        Python作为一门流行的编程语言,其前景一直是编程界的热门话题。Python简洁的语法、强大的标准库和丰富的第三方库使其在众多领域都有广泛的应用,从网站开发、数据分析、人工智能到科学计算等。随着数据科学和人工智能的兴起,Python的需求持续增长,成为了许多初学者和转行人士的首选语言。 

        根据业界调查,Python的流行度持续位于顶尖位置。技术发展的趋势表明,Python在未来仍然是一个值得投资学习的语言,不仅因为其在当前的技术市场中的需求,还因为它在教育、科研和自动化等领域的扩展性。

二、Python十四案例

1.ASCII码

# Author : zzd
# Date : 2016/3/24 23:10

a = 97
for i in range(0,26):
    print(chr(a+i),'----->',a+i)

 2.enumerate函数

# Author : zzd
# Date : 2016/3/24 23:27
# Desc : enumerate函数,用于获取列表索引和数据

# 1.列表
year = [82,89,88,86,85,00,99]
print('原列表:',year)

for index,value in enumerate(year):
    if str(value) != '0':
        year[index] = int('19'+str(value))
    else:
        year[index] = int('200'+str(value))

print('调整后:',year)

# 2.元组
coffice_name = ('蓝山','卡布奇诺','拿铁','皇家咖啡')
print('您好,欢迎光临')
print('本店经营的咖啡有:')
for index,item in enumerate(coffice_name):
    print(index+1,'.',item,end=' ')

buy = int(input('\n请输入你想购买的品类:'))
print('你购买的[',coffice_name[buy-1],']咖啡已到货!')

3.wordcount

# Author : zzd
# Date : 2016/3/26 14:24
def wordcount(words,word):
    count = 0
    for item in words:
        if word.upper() == item or word.lower() == item:
            count += 1
    return count

if __name__ == '__main__':
    words = 'helloworldhellopythonhelloJava'
    word = 'h'
    count = wordcount(words,word)
    print(count)

4.修改字体输出颜色

# Author : zzd
# Date : 2016/3/23 23:14
# Desc : 控制输出颜色

# 格式:
#   设置颜色开始 :\033[显示方式;前景色;背景色m
# 说明:
# 前景色            背景色           颜色
# ---------------------------------------
# 30                40              黑色
# 31                41              红色
# 32                42              绿色
# 33                43              黃色
# 34                44              蓝色
# 35                45              紫红色
# 36                46              青蓝色
# 37                47              白色
# 显示方式           意义
# -------------------------
# 0                终端默认设置
# 1                高亮显示
# 4                使用下划线
# 5                闪烁
# 7                反白显示
# 8                不可见

print('\033[0:35m\t\tPython切换颜色打印\033[m')

print('\033[0:32m\t\tPython切换颜色打印\033[m')



5.列表倒叙输出

# Author : zzd
# Date : 2016/3/26 12:45
# Desc : 列表倒序输出

card = ['1001','1002','1003','1004']

print('正序输出...')
for i in card:
    print(i)

print('倒叙输出...')
# 从len(card)-1 到 -1依次输出,步长为-1
for i in range(len(card)-1,-1,-1):
    print(card[i])

6. 十进制转二八十六

# Author : zzd
# Date : 2016/3/23 23:48
# Desc : 十进制转二进制、八进制、十六进制

def cale():
    number = int(input('请输入一个十进制数字:'))

    print('你输出的十进制数字为:{}'.format(number))
    print('你输出的二进制数字为:{}'.format(bin(number)))
    print('你输出的八进制数字为:{}'.format(oct(number)))
    print('你输出的十六进制数字为:{}'.format(hex(number)))

if __name__ == '__main__':
    while True:
        try:
            cale()
            break
        except:
            print('你输入有误!!!请重写输入')

 7.向文件输入语句

# Author : zzd
# Date : 2016/3/23 21:15
# Desc : 向文件中输入语句

# 方式一:使用print方式输出文字,目的地是文件
fp = open('E:\\BigData\\date\\PyCharm\\PythonStudy\\action\\test1.txt','w')

print('Good Good Study,Day Day Up',file=fp)

fp.close()

# 方式二:是哦那个with上下文管理器
with open('E:\\BigData\\date\\PyCharm\\PythonStudy\\action\\test2.txt','w') as file:
    file.write('好好学习,天天向上')

8.字符串分割

# Author : zzd
# Date : 2016/3/28 0:42

class Student(object):
    def __init__(self,stu_name,stu_age,stu_gender,stu_score):
        self.stu_name = stu_name
        self.stu_age = stu_age
        self.stu_gender = stu_gender
        self.stu_score = stu_score

    def show(self):
        print(self.stu_name,self.stu_age,self.stu_gender,self.stu_score)

# 列表中存储对象,然后通过对象调用类中函数
if __name__ == '__main__':
    print('请输入五位学生信息,(姓名-年龄-性别-成绩)')
    lst = []
    for i in range(0,2):
        s = input(f'请输入入第{i+1}位学生的信息和成绩:')
        s_lst = s.split('-')
        stu = Student(s_lst[0],int(s_lst[1]),s_lst[2],float(s_lst[3]))
        lst.append(stu)

    for item in lst:
        item.show()

9.异常抛出

# Author : zzd
# Date : 2016/3/24 0:07
# Desc : 验证登录用户名,密码;设置限制支付密码只允许是数字

def login(username,password):
    if username == 'admin' and password == '123456':
        print('登录成功!!')
        return True

if __name__ == '__main__':
    while True:
        username = input('请输入用户名:')
        password = input('请输入密码:')
        if login(username,password):
            pay = input('请输入支付密码:')
            """
            if条件成立,则将开头的字符串输出,否则输出else后的字符
            """
            print('支付密码合法' if pay.isdigit() else '支付密码不合法,只能由数字组成!!')
            break

        else:
            print('账号密码错误,请重新登录!!!')

10.格式化字符串输出

# Author : zzd
# Date : 2016/3/23 23:28
# 格式化字符串输出

height = 170

weight = 50.5

bmi = weight / (height + weight)

# 6种格式化方式
print('身高',height,'的你体重',weight)
print('身高'+str(height)+'的你体重'+str(weight))
print('身高{0}的你体重{1}'.format(height,weight))
print(f'身高{height}的你体重{weight}')
print('身高%s的你体重%s' % (height,weight))

print('你的BMI指标是:' + '{:0.7f}'.format(bmi))

11.格式化时间类型

# Author : zzd
# Date : 2016/3/28 11:00
import time

if __name__ == '__main__':
    print(time.time())
    print(time.localtime(time.time()))
    print(time.strftime('%Y-%m-%d  %H:%M:%S',time.localtime(time.time())))

12.登录支付功能

# Author : zzd
# Date : 2016/3/24 0:07
# Desc : 验证登录用户名,密码;设置限制支付密码只允许是数字

def login(username,password):
    if username == 'admin' and password == '123456':
        print('登录成功!!')
        return True

if __name__ == '__main__':
    while True:
        username = input('请输入用户名:')
        password = input('请输入密码:')
        if login(username,password):
            pay = input('请输入支付密码:')
            """
            if条件成立,则将开头的字符串输出,否则输出else后的字符
            """
            print('支付密码合法' if pay.isdigit() else '支付密码不合法,只能由数字组成!!')
            break

        else:
            print('账号密码错误,请重新登录!!!')

13.遍历两个列表输出

# Author : zzd
# Date : 2016/3/23 21:22
# Desc : 循环遍历两个列表中数据

# 1.列表输出
lst_type = ['id','name','age']
lst_data = ['001','zxy','5']

for i in range(len(lst_type)):
    print(lst_type[i],':',lst_data[i])

# 2.字典输出
dic_data = {'id':'001','name':'zxy','age':'5'}
for i in dic_data:
    # print(i, ':', dic_data[i])
    print(i,':',dic_data.get(i))

# 3.列表转元组输出
lst_type = ['id','name','age']
lst_data = ['001','zxy','5']

for i,j in zip(lst_type,lst_data):
    print(i,':',j)


# 4.列表转字典输出
dict_list = dict(zip(lst_type, lst_data))
for i in dict_list:
    print(i,dict_list[i])

14.列表

# Author : zzd
# Date : 2016/3/26 14:10


phones = set()

for i in range(5):
    info = input(f'请输入第{i+1}个朋友的姓名和手机号:')
    phones.add(info)

for item in phones:
    print(item)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值