zoukankan      html  css  js  c++  java
  • 二、基础语法

    1. 注释,使用#号注释
    2. 变量声名
      • 只能包含字母、数字、下划线,以字母、下划线开头,不能以数字开头
      • 变量名不能包含空格
      • 不要将关键字做为变量名
      • 小写字母声名变量
      • 变量名简短又具有描述性
    3. 数据类型
      • 字符串,str ;使用双引号或者单引号包括,推荐使用单引号
      • 数字
    4. 字符串
      • 字符串拼接
      • a="hello" 
        name="Jack"  
        greeting=a+name
        
        print(greeting)
        View Code

        大小写操作 title()首字母大写, upper()全部转大写 lower()全部转小写

      • name="alice jucy"
        last_name=' joke tTTTm'
        
        print(name.title())
        print(last_name.title())
        print(name.upper())
        print(last_name.lower())
        
        print(name)
        print(last_name)
        View Code

        格式化字符串

      • my_age=33 # not a lie
        let_talk="Let's talk about{}"
        
        print(let_talk.format(my_age))
        
        print("Hello everyone my age is {}".format(my_age))
        View Code

        重复指定次数

      • a="hello" 
        name="Jack"  
        greeting=a+name
        
        print(greeting*10)
        View Code

         多行文本 使用三个连续双引号或者单引号

      • fat_cat='''
        I'll do a list
        	*  Cat food 
        	* Fishies
        	* Catnip
        	* Grass
        '''
        
        fat_cat2='''
        I'll do a list
        	*  Cat food 
        	* Fishies
        	* Catnip
        	* Grass
        '''
        print(fat_cat)
        print(fat_cat2)
        View Code

        转义字符  表示 转义  常见:  换行, 制表  \转本身 "双引号字符串

      • tabby_cat="	I'm tabbed " in ."
        persian_cat="I'm split
        on al line."
        backsLash_cat="I'm \a \ cat"
        
        
        print(tabby_cat)
        print(persian_cat)
        View Code

         删除字符串开头结尾空白 rstrip() lstrip() strip()

      • right_space="alice jucy  "
        left_space=' joke tTTTm'
        space='  Afdsa afd   '
        
        print(right_space.rstrip())
        print(right_space)
        
        print(left_space.lstrip())
        print(left_space)
        
        print(space.strip())
        print(space)
        View Code

         

    5. 数字 
      • 优先级同数学运算优化级
      • 运算包括:+、-、*、**(平方)、/、%、>、>=、<、<=
      • 整数
      • 浮点数
      • print(2+3)
        print(2-3)
        print(2*3)
        print(2/3)
        print(2**3)
        
        print(0.2+0.1)
        print(0.222+0.33)
        View Code

        py2版本中整数计算时如果有小数,会直接舍弃

    6. 列表
      • 列表命名时以s结尾,表示复数形式
      • 列表元素索引从0开始
      • 修改元素为指定索引赋值
      • 读取元素使用列表[索引]读取
      • 末尾添加使用append
      • 中间或开头位置添加使用insert(位置,数据)
      • del 删除列表元素
      • pop 指定索引位置元素并素赋值给接收变量,原列表指定位置移除元素。
      • remove()根据元素值删除列表数据,每次只能删除第一个找到数据。
      • sort()永久排序,通过参数(reverse=True)可指定相反顺序
      • sorted临时排序,通过参数(reverse=True)可指定相反顺序
      • 列表倒序使用reverse()
      • len(列表)获得列表长度
    7. cars=['BMW','BEN','DW',"XD"]
      print(cars)
      print(cars[0])
      
      ints=[1,2,3,4]
      print("my age "+str(ints[3])+" old")
      
      #del 示例
      print(ints)
      del ints[2]
      print(ints)
      
      #pop 示例
      ints.insert(2,3)
      print(ints)
      
      int0=ints.pop(0)
      
      print(int0)
      print(ints)
      
      print("
      	 根据值删除元素示例")
      #根据值删除元素
      ints.insert(2,3)
      print(ints)
      ints.remove(3)
      print(ints)
      
      #永久排序
      print("
      排序")
      ints=[1,8,9,10,7,3,4]
      
      print("临时排序")
      print(ints)
      print(sorted(ints,reverse=True))
      print(ints)
      
      print("永久排序")
      
      print(ints)
      ints.sort(reverse=True)
      print(ints)
      
      print("列表长度")
      print(len(ints))
      
      #链式操作尝试失败
      print("尝试链式操作")
      ints=[1,8,9,10,7,3,4]
      print(ints.sort())
      print(ints.reverse())
      print(ints.sort().reverse())
      View Code

        操作列表

      • for 循环  for item in list: 注意冒号容易被遗漏 .
      • for循环待执行代码通过缩进控制
      • magicians=['alice','david','carolina']
        
        print(magicians)
        
        for magician in magicians:
            
            print(magician.title()+" Thank you ")
            print("Have a nice day "+magician.title()+"
        ")
        print("Thank you everyone")
        View Code

        数字列表

      • range(min,max,步长) 生成从min开始(包含)到max(不包含)结束数字列表
      • list()将数据转换为列表
      • squals=[]
        for value in list(range(1,13,2)):
            print(value)
            squals.append(value**2)
        print(squals)
        
        digits=list(range(1,11))
        print(min(digits))
        
        print(max(digits))
        
        print(sum(digits))
        
        varlist= list(range(1,1000000))
        print(sum(varlist))
        View Code

        切片,列表[开始索引,结束索引],结束索引到指定位置(不包含)结束。索引值为负数时从结尾开始计算
          开始索引、结束索引不填写时可复制整个列表。

      • 列表通过切片复制后会等到新副本,通过直接赋值操作不会形成新副本。
      • pian=list(range(1,20))
        pian.append(20)
        pian.append(21)
        pianclone.append(22)
        pianclone.append(23)
        
        print(pian)
        print(pianclone)
        View Code
      • in 和 not in 确定值是否在列表中
      • cars=["audi","bmw","subaru",'toyota']
        bmw='bmw'
        
        if bmw in cars:
            print("I find bmw")
        
        if "Ben" not in cars:
            print("I don't find ben")
        View Code
    8. 元组条件判断
      • 元组同列表,使用()定义,元组值不能单个修改。
      • 元组再次赋值时修改值,只能整体修改
    9. if语句
      • if 计算规则。
      • if-else
      • if-elif-else
      • View Code
    10. 字典
      • 字典是大括号包括的键值对形式,值的内容可以是任何形式。
      • 字典同json
      • 遍历所有值 for key,value in dic.items()
      • 遍历所有键 for key in dic.keys()或者for key in dic 默认形式可以不带.keys().
      • 遍历所有值 for value in dic.values()
      • 字典 .key(),.values()方法返回值是列表,可以根据列表所有操作。
      • View Code
      • 字典和列表可以互相嵌套
    11. while循环
      • wile循环不断运行直到条件不满足为止
      • 中间退出使用break
      • continue跳过后续代码继续执行
      • while True:
            inputs= input("input you info,
         input quit finish")
            if(inputs=='quit'):
                active=False
                #也可能过break退出
            else:
              print(inputs)     
        View Code
    12. input
      • input([prompt]),显示提示信息,并接收输入值,接收的输入值为str类型
      • age=input("How old are you :")
        print(age)
        intage=int(age)
        print(intage)
        View Code

    基础语法结束

  • 相关阅读:
    ADO.NET 数据连接查询
    弹出消息对话框类
    C# 火星文转化 算法 dictionary 的使用案例
    微软官方的SqlHelper
    jsHelper
    【VC++积累】之六、MFC简要剖析
    C# 统计文章中字符的种类和个数 哈希表和字典的使用
    C# NetHelper
    C# XML操作类 XmlHelper
    【VS++积累】之七、黑客编程之匿名管道
  • 原文地址:https://www.cnblogs.com/bro-ma/p/10367914.html
Copyright © 2011-2022 走看看