zoukankan      html  css  js  c++  java
  • PYTHON笔记:第二章.Python入门

    • 2.1 环境的安装

      解释器:py2 / py3 (环境变量)

      解释器工具 python 2.7.16,python3.6.8

      开发工具:pycharm

    • 第一个PYTHON 的脚本

      打开电脑终端, 功能键+R

      CMD

      输入命令: 解释器路径 脚本路径(建议 .py 后缀)
      # print('你好')

    • 2.2 编码

    2.2.1 编码基础
    • ascii

    • unicode

      • utf-8
    • gbk

      • gb2312
    2.2.2 python编码相关

    对于Python默认解释器编码:

    • py2: ascii

    • py3: utf-8

    如果想要修改默认编码,则可以使用:
    # -- coding:utf-8 --
    注意:对于操作文件时,要按照:以什么编写写入,就要用什么编码去打开。

    2.2.3 文件编码

    建议:编写文件时,保存文件要用 utf-8 格式。
    以什么编码保存,就要用什么编码方式打开,否则出现乱码。

    • 2.3 变量

    问:为什么要有变量?

    为某个值创建一个“外号”,以后在使用时候通过此外号就可以直接调用。

      content = '钓鱼要钓刀鱼,刀鱼要到岛上钓。'
      content = 666
      print(content)
    

    变量的要求:

    1. 变量名只能包含:字母/数字/下划线
    
    2. 数字不能开头
    
    1. 不能是python的关键字。
      [‘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’]
    4. 建议:
    
    - 见名知意: name = "alex"   age= 18 
    - 用下划线连接:alex_dad = "吴佩其"  
      
    补充:AlexDad = '吴佩其' (驼峰式命名)
    
    • 2.4 解释器

      文件:a.py

      #!/usr/bin/env python  在Linux中指定解释器的路径
      # -*- coding:utf-8 -*-
      
      print('你好')
      

      运行: 解释器 文件路径

      在linux上有一种特殊的执行方法:

      • 给文件赋予一个可执行的权限

      • ./a.py 自动去找文件的第一行 = /usr/bin/env/python a.py

    • 2.5输入输出

    2.5.1读取键盘输入
    ###### Python2中有两个内置的函数可从标准输入读取数据,它默认来自键盘。这些函数分别是:
    
    ###### input() 和 raw_input()
    
    ###### 在Python3中,raw_input()函数已被弃用。此外, input() 函数是从键盘作为字符串读取数据,不论是否使用引号
    
        例:
    
        X=input("请输入x=") 
    
        y=input("请输入y=")
    
        z=x+y **print**("x+y="+z)
    
    2.5.2 输出
    PYTHON的输出函数 print()
    
    print(你想要输出的东西)
    
    • 2.6 条件判断

    2.6.1最简单
       if 条件:
    
    ​     内容
    
           gender = input("请输入性别:") # 女
           if gender == "男":
              print('再见') 
    
    2.6.2初级条件语句

    if 条件:

    ​ 内容

    else:

     内容
    
         # 请实现一个功能:让用户输入性别,如果是 男,则输出:再见;如果是 女:则输出 来呀来呀;
       
       gender = input("请输入性别:")
       """
       如果是男生:打印再见
       否则:打印来呀来呀
    
       
       if gender == "男":
       	print('再见')
       else:
       	print('来呀来呀')
    
    2.6.3 elif 条件

    if 条件:

    内容
    

    elif 条件:

    ​ 内容

    else:

    内容
      # 请实现一个功能:让用户输入性别,如果是 男,则输出:再见;如果是 女:则输出 来呀来呀;如果是 人妖:找alex去,他也是。否则:滚
    
          gender = input("请输入性别:")
       """
        如果是男生:打印再见
       否则:打印来呀来呀
       """
    
        if gender == "男":
            print('再见')
        elif gender == '女':
            print('来来来')
        elif gender == '人妖':
            print('找alex去,他也是')
        else:
                print('滚')
        print('end')
    

    2.6.4 三元运算(三目运算)

    v =  前面  if 条件 else 后面 
    
    if 条件:
    	v = '前面'
    else:
        v = '后面'
    
    # 让用户输入值,如果值是整数,则转换成整数,否则赋值为None
    
    data = input('>>>')
    value =  int(data) if data.isdecimal() else None 
    

    注意:先做出来,再思考如何简化

    • 2.7 循环语句

    2.7.1 while语句
    2.7.1.1 while True:
                             #循环打印 “人生苦短,我用Python。” 
                                  while True: 
                                     print('人生苦短,我用Python。')
    
    2.7.1.2 while后加入条件
                                   while 1>0 and 2>1:
       	                        print('人生苦短,我用Python。')
    
    2.7.1.3 while else
                                     count = 1
                                     while count < 10: 
     	                                print(count) 
     	                                count = count + 1 
                                    else: # 不再满足while后的条件时,触发。 或 条件=False 
                                             print('ELSE代码块') 
                                    print('结束')
    
    2.7.2 for 语句
    2.7.2.1 for ...in
                                              for letter in 'Python':   # 第一个实例
                                                          print ('当前字母 :',)
    
    2.7.2.2 for ..in range()

    #求前20项的斐波那契数
    a = 0
    b = 1
    for _ in range(20):
            (a, b) = (b, a + b)
            print(a, end=' ')
    
    2.7.2.3 for else语句

    for i in []:
    print(i)
    else:
    print("end")

    for i in range(3):
        if i % 2 == 0:
            print(i)
    else:
    	print("end")
    
    2.7.3 break 终止当前循环
    while True: 
        	print(666) 
        	break # 终止当前循环 
        print('结束')
    
    # 通过break实现 1 ~ 10 
    count = 1 
    while True: 
    	print(count) 
    	if count == 10: 
    		break 
    	count = count + 1 
    	print('结束')
    
    2.7.4 continue 本次循环如果遇到CONTINUE,则不在继续往下走,而是回到while 条件位置
    count=1
    while count <=10:
     print(count)
     if count == 7: 
      	continue 
     count=count+1
    
    • 2.8 字符串格式化

    2.8.1 %s 代表string 字符串
    # 字符串格式化存在的意义
    
    name = input('姓名:')
    do = input('在干什么:')
    template = "%s在教室,%s。" %(name,do,)
    print(template)
    # 直接做占位符
    
    # template = "我是%s,年龄%s, 职业%s。" %("alex",73,'讲鸡汤',)
    
    # print(template)
    
    2.8.2 %d 代表int 整型
    template = "我是%s,年龄%d, 职业%s。" %("alex",73,'讲鸡汤',)
    print(template)
    
    2.8.3 %% 百分号标记 #就是输出一个%
     name = 'alex' 
     template = "%s现在手机的电量是100%%" %(name,) 
     print(template)
        ```
    
    • 2.9 运算符

    2.9.1算数运算
    # 练习题: 1 ~ 100 之间所有的数相加。 
    
    total = 0 
    count = 1 
    while count <=100: 
        total = total + count 
        count = count + 1 
    print(total)
    
    # 练习题:打印 1 ~ 100 之间的奇数。
    count = 1 
    while count <= 100: 
    	val = count % 2 
    	if val == 1: 
    		print(count) 
    	count = count + 1
    
    2.9.2 赋值运算
         count = 1 
         while count <=100: 
             print(count) 
             count +=1  # count = count + 1
    
    2.9.3逻辑运算
    优先级
    1. and 与 2. or 或 3. not 非

      优先级 not > and > or

    一般情况 用于做判断
    if 1>0 and 1>2 :
      print('666')
    
    二般情况,用于做取值

    ​ or 或

    """
        对于 or,如果有遇到 value= 1 or 9
        第一个值如果是转换成布尔值如果是真,则value=第一值。
        第一个值如果是转换成布尔值如果是假,则value=第二值。
        如果有多个or条件,则从左到右依次进行上述流程。
        示例:
            v1 = 0 or 1
            v2 = 8 or 10
            v3 = 0 or 9 or 8
    
    

    and 与

    对于and,如果遇到 value= 1 and 9 这种情况
     如果第一个值转换成布尔值是True,则value=第二个值。
     如果第一个值转换成布尔值是False,则value=第一个值。
     如果有多个and条件,则从左到右依次进行上述流程。
     示例:
         v1 = 1 and 9
         v2 = 1 and 0
         v3 = 0 and 7
         v4 = 0 and ""
         v5 = 1 and 0 and 9
    

    ​ not 非

    对高优先级的运算结果取反,值为布尔(2为真,取反为假:False)     
          	not 2:False   
          	not 1 and 2:False
       	 	not 1 or 2:False
    		not not 1:True
    		not 0 :True
        `
    
    # 综合
    # 先看and再看or
    # v1 = 1 and 9 or 0 and 6
    # print(v1)
    
     1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  True
    
     not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 Flase
    

    2.9.4 in 与 not in

    in
    value = "我是中国人"
    
    # 判断‘中国’是否在value所代指的字符串中。 “中国”是否是value所代指的字符串的子序列。
    
    v1 = "中国" in value 
      ```
    
    
    
    
    
      # 示例
    
    content = input('请输入内容:')
    if "退钱" in content:
    	print('包含敏感字符')
    
    
     # 示例
    
    while True:
    	content = input('请输入内容:')
    	if "退钱" in content:
    		print('包含敏感字符')
    	else:
    		print(content)
    		break
    

    ​ not in

    #!/usr/bin/env python
    
    # print("hi!")
    
    name = "雷哈哈"
    
    # if "雷" in name :
    
    #     print("OK!")
    
    # else :
    
    #     print("error!")
    
    if "雷" not in name :
        print("OK!")
    else :
        print("error!")
    

    优先级

    not 2 > 1
    
    not 2     >    1   # 错误
    not    2>1   # 正确
    
  • 相关阅读:
    Linux面试题(后续会不断更新)
    01 . Shell详细入门介绍及简单应用
    06 . Prometheus监控Redis并配置Grafana
    Django实现图片上传并前端页面显示
    03 . 二进制部署kubernetes1.18.4
    Docker部署Python项目
    02 . Kubeadm部署Kubernetes及简单应用
    01 . 容器编排简介及Kubernetes核心概念
    05 . Prometheus监控Nginx
    GoAccess分析Web日志
  • 原文地址:https://www.cnblogs.com/flamechan1981/p/12329069.html
Copyright © 2011-2022 走看看