python学习day1

 


今日内容:
1. Python介绍

2. Python版本

3. Hello World

4. 编码

5. 输入输出

6. 变量

7. 初始数据类型

8. 条件语句

9. 循环语句

10. 经常使用数据类型


内容详细:
1. Python介绍

- 为何写代码?百钱买百鸡
100文且100只
5文-g
3文-m
1/3文-x

找程序员开发一个软件
# 问题:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买100只鸡,其中公鸡,母鸡,小鸡都必需要有,问公鸡,母鸡,小鸡要买多少只恰好凑足100文钱?
"""
5*公鸡 < 100
3*母鸡 < 100
1*小鸡 < 300
公鸡+母鸡+小鸡 = 1000
"""
for g in range(1, 21):
for m in range(1, 34):
for x in range(1, 301):
score = g * 5 + m * 3 + x / 3
if score == 100 and g + m + x == 100:
print('公鸡 %s 只,母鸡 %s 只,小鸡 %s 只' % (g, m, x))

目的解决生活中的问题。

- 编译器解释器
安装Python解释器:+ 编写解释器认识的代码

安装Java解释器:+ 编写解释器认识的代码

安装PHP解释器:+ 编写解释器认识的代码

- 编程语言?
C
Python
Java
PHP
C#

- c语言和其余
- 机器码:C
- 字节码: 其余

- 编译型和解释性
- Python、PHP
- C#/Java/C

- 难易程度
- C
- C#、Java
- PHP
- Python(类库齐全/模块)

总结:
1. 安装解释器
2. 学习语言规则
3. 编写代码
4. 解释器运行(解释)

- 安装解释器
- cpython解释器(*)
- jpython解释器
- ironPython解释器
- rubyPyhton解释器
.....
- pypy解释器

2. Python解释器版本:cpython解释器
- Python2.7
- Python3.6

环境变量:
Python配置:
;C:\Python36;
终端:python
pip配置:
;C:\Python36\Scripts
终端:pip3 install xxxx

;C:\Python36;C:\Python36\Scripts;python


3. 编写程序
建立任意文件.py
print('hello world')

4. 编码
ascii:用1个字节=8位来表示计算机能表达的全部东西。
2**8 = 256
00000000 -> A
00000001 -> B
00000010 -> C
00000011
00000100
unicode: 万国码,用4个字节=32位来作对应关系
2**32 = 4294967296
00000000 00000000 00000000 00000000 -> A
00000000 00000000 00000000 00000001 -> B
00000000 00000000 00000000 00000010 -> C
00000000 10000000 00010000 00011010 -> 紫
utf-8: 对万国码进行压缩,至少使用1个字节表示
00000000
00000001
00000010
10000000 00010000 00011010
PS: 中文3个字节=24位

gbk:对亚洲国家的文字作的对应关系
PS: 中文2个字节=16位


现象:
py2: 解释器默认编码ascii
# -*- coding:utf-8 -*- 解释器默认编码utf-8
print('王紫薇')

py3:解释器默认编码utf-8
print('要睡觉')


py2/py3
# -*- coding:gbk -*-
print('要睡觉')

5. IDE
- pycharm
- vim

使用:
1. 启动Pycharm:选择已存在的解释器

2. 运行

3. 配置
- 文字大小
- 模板程序员

6. 输入输出
输出:
print("你是风儿我是沙")
输入:
user = input("请输入用户名:")

密码加密:
import getpass

pwd = getpass.getpass("请输入密码:")
7. 变量

格式: 变量名 = 值
规范:
a. 数字、字母、下划线
b. 不能以数字开头
c. 不能使用Python的关键字
建议:见名知意; user_pwd = "xxx"


注意:
示例一:
name = 'alex'
user = 'alex'

示例二:
name = 'alex'
user = name

8. 数据类型

age = 18 # 整数类型
name = "alex" # 字符串类型

xx = "18"
xx = '18'
xx = '''18'''
xx = """18"""
xx = "123'
9. 条件语句

格式一:
if 条件:
成功以后走这里

格式二:
if 条件:
成功以后走这里
else:
失败以后走这里编程

格式三:
if 条件:
成功以后走这里
elif 条件:
成功以后走这里
elif 条件:
成功以后走这里
else:
上述都失败

练习题: 10086提醒
msg = """
欢迎致电10086
1. 查询话费
2. 查水表
3. 人工服务
"""

print(msg)

choice = input('请选择您须要服务')
if choice == "1":
print("1.查询本机;2.查询他人手机;3.查询座机")
search_type = input('请输入要查询的类型:')
if search_type == '1':
print('查询本机')
elif search_type == '2':
print('查询他人手机')
elif search_type == '3':
print('查询座机')
else:
print('查询类型输入错误')
elif choice == "2":
print("查水表")
elif choice == "3":
print("人工服务")
else:
print("输入错误")vim

问题青年:
错误:
print('欢迎登录')
user = input('请输入用户:')
纠正:
print('欢迎登录')
user = input('请输入用户:')

姑娘:
错误:
msg = """
欢迎致电10086
1.查询话费
2.查水表
3.人工服务ruby

"""
print(msg)编程语言

choice = input("请选择您须要的服务")
if choice == "1":
print("1.查询本机;2.查询他人手机;3.查询座机")
search_type = input("请输入要查询的类型:")
if search_type =='1':
print('查询本机')
elif search_type == input('2'):
print('查询他人手机')
elif search_type == input('3'):
print('查询座机')
else:
print('查询类型输入错误')
elif choice == input("2"):
print("查水表")
elif choice == input("3"):
print("人工服务")
else:
print("输入错误")

纠正: 学习

msg = """
欢迎致电10086
1.查询话费
2.查水表
3.人工服务编码

"""
print(msg)加密

choice = input("请选择您须要的服务")
if choice == "1":
print("1.查询本机;2.查询他人手机;3.查询座机")
search_type = input("请输入要查询的类型:")
if search_type == '1':
print('查询本机')
elif search_type == '2':
print('查询他人手机')
elif search_type == '3':
print('查询座机')
else:
print('查询类型输入错误')
elif choice == "2":
print("查水表")
elif choice == "3":
print("人工服务")
else:
print("输入错误")code


10. 循环语句

while 条件:
条件成立执行


while True:
print('钓鱼要钓刀鱼,刀鱼要到岛上钓')


while 1==1 and 2==2:
print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

timer = 0
while timer < 3:
print('钓鱼要钓刀鱼,刀鱼要到岛上钓')
timer = timer + 1

print('完成')


练习1: 页面上输出 1 - 10

count = 1
while count < 11:
print(count)
count = count + 1



- break,强制终止当前所在循环
while True:
print('钓鱼要钓刀鱼,刀鱼要到岛上钓')
break

练习2: 页面上输出 1 - 10 (使用break)
count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True:
print(count)
if count == 10:
break
count = count + 1

- continue,跳出本次循环,继续下一次循环。



练习题
1. 页面上输出 1 - 10

count = 1
while count < 11:
print(count)
count = count + 1

count = 1
while True:
print(count)
count = count + 1
if count == 11:
break

count = 1
while True:
print(count)
if count == 10:
break
count = count + 1
2. 页面上输出 1 - 10,排除7
count = 1
while count < 11:
if count == 7:
count = count + 1
continue
print(count)
count = count + 1


count = 1
while count < 11:
if count == 7:
pass
else:
print(count)
count = count + 1

其余练习题:
三、求1-100的全部数的和

sum = 0

count = 1
while count < 101:
sum = sum + count
count = count + 1

四、输出 1-100 内的全部奇数
n=3
div = n%2

五、输出 1-100 内的全部偶数

六、求1-2+3-4+5 ... 99的全部数的和




11. 经常使用数据类型

整数:
age = 18
字符串:
name = "紫薇"
# 获取紫
n1 = name[0]
n2 = name[1]
列表:
user_list = ["紫薇","尔康","18","海量","小鸡"]
n3 = user_list[0]
n4 = user_list[1] # "尔康"

user_list = ["紫薇","尔康","18","海量","小鸡"]

for xxx in user_list: print(xxx) if xxx == '18': break 字典: user_info = {"name":"紫薇","age":18} n5 = user_info["name"] n6 = user_info["age"] user_info['count'] = 666 # {"name":"紫薇","age":18,"count":666} 数据类型嵌套: n7 = ["alex","eric",[11,22,33]] n7[1] n7[2][1] n8 = [ "alex", {'name':'日天','age':18}, [11,22,33] ] n8[1]["age"] = 19 问题: 循环用户列表,打印用户姓名和密码 user_list = [ {'username':'alex', 'password':'123', 'count':0 }, {'username':'eric', 'password':'123', 'count':0 }, {'username':'tony', 'password':'123', 'count':0 }, {'username':'oldboy', 'password':'123', 'count':0 }, ] for item in user_list: print(item['username'],item['password'],item['count']) 问题: 将每一个用户的count改为1 user_list = [ {'username':'alex', 'password':'123', 'count':0 }, {'username':'eric', 'password':'123', 'count':0 }, {'username':'tony', 'password':'123', 'count':0 }, {'username':'oldboy', 'password':'123', 'count':0 }, ] for item in user_list: if item['username'] == 'alex' and item['password'] == '123': item['count'] = 1 print(item) 问题: 用户输入用户名和密码,在user_list中进行校验 user_list = [ {'username':'alex', 'password':'123' }, {'username':'eric', 'password':'123'}, {'username':'tony', 'password':'123'}, {'username':'oldboy', 'password':'123'}, ] user = input("请输入用户名:") pwd = input("请输入密码:") flag = False for item in user_list: if item['username'] == user and item['password'] == pwd: flag = True break else: pass if flag: print("登陆成功") else: print("登陆失败") 内容梳理: 1. 编程语言 - c和其余 - 解释性和编译型 - 难以程度 2. 解释器 - cpython 3. 语法规则 - 头 - #!/usr/bin/env python - # -*- coding:utf-8 -*- (py2中若是代码中有中文,必定要找个头) - 编码 - ascii - unicode - utf-8 - gbk PS: 一个字节 = 8位 00000000 - 输入输出 - input - row_input (py2) - print - 变量 - 规范: - 字母、数字、下划线 - 数字不能开头 - 不能是内置关键字 建议:见名知意 - 条件语句 if 条件: pass else: pass if 条件: pass elif 条件: pass - 循环语句: while 条件: pass 关键字: break continue while True: print(1) while True: print(2) break 注意:缩进 - 数据类型 - 整型 age = 19 - 字符串 name = "xxxx" - 列表 names = ["xx","xx","xx"] - 字典 info = { "name":'alex', # 键值对 "age":'18' }

相关文章
相关标签/搜索