做者:赵俊 发布日期:2019/10/18python
1、第一个python程序app
一、在解释器下写hello world程序运行,与运行外部文件方法oop
运行外部文件,必须在相应位置建立一个python文件,里面写上语句优化
二、#!/usr/bin/evn python的做用,告诉操做系统使用的解释器是什么编码
#!/usr/bin/python至关于写死了python路径;
#!/usr/bin/env python会去环境设置寻找python目录,推荐这种写法spa
2、变量操作系统
一、pycharm工程新建文件,模板代码设置命令行
二、变量的内存管理3d
三、变量定义的规则code
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
3、字符编码的区别与介绍
一、python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill)
ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其余西欧语言,其最多只能用 8 位来表示(一个字节),即:28 = 256,因此,ASCII码最多只能表示 255 个符号。
二、关于中文编码
简体中文的GB2312和用于繁体中文的big5
从ASCII、GB23十二、GBK 到GB18030,这些编码方法是向下兼容的
三、国际编码
Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码
UTF-8,是对Unicode编码的压缩和优化,他再也不使用最少使用2个字节,而是将全部的字符和符号进行分类:ascii码中的内容用1个字节保存、欧洲的字符用2个字节保存,东亚的字符用3个字节保存
四、# -*- coding: utf-8 -*-
python 2.x上写中文要告诉解释器编码格式
python 3.x支持unicode编码
4、用户输入及格式化输出
一、注释
单行注释:#号后面跟要注释的内容
多行注释:'''被注释内容''' 三个单引号或双引号均可以
多行注释能够用来打印多行,以下图,左侧为源码,右侧为输出
二、用户输入
1 username = input("请输入用户名") 2 password = input("请输入密码") 3 print(username,password)
三、格式化输出
用+号拼接字符串,不建议使用,占用内存多
1 name = input("请输入用户名") 2 age = input("请输入年龄") 3 info = ''' 4 ----------info of '''+name+'''---------- 5 name:'''+name+''' 6 age:'''+age 7 print(info)
用%s或%d或f%,%s是接收字符串的,%d是接收数值的,%d是接收浮点。input输入的均为字符串,全部在用%d时,必须用int()强制类型装换为整型
1 name = input("请输入用户名") 2 age = input("请输入年龄") 3 info = ''' 4 ----------info of %s---------- 5 name:%s 6 age:%s 7 '''%(name,name,age) 8 print(info)
使用format格式化输出
1 name = input("请输入用户名") 2 age = input("请输入年龄") 3 info = ''' 4 ----------info of {_name}---------- 5 name:{_name} 6 age:{_age} 7 '''.format(_name=name,_age= age) 8 print(info)
使用format格式化输出另外一种
1 name = input("请输入用户名") 2 age = input("请输入年龄") 3 info = ''' 4 ----------info of {0}---------- 5 name:{0} 6 age:{1} 7 '''.format(name,age) 8 print(info)
5、if else流程判断
一、密文输入
导入标准库import getpass。这个在pychar中没效果,用命令行就能够实现
1 import getpass 2 name = input("请输入用户名") 3 age = getpass.getpass("请输入年龄") 4 info = ''' 5 ----------info of {0}---------- 6 name:{0} 7 age:{1} 8 '''.format(name,age) 9 print(info)
二、if语句基本结构
1 _username = "zhaojun" 2 _password = "123" 3 username = input("请输入用户名:") 4 password = input("请输入密码:") 5 6 if _username == username and _password == password: 7 print("welcome {name} login...".format(name = _username)) 8 else: 9 print("invalid username or password")
三、if elif语句 猜数字
1 _number = 33 2 number = int(input("猜数字:")) 3 4 if number == _number: 5 print("恭喜你,猜对了!") 6 elif number > _number: 7 print("猜大了!") 8 else: 9 print("猜小了!")
6、while循环
一、while循环,改进循环猜数字3次
1 _number = 33 2 count = 0 3 while count<3: 4 number = int(input("猜数字:")) 5 if number == _number: 6 print("恭喜你,猜对了!") 7 break 8 elif number > _number: 9 print("猜大了!") 10 else: 11 print("猜小了!") 12 count +=1
二、while else。改进三次后提示猜次数过多
1 _number = 33 2 count = 0 3 while count<3: 4 number = int(input("猜数字:")) 5 if number == _number: 6 print("恭喜你,猜对了!") 7 break 8 elif number > _number: 9 print("猜大了!") 10 else: 11 print("猜小了!") 12 count +=1 13 else: 14 print("已经猜了三次了,再见!")
三、猜数字任性玩改进
1 _number = 33 2 count = 0 3 while count<3: 4 number = int(input("猜数字:")) 5 if number == _number: 6 print("恭喜你,猜对了!") 7 break 8 elif number > _number: 9 print("猜大了!") 10 else: 11 print("猜小了!") 12 count +=1 13 if count == 3: 14 confirm = input("你是否继续猜数字?按回车从新开始,按n为退出") 15 if confirm != "n": 16 count = 0
7、for循环
一、基本语法
1 for i in range(10): 2 print("loop",i)
二、用for循环改进猜数字
1 _number = 33 2 for i in range(3): 3 number = int(input("猜数字:")) 4 if number == _number: 5 print("恭喜你,猜对了!") 6 break 7 elif number > _number: 8 print("猜大了!") 9 else: 10 print("猜小了!") 11 else: 12 print("已经猜了三次了,再见!")
三、range里面的参数分别为,最小值,最大值,步长
1 for i in range(0,10,1): 2 print("loop",i)
8、课后做业
一、做业二:编写登录接口
在代码目录创建三个文本文件以下(最后一个字符串要带换行,由于前几行都有换行,为了比较时比如较,也就给最后一个加换行):
程序流程图:
代码:
1 # 是否锁定标志 2 is_locked = 0 3 # 登陆三次计数 4 count = 0 5 #用户名是否存在标志 6 user_exist = 0 7 num = 0 8 while count<3: 9 print("----------请登陆----------") 10 username = input("请输入用户名:") 11 password = input("请输入密码:") 12 count+=1 13 locked_file = open("locked.txt", "r") 14 locked_list = locked_file.readlines() 15 for i in range(len(locked_list)): 16 if username + "\n" == locked_list[i]: 17 is_locked = 1 18 break 19 if is_locked == 0: 20 locked_file.close() 21 user_file = open("username.txt", "r") 22 user_list = user_file.readlines() 23 for i in range(len(user_list)): 24 if username+"\n" == user_list[i]: 25 user_exist = 1 26 num = i 27 break 28 if user_exist == 1: 29 user_file.close() 30 pass_file = open("password.txt", "r") 31 pass_list = pass_file.readlines() 32 if password + "\n" == pass_list[num]: 33 pass_file.close() 34 print("恭喜,登陆成功!") 35 break 36 else: 37 pass_file.close() 38 print("密码错误!") 39 continue 40 else: 41 user_file.close() 42 print("用户名不存在,请重新输入!") 43 else: 44 locked_file.close() 45 print("用户被锁定,请联系管理员解锁!") 46 else: 47 locked_file = open("locked.txt", "a") 48 locked_file.write(username+"\n") 49 locked_file.close() 50 print("输入超过三次,用户被锁定!")
程序流程图:
代码:
1 #Author:ZHJ 2 phone_model_huawei = { 3 "Mate30系列":"" 4 ,"nova 5 Pro系列":"" 5 ,"畅享9系列":"" 6 } 7 phone_model_apple = { 8 "iPhone 11系列":"" 9 ,"iPhone X系列":"" 10 ,"iPhone 8系列":"" 11 } 12 phone_model_samsung = { 13 "GALAXY Note 10系列":"" 14 ,"GALAXY Note 9系列":"" 15 ,"GALAXY S8系列":"" 16 } 17 18 computer_modle_lenove = { 19 "启天M415系列":"" 20 ,"扬天T4900d系列":"" 21 ,"擎天T510A系列":"" 22 } 23 computer_modle_dell = { 24 "成铭 3980MT系列":"" 25 ,"Inspiron 灵越 3670系列":"" 26 ,"Vostro 成就 3670系列":"" 27 } 28 computer_modle_hp = { 29 "光影精灵系列":"", 30 "暗影精灵5系列":"", 31 "战99 Pro G1 MT系列":"" 32 } 33 34 phone_brand = { 35 "华为":phone_model_huawei, 36 "苹果":phone_model_apple, 37 "三星":phone_model_samsung 38 } 39 computer_brand = { 40 "联想":computer_modle_lenove, 41 "戴尔":computer_modle_dell, 42 "惠普":computer_modle_hp 43 } 44 45 commodity = { 46 "手机":phone_brand, 47 "电脑":computer_brand 48 } 49 50 num = 1 51 input_commodity = "" 52 input_brand = "" 53 input_modle = "" 54 while True: 55 if num == 1: 56 print("----------请输入要查询的商品----------") 57 print(list(commodity.keys())) 58 input_commodity = input("请选择一个商品:") 59 if input_commodity in commodity: 60 num += 1 61 elif input_commodity =="q": 62 break 63 elif input_commodity =="b": 64 continue 65 else: 66 print("查询商品不存在,请从新输入!") 67 continue 68 elif num == 2: 69 print("----------请输入要查询的品牌----------") 70 print(list(commodity.get(input_commodity).keys())) 71 input_brand = input("请选择一个品牌:") 72 if input_brand in commodity.get(input_commodity): 73 num += 1 74 elif input_brand =="q": 75 break 76 elif input_brand =="b": 77 num-=1 78 continue 79 else: 80 print("查询品牌不存在,请从新输入!") 81 continue 82 elif num == 3: 83 print("----------请输入要查询的型号----------") 84 print(list(commodity.get(input_commodity).get(input_brand).keys())) 85 input_modle = input("请选择一个型号:") 86 if input_modle in commodity.get(input_commodity).get(input_brand): 87 print("----------**********----------") 88 print("谢谢使用,查询完成!") 89 print("----------**********----------") 90 break 91 elif input_modle =="q": 92 break 93 elif input_modle =="b": 94 num-=1 95 continue 96 else: 97 print("查询品牌不存在,请从新输入!") 98 continue