运算符与表达式

来源:互联网 发布:风油精滴到私处知乎 编辑:程序博客网 时间:2024/04/30 13:57

运算符
赋值运算符
‘=’等于:x = 3,y = “abcde”
‘+=’加等于:x+=2
‘-=’减等于:x-=2
=’乘等于:x=2
‘/=’除等于:x /=2
‘%’求余等于:x %=2
算术运算符
‘+’加法:x+y
‘-’减法:x-y
‘*’乘法:x*y
‘/’实数除法:3/2=1, 3.0/2=1.5
‘//’整数除法:5.6/2=2.8, 5.6//2=2.0
‘%’求余数:除法求余运算,如17除6余5
‘**’求幂运算: 2**3=8
关系运算符:起判断的作用
‘<’小于:1<2
‘>’大于:2>3
‘<=’小于等于:1<=1
‘>=’大于等于:2>=2
‘!=’不等于:1!=2
‘==’完全等于:2==2
逻辑运算符
‘and’逻辑与:True and False
‘or’逻辑或:True or False
‘not’逻辑非:not True

运算符的优先级

  1. 同级别的运算符,按从左到右处理
  2. 高优先级的先运算
  3. 运算符优先级由低到高排序:
    Lambda
    逻辑运算:or
    逻辑运算:and
    逻辑运算:not
    成员测试:in, not in
    同一性测试:is, is not
    比较:<, <=, >, >=, !=, ==
    按位或:|
    按位异或:^
    按位与:&
    移位:<<, >>
    加法与减法:+,-
    乘法、除法与取余:*, /, %
    正负号:+x,-x
    按位翻转:~x
    指数:**

表达式:将不同数据(包括变量、函数)用运算符按一定规则连接起来的一种式子

字符串类型变量的操作
1. string类型是不可以直接用运算符进行计算的,如下是非法的:
message-1 “Hello”/123 message*”Hello” “15”+2

>>> "Hello"/123Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for /: 'str' and 'int'
  1. +,* 可以用于string类型
    +:在string类型,+代表把字符串首尾相连
>>> fruit =  'banana'>>> baked_good = " nut bread">>> print fruit+baked_goodbanana nut bread

*:代表重复,左边必须是string型,后面跟的数字必须是int型

>>> 'Fun'*3'FunFunFun'

输入内置函数
python包含2种获取键盘输入的的内置函数:input,raw_input
input和raw_input都可以读取控制台的输入,但在处理数字时是有区别:
纯数字输入:
input返回的是数值类型,如int,float
raw_inpout返回的是字符串类型,string类型

输入字符串为表达式:
input会计算在字符串中的数字表达式,而raw_input不会
如输入 “57 + 3”:
input会得到整数60
raw_input会得到字符串”57 + 3”
脚本举例:

n = raw_input("Please enter your name:")print nn = input("Enter a numerical expression:")print n

运行结果:

C:\Users\Administrator>python test1.pyPlease enter your name:test1,test2test1,test2Enter a numerical expression:1+12

注解
以#号开头,可以单独一行显示,也可以跟在一段代码后面。#后面到该行末尾的字符串都被注解掉,对程序无影响

# compute the percentage of the hour that has elapsedpercentage = (minute * 100) / 60
percentage = (minute * 100) / 60     # caution: integer division

练习题:
1.写一个四则运算器
视频给了答案:

#!/usr/bin/pythona=int (raw_input("please input num1:"))b=int (raw_input("num2:"))print a+bprint a-bprint a*bprint a/b

由于视频解说是在Linux系统上举例的,今天花了几个小时纠结#!/usr/bin/python是什么。有人说是编译器的地址,加上它,可以在命令行直接输入./3.py运行程序。在Windows上运行,不用写#!这一块东西
不知道是这样理解不。。。

2.使用交互模式或写一个小程序完成下面问题

  • 3人吃饭,分摊35.27美元饭费,他们还想留15美分的小费,怎么分
personNum = float(raw_input("please input person number:"))allMoney = float(raw_input("All money number:"))tipMoney = float(raw_input("Tip money number:"))print (allMoney-tipMoney)/personNum

运行结果:

C:\Users\Administrator> 3.pyplease input person number:3All money number:35.27Tip money number:0.1511.7066666667
  • 计算12.5m X 16.7m的房间面积和周长
length = float(raw_input("please input the length:"))width = float(raw_input("Width:"))area = Length * Widthperimeter = length * 2 + width * 2print area, perimeter

运行结果:

C:\Users\Administrator> 3.pyplease input the length:12.5width:16.7208.75 58.4
  • 写一个程序,把华氏温度转化为摄氏温度。转换公式C = 5/9*(F-32)
F = float(raw_input("Please input F: "))C = (5.0/9*(F - 32))print C

刚开始5没有写成5.0,运行出来结果都是0.0。一位大哥告诉我,“5/9 这里已经 == 0了,在没有强制转换为float的时候,常量为整型的时候,都是按整型计算。”
运行结果:

C:\Users\Administrator> 3.pyPlease input F: 98.637.0
  • 写一个小程序运算以80km/h的速度行驶200km需要的时间,并显示答案
speed = int(raw_input("Please input speed:"))distance = int(raw_input("Distance:"))time = float(distance)/Speedprint time

运行结果:

C:\Users\Administrator> 3.pyPlease input speed:80distance:2002.5

今天做完这5道练习题,花了一下午时间,有两次因为数据类型转换问题,花了好些时间。有点心累~~
有位同事说我看的视频是歪的,“计算机科学国内的教材95%不能看,国内的视频99.99%不能看。越看越晕
因为他们自己都搞不清楚,你学了以后遇到问题也是稀里糊涂的”

在咨询问题的过程中,也学到 一些小知识,比如变量命名首字母应该是小写,要注意数据类型的转换。

感谢给我答疑的大神们!

我考虑换教材,去看官网推荐的书了
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers

下题是官方推荐课本《indexnext |How to Think Like a Computer Scientist: Learning with Python 2nd Edition documentation》的作业
http://openbookproject.net/thinkcs/python/english2e/ch02.html
题目:
7.Write a program (Python script) named madlib.py, which asks the user to enter a series of nouns, verbs, adjectives, adverbs, plural nouns, past tense verbs, etc., and then generates a paragraph which is syntactically correct but semantically ridiculous (see http://madlibs.org for examples).
我做出来的结果:

noun = raw_input("Please input a noun:")Number = raw_input("Input number:")Proper_noun = raw_input("Input a Proper:")Verb = raw_input("Input verb:")Adverb = raw_input("Input Adverb:")print "This is "+ noun+", Totally " + Number+ ". "+Proper_noun+" like it very much, he " +Verb+" it "+Adverb

运行结果:

C:\Users\Administrator>python madlib.pyPlease input a noun:BookInput number:3Input a Proper:TomInput verb:likeInput Adverb:slowlyThis is Book, Totally 3. Tom like it very much, he like it slowly
0 0
原创粉丝点击