初识Python (变量、数据类型及取值、简单的for、if、while循环)

一、编程语言
-编程语言分为C语言
-其他语言(Java、PHP、C#、python)
(因为所有的语言解释器都是用C语言编写的)
难易程度
-C
-C#、java
-PHP
-python(类、库、模块齐全。相对来说简单,只是相对,所以还是挺难的。)
 
二、python解释器
-cpython(常用的python解释器)
-jpythong
-rubypython
-ipython
-pypy
python版本
-python2以及python3
(py2计划支持到2020年,所以2020年以前,要尽量把py2,转换成py3)
三、编写程序
创建任意以.py结尾的文件
例:
你好世界.py
内容:print("hello world")
四、编码
ASCII码
用一个字节 1B(1byte)=8位 8(bit)来表示计算机能表达的所有东西
2**8 = 256   (最小值为0,最大值为255)
例:
01100001    解释为    a
01000001    解释为  A
Unicode:万国码用4个字节(4byte) = 32 来做对应关系,为了解决传统的字符编码方案的局限而产生。但是,占用存储较多!
2**32 = 4294967296
例:
00000000 00000000 00000000 00000000 解释为 A
   00000000 00000000 00000000 00000001   解释为 B
utf-8:对万国码进行压缩,至少使用一个字节表示。*中文(简体|繁体)占三个字节。
例:
01100001    解释为    a
01000001    解释为  A
gbk:汉字编码字符集(中华人民共和国全国信息技术标准化技术委员会95年监制)
汉字占2字节(2byte) = 16位来表示
现象:
python2:默认解释器为ASCII码
需在开头声明编码格式
# -*- coding:utf-8 -*-     :更改默认解释编码为utf-8
print('你是猴子请来的逗比么')
 
python3:默认解释器编码为uft-8
可以省略,开头声明编码格式
print('你是猴子请来的逗比么')
当然,也可以声明
py2/py3
   # -*- coding:gbk -*-
   print('你是猴子请来的逗比么')
五、 IDE
- pycharm
- vim 
六、输入输出
输入:
name = input('lisi')
输出:
print('小猪佩奇身上纹,一看就是社会人')
密码加密:
import getpass
pwd = getpass.getpass('请输入密码')
七、变量
格式:
变量名 = 值
规范:
-以字母,下划线,数字命名
-不能以数字开头
-用下划线分隔
-不能使用python内置的关键字
建议:见名识意
注意:
示例1:
name = 'zhangsan'
user = 'zhangsan'
示例2:
name = 'zhangsan'
user = name
ps:user = name 若name重新赋值,则user值不变
八、数据类型
-整型(int)
age = 18
-字符串(str)
user = 'lisi'
-列表(list)
name_list = ['zhang_san','li_si','wang_er_ma_zi']
字典(dict)
info_dict = {'name':'zhang_san','age':'20','height':'160'}
ps:单引号'',双引号"",三引号'''  ''',"""   """在语句里用,没有区别
三引号'''  '''  , """   """可以换行。
九、取值
字符串(str):
name = '紫薇'
#获取紫
n1 = name[0]
#获取微
n2 = name[1]
print(n1,n2)
列表(list):
user_list = ['zhang_san','li_si','wang_wu','zhao_liu']
#获取zhang_san
n3 = user_list[0]
#获取wang_wu
n4 = user_list[2]
print(n3,n4)
user_list = ['紫薇','li_si','尔康','zhao_liu']
#获取 紫薇
n3 = user_list[0]
#获取 尔康
n4 = user_list[2]
#获取  紫和康
print(n3[0],n4[1])
字典(dict):
user_info = {'name':'紫薇','age':18,'ti_zhong':'100kg'}
#获取紫薇
n5 = user_info['name']
#获取体重
n6 = user_info['ti_zhong']
print(n5,n6)
#字典里增加key和值
user_info['shen_gao'] = '160cm'
数据类型嵌套   PS:不用引号
列表嵌套列表
n7 = ['zhang_san','li_si',['wang_wu','zhao_liu']]
#获取li_si
n8 = n7[1]
#获取zhao_liu
n9 = n7[2][1]
print(n8,n9)
列表嵌套字典
n10 = ['zhang_san','li_si',{'name':'wang_wu','age':'25'}]
#获取wang_wu
n11 = n10[2]['name']
print(n11)
十、break和continue
break:强制终止本次循环
continue:终止本次循环,进入下一个循环
十一、if条件语句     PS:条件后面加冒号:   
-if 条件:
成功之后走这里
-if 条件:
成功之后走这里
else:
失败之后走这里
-if 条件:
成功之后走这里
elif 条件:
成功之后走这里
elif 条件:
成功之后走这里
else:
失败之后走这里
练习题:10068提醒
msg = ('''您好,欢迎致电10086
1、查询话费。
2、查询水表。
3、妹子服务
''')
print(msg)
user = input('请选择需要的服务:')
if user == '1':
chose = ('''
1.查询本机,2.查询他人手机,3.臭不要脸''')
print('查询话费')
print(chose)
user_chose = input('选择查询项目:')
if user_chose == '1':
print('查询本机')
elif user_chose == '2':
print('查询他人手机')
elif user_chose == '3':
print('臭不要脸')
else:
print('你是sb么')
elif user == '2':
print('查询水表')
elif user == '3':
print('妹子服务')
else:
print('智障,选择错误')
十二、while循环
while 条件:
条件成立执行
while True:
print('黑化肥发灰会挥发')
while 1==1 adn 2==2     #条件也成立
timer = 0
while 0 < 3
print('黑化肥发灰会挥发')
timer = timer + 1
练习题:
-页面上输出 1 - 10
(自己)
count = 1
while True:
print(count)
count = count + 1
if count > 10:
break
 
(老师)
count = 1
while count < 11
pring(count)
count = chount + 1
while True:
print(count)
if count == 10:
break
count = count + 1
练习题:
-页面上输出 1 - 10,排除7
(自己)
count = 1
while count < 10:
count = count + 1
if count == 7:
continue
print(count)
(老师)
count = 1
while count < 11:
if count == 7:
pass
else:
print(count)
count = count + 1
十三、for循环
for xxx in 变量名
要执行的操作
例:
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 xx in user_list:
if xx['count'] == 0:
xx['count'] = 1
print(xx['count'])
练习:
用户输入用户名和密码,在user_list中进行校验
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},
]
name = input('请输入用户名:')
pwd = input('请输入密码:')
for xx in user_list:
if name == xx['username'] and pwd == xx['password']:
print('欢迎登陆')
扩展:
******三次登陆失败,禁止登陆*****
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},
]
name = 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("登录失败")
posted @ 2018-05-06 16:32  蹲坑  阅读(597)  评论(0编辑  收藏  举报