zoukankan      html  css  js  c++  java
  • python基础一

    1. python2与python字符编码问题  

      python2默认编码格式为ASC2,而python默认编码格式为utf-8,所以在出现编译编码报错时,需要在编码首行加上如上代码:

    # -*- encoding:utf-8 -*-

    python 支持3种编码声明,一般常用能见到下面两种
    1.# -*- coding: utf-8 -*-
    这种写法是为了兼容Emacs的编码声明
    2.短一点,但Emacs不能用# coding=utf-8
    短一点,但Emacs不能用
    之所以要声明未编码类型 ,主要是中文出错的问题。
    在python 文件开头(一般是第一行或第二行),用来说明你的Python源程序文件用使用的编码。缺省情况下你的程序需要使用ascii码来写,但如果在其中写中文的话,python解释器一般会报错,但如果加上你所用的文件编码,python就会自动处理不再报错。
    这里要注意的是:
    1.coding 后面使用 ":" 或 "=" 都可以
    2.但是, ":" 或 "=" 必须和 coding之间没有空格。之前我就试过有空格声明失败,还是不支持中文。至于 ":" 或 "=" 后面,有没有空格就没所谓了。

    2.------变量-------
    #变量定义:变量就是将一些数据中间结果暂时保存起来。
    变量的命名规则:1.变量是由字母数字下滑下线组成;
            2.变量不能以python关键字命名;
            3.变量不能以数字开头;
            4.变量才有驼峰式或下滑线形式命名;
            5.变量不建议使用中文命名;
    age1 = 24
    age2 = age1
    age3= age2
    age2 = 22
    #程序是自上而下执行
    

    3.给变量赋值时如果,如果实现换行的效果,可以使用单引号货双引号注释来实现

    print(age1,age2,age3)
    msg = """窗前明月光,
            疑是地上霜"""
    msg1 = '''窗前明月光,
    疑似地上霜'''
    msg2 = "窗前明月光," 
           "疑似地上霜"
    print(msg)
    print(msg1)
    print(msg2)
    

    4.字符串的可以实现的加+,和字符串的乘*。+作用:连接字符串,*作用:打印n个数字符串

    str1 = 'cat'
    str2 = 'dog'
    print(str1+str2)
    print(str1*3+str2*5)
    

    5.布尔值

    print(0<1)
    print(0>1)
    #input输出的全部为字符串
    a = input("请输入内容")
    print(type(a))
    

    6.if判断语句五种类型

     6.1 第一种:单if判断

    ss = 8
    if ss>6:
        print("恭喜你中大奖了")
    

      

        6.2 第二种:if...else二选一类型

    num =int(input("请输入你的存款"))
    if num>88:
        print("你是富豪")
    else:
        print("穷鬼一个继续努力")
    

      

        6.3 第三种:if..elif多选判断

    cho = int(input("请输入色子种的任意一个数"))
    if cho == 1:
        print("太小了,手气真差")
    elif cho == 2:
        print("有点小,继续努力")
    elif cho == 3:
        print("还可以")
    elif cho == 4:
        print("中不溜")
    

      

        6.4 第四种结构: if elif elif ..... else

    cho = int(input("请输入色子种的任意一个数"))
    if cho == 1:
        print("太小了,手气真差")
    elif cho == 2:
        print("有点小,继续努力")
    elif cho == 3:
        print("还可以")
    elif cho == 4:
        print("中不溜")
    else:
        print("你已经很大了")
    

      

     

        6.5 第五种:if...else嵌套

    ca = input("请输入石头,剪刀,布")
    cd = "石头"
    if ca == "石头" or ca == "剪刀" or ca == "布" :
        if ca == cd:
            print("平局")
        elif ca == "剪刀":
            print("你赢了")
        else :
            print("你输了")
    else:
        print("你输入的内容不合法")
    

      

     

     


     
  • 相关阅读:
    利用相关的Aware接口
    java 值传递和引用传递。
    权限控制框架Spring Security 和Shiro 的总结
    优秀代码养成
    Servlet 基础知识
    leetcode 501. Find Mode in Binary Search Tree
    leetcode 530. Minimum Absolute Difference in BST
    leetcode 543. Diameter of Binary Tree
    leetcode 551. Student Attendance Record I
    leetcode 563. Binary Tree Tilt
  • 原文地址:https://www.cnblogs.com/qilin610/p/9410450.html
Copyright © 2011-2022 走看看