zoukankan      html  css  js  c++  java
  • python的变量

    变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变。
     
    python下变量是对一个数据的引用
     
    变量的命名
    -变量名由字母,数字,下划线组成。
    -变量不能以数字开头
    -不可以使用关键字
    -a a1 _a
     
    变量的赋值
    -是变量的声明和定义的过程
     a = 1
    id(a)
     
    运算符与表达式
    Python运算符包括
    -赋值运算符
    -算术运算符
    -关系运算符
    -逻辑运算符
    表达式是将不同的数据(包括变量,函数)用运算符号按一定规则连接起来的一种式子。
     
    x = 2
    type(x)
    显示 int
     
    x = '2'
    type(x)
    显示 str
     
    x = 2
    x +=2
    x
    显示 4
     
    x -=3
    x
    显示 1
     
    x *=4
    x
    显示 4
     
    x  /=4
    x
    显示 1
     
    3 + 4
    显示 7
     
    'a'+'b'
    显示 ‘ab'
     
    3 - 4
    显示 -1
     
    3 * 4
    显示 12
     
    4 / 3
    显示 1
     
    4.0 / 3
    显示 1.33333333333
     
    4.0 // 3
    显示 1.0  表示整除的部分
     
    4 // 3
    显示 1
     
    4 % 3
    取余
     
    2**3
    显示 8 2的3次方
     
    2**10
    显示 1024 2的10次方
     
    1 > 2
    显示 false
     
    1 < 2
    显示 true
     
    1 >= 2
    显示 false
     
    1 == 1
    显示true
     
    1== 2
    显示 false
     
    1 != 2
    显示 true
     
    逻辑运算符
    and 逻辑与 : true and false
    or 逻辑或 : false or true
    not 逻辑非 : not true
     
    1 == 2 and 2 > 1
    显示 false
     
    1 == 1 and 2 > 1 
    显示 true
     
    1 == 2 or 2 > 1
    显示 true
     
    not 1 == 2
    显示 true
     
    写一个四则运算器
    -要求从键盘读取数字
    input()与raw_input()区别
     
    input("Please input: ")
    输入 123 ,显示 123
    输入'abc'  显示 'abc'
    输入 abc 会报错
     
    raw_input("Please input: ")
    输入 abcdjf ,显示 'abcdjf'
    输入 234 显示 ‘234’
     
    vim 3.py
     
    #!/usr/bin/python
     
    num1 = input("Please a number: ")
    num2 = input("Please a number: ")
     
    print "%s + %s = %s" %(num1,num2,num1+num2)
    print "%s - %s = %s" %(num1,num2,num1-num2)
    print "%s * %s = %s" %(num1,num2,num1*num2)
    print "%s / %s = %s" %(num1,num2,num1/num2)
     
    显示:
    python 3.py
    Please a number: 4
    Please a number: 5
    4 + 5 = 9
    4 - 5 = -1
    4 * 5 = 20
    4 / 5 = 0
     
     
    简单计算器
     
    [root@weifeng08 day01]# cat cal.py
    #!/usr/bin/python
     
    def counter():
        while True:
            num1 = int(raw_input("please input num1:"))
            oper = raw_input("please input oper:")
            num2 = int(raw_input("please input num2:"))
     
            if oper == "+":
                print "%s + %s = %s" %(num1,num2,(num1+num2))
            elif oper == "-":
                print "%s - %s = %s" %(num1,num2,(num1-num2))
            elif oper == "*":
                print "%s * %s = %s" %(num1,num2,(num1*num2))
            elif oper == "/":
                if num2 == 0:
                    print 'error / zero'
                    break
                else:
                    print "%s / %s = %s" %(num1,num2,(float(num1)/float(num2)))
            exit = raw_input("input q exit:")
            if exit.upper() == "Q":
                 break;
     
    counter()
     
     
  • 相关阅读:
    Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题
    Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题
    Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
    Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增
    BZOJ 4706: B君的多边形 找规律
    HDU 5909 Tree Cutting 动态规划 快速沃尔什变换
    Codeforces Round #284 (Div. 1) A. Crazy Town 计算几何
    HDU 5908 Abelian Period 暴力
    HDU 5907 Find Q dp
    Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心
  • 原文地址:https://www.cnblogs.com/weifeng1463/p/7419403.html
Copyright © 2011-2022 走看看