运算符与表达式

运算符
赋值运算符
‘=’等于: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"/123
Traceback (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_good
banana 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 n
n = input("Enter a numerical expression:")
print n

运行结果:

C:\Users\Administrator>python test1.py
Please enter your name:test1,test2
test1,test2
Enter a numerical expression:1+1
2

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

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

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

#!/usr/bin/python

a=int (raw_input("please input num1:"))
b=int (raw_input("num2:"))

print a+b
print a-b
print a*b
print 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.py
please input person number:3
All money number:35.27
Tip money number:0.15
11.7066666667
  • 计算12.5m X 16.7m的房间面积和周长
length = float(raw_input("please input the length:"))
width = float(raw_input("Width:"))
area = Length * Width
perimeter = length * 2 + width * 2

print area, perimeter

运行结果:

C:\Users\Administrator> 3.py
please input the length:12.5
width:16.7
208.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.py
Please input F: 98.6
37.0
  • 写一个小程序运算以80km/h的速度行驶200km需要的时间,并显示答案
speed = int(raw_input("Please input speed:"))
distance = int(raw_input("Distance:"))
time = float(distance)/Speed

print time

运行结果:

C:\Users\Administrator> 3.py
Please input speed:80
distance:200
2.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.py
Please input a noun:Book
Input number:3
Input a Proper:Tom
Input verb:like
Input Adverb:slowly
This is Book, Totally 3. Tom like it very much, he like it slowly
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值