Fork me on GitHub

详细内容:

1.Python介绍

 为什么写代码?百钱买百鸡的例子

100文钱且100只鸡

找程序员开发一个小功能

#问题:5文一只公鸡,3文一只母鸡,1/3文一只小鸡,100文钱买100只鸡,其他中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡买多少刚好凑足100文钱?

'''

使用方程式列出以下内容

5*公鸡数量<100

3*母鸡数量<100

1/3*小鸡数量<100

小鸡数量+母鸡数量+公鸡数量=100

'''

'''
使用python写出解决该问题
'''

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

  字节码:其他

编译型和解释型

  C#/java/C

  Python/PHP

难易程度

  C

  C#/Java

  PHP

  Python

总结:

  1.安装解释器

  2.学习语言规则

  3.编写代码

  4.解释器代码(解释)

 安装解释器

  cPython解释器(*)

  jPython解释器(java)

  ironPython解释器()

  rubyPython解释器(ruby)

  pypy解释器

2.Python解释器版本:cPython解释器

  Python.27

  Python3.6

  环境变量:

    Python配置:

      ;C:\Python36;

      终端:python

    pip配置:

      ;C:\Python\Scripts

      终端:pip3 install xxx

  Python3.6环境变量总体

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

3.编写程序

  创建任意文件.py

  print("Hello world")

 

4.编码

  ascii:用1个字节=8位来表达计算机能表达的东西

  2**8 =256

  假设以下8位bit代表一个字符

  0000 0000 -> A

  0000 0001 -> B

  0000 0010  -> C

  ...

  unicode:万国码,用4个字节=32位来做对应关系

  2**32 ≈ 43亿

  0000 0000  0000 0000  0000 0000  0000 0000 -> A

  0000 0000  0000 0000  0000 0000  0000 0001 -> B

  0000 0000  0000 0000  0000 0000  0000 0010 -> C

  ...

  utf-8:对万国码进行压缩,至少使用1个字节表示

  0000 0000 -> A

  0000 0001 -> B

  0000 0010 -> C

  PS:中文3个字节=24  

  PS:utf-8编码以1个字节表示英文字符,以3个字节表示中文字符

  gbk:对亚洲各国的文字做对应的关系

  PS:中文2个字节=16位

  现象:

    py2:解释器默认编码ascii

      # -*- coding:utf-8 -*- #解释器指定编码utf-8

      python2不指定utf-8编码格式(使用默认编码),则文件中不能出现中文

    py3:解释器默认使用utf-8

      print("尘外孤标")

    py2/py3

    # -*- coding:bgk -*- #不推荐使用bgk编码

    print("古陵逝烟")

5.IDE

  Pycharm

  Vim

  使用:

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

    2.运行

    3.配置

      文字大小

      模板

6.输入输出

  输出

    print("遥望齐州九点烟")

 

  输入

    user = input("请输入你的名称:")

 

  密码加密:

    import getpass

    pwd = getpass.getpass("请输入密码:")
View Code

 

7.变量

  格式:变量名 = 值

  规范:

    a.数字,字母,下划线

    b.不能数字开头

    c.不能使用python内置的关键字

    建议:见名知意; user_pwd = 'xxxx"

  注意:

    示例一:

      name = 'alex'

      user = 'alex'

 

    示例二:

      name = 'alex'

      user = name

 

8.数据类型

  age = 18 #整数类型

  name = "alex" #字符串类型

  xx = "18"

  xx = '18'

  xx = '''18'''

  xx = """18"""

 

9.条件语句

  格式一:

    if 条件:

      成功之后走这里

  格式二:

    if 条件:

      成功之后走这里

    else:

      失败之后走这里

  格式三:

    if 条件:

      成功之后走这里

    elif 条件:

      成功之后走这里

    elif 条件:

      成功之后走这里

    else:

      上述都失败

  练习题:10086提醒

    msg = """

      欢迎致电10086

      1.查询话费

      2.查水表

      3.人工服务

    """

    print(msg)

    choice = input("请选择您需要的服务")

    if choice == "1":

      print("查询话费")

    elif choice == "2":

      print("查水表")

    elif choice == "3":

      print("人工服务")

    else:

      print("输入错误")
View Code

10.循环语句

  while 条件:

    条件成立执行

  while True:

    print("一弘海水杯中泄")

  while 1==1 and 2==2:

    print("黄尘清水上三山下")

  timer = 0

  while timer < 3:

    print("更变千年如走马")

    timer = timer + 1

  print("完成")
View Code

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

  count = 1

  while count < 11:

    print(count)

    count = count + 1
View Code

  break,强制终止循环

while True:
        print("玉轮扎露湿团光")
        break            
View Code

  练习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
View Code

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

  页面上输出1-10,排除7

  方法1:

  count = 1

  while count < 11:

    if count == 7:

      count = count + 1

      continue

    print(count)

    count = count + 1
View Code

  方法2:

  count = 1

  while count < 11:

    if count == 7:

      pass

    else:

      print(count)

    count = count + 1
View Code

  其他练习题

  3.求1-100的所有数的和

  sum = 0

  count = 1

  while count < 101:

    sum = sum + count

    count = count + 1
View Code

  4.输出1-100内所有的奇数

i = 0
while i<101:
    if i%2 == 1: #如果为奇数
        print(i)
        i +=2
    else:        #如果为偶数
        i +=1
View Code

 

  5.输出1-100内所有的偶数

i = 0
while i<101:
    if i%2 == 0: #如果为偶数
        print(i)
        i +=2
    else:        #如果为奇数
        i +=1
View Code

 

  6.求1-2+3-4+5...99的所有数的和

i = 0
sum = 0
while i<100:
    if i%2 == 1: #如果为奇数
        sum += i
    else:        #如果为偶数
        sum -= i
    i +=1
print(sum)
View Code

 

11.常用数据类型

  整数:

    age = 18
View Code

  字符串:

    name = "天踦爵"

    #获取每个字符

    n0 = name[0]  

    n1 = name[1]

    n2 = name[2]
View Code

  列表:

    user_list = ['天踦爵','无梦生','鷇音子','解锋镝']

    for item in user_list:

      print(item)

      if item == "解锋镝":

        break
View Code

  字典:

    user_info = {"name":"天踦爵","age":18}

    n5 = user_info['name']

    n6 = user_info['age']

    user_info['count'] = 666

    #{'name':'天踦爵','age':18,'count':666}
View Code

  数据类型嵌套:

    n7 = ['天踦爵','无梦生','鷇音子',['四能童子','百袖嶙峋']]

    n7[1]

    n7[3][0]

    n8 = [

      "丹华抱一",

      {'name':'鷇音子','age':18},

      ['齐烟九点','尘外孤标','春秋一阕 ']

    ]
View Code

  问题:循环用户列表,打印用户姓名和密码

    user_list = [

      {'user_name':"素还真","password":123,'count':0},

      {'user_name':'叶小钗','password':123,'count':0},

      {'user_name':'一页书','password':123','count':0}

    ]

    for item in user_list:

      print(item['user_name'],item['password'],item['count'])

  将每个用户的count改为1

    user_list = [

      {'user_name':"素还真","password":123,'count':0},

      {'user_name':'叶小钗','password':123,'count':0},

      {'user_name':'一页书','password':123','count':0}

    ]

    for item in user_list:

      item['count'] = 1

      print(item)
View Code

  问题:用户输入用户名和密码,在user_list中进行校验

    user_list = [

      {'user_name':"素还真","password":123,'count':0},

      {'user_name':'叶小钗','password':123,'count':0},

      {'user_name':'一页书','password':123','count':0}

    ]

    user = input("请输入用户名:")
    pwd = input("请输入密码")
    flag = False
    for item in user_list:
      if item['user_name'] == user and item['pwd'] == pwd:
        flag = True
      else:
        pass
      if flag:
        print("登录成功")
      else:
        print("登录失败")
View Code

 

posted on 2018-05-08 13:41  anyux  阅读(140)  评论(0)    收藏  举报