您的位置:首页 > 编程语言 > Python开发

笨方法学Python(1-5)

2015-12-25 15:51 771 查看

习题1、第一个程序

#!/usr/bin/python
# -*- coding:utf-8 -*-

__author__ = 'QUIQUE'

print "Hello World!"
print "Hello Again"
print "I like trying this."
print "This is fun."
print "Yay! Printing."
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'


自我总结

1. 单引号和双引号,使用时避免 临近配对

2. 井号有很多的英文名字,例如:’octothorpe(八角帽)’,’pound(英镑符)’,’hash(电话的#键)’,‘mesh(网)’ 等。

习题2、注释和#号

#!/usr/bin/python
# -*- coding:utf-8 -*-

__author__ = 'QUIQUE'

# A comment, this is so you can read your program later.
# Anything fater the # is ignored python.

print "I could have code like this." #and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
#print "This won't run."

print "This will run."


总结:

如果 # 是注解的意思,那么为什么
# -*- coding: utf-8 -*-
能起作用呢?

Python 其实还是没把这行当做代码处理,这种用法只是让字符格式被识别的一个取巧的方案,或者说是一个没办法的办法吧。在编辑器设置里你还能看到一个类似的注解。

为什么
print "Hi # there."
里的 # 没被忽略掉?

这行代码里的
#
处于字符串内部,所以它就是引号结束前的字符串中的一部分,这时它只是一个普通字符,而不代表注解的意思。

习题3、数字和数学计算

#!/usr/bin/python
# -*- coding:utf-8 -*-

__author__ = 'QUIQUE'

"""
• + plus 加号
• - minus 减号
• / slash 斜杠
• * asterisk 星号
• % percent 百分号
• < less-than 小于号
• > greater-than 大于号
• <= less-than-equal 小于等于号
• >= greater-than-equal 大于等于号
"""

print "I will now count my chickens:"

print "Hens", 25 + 30 / 6
print "Roosters", 100 - 25 * 3 % 4

print "Now I will count the eggs:"

print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6

print "Is it true that 3 + 2 < 5 - 7?"

print 3 + 2 < 5 - 7

print "What is 3 + 2?", 3 + 2
print "What is 5 - 7?", 5 - 7

print "Oh, that's why it's False."

print "How about some more."

print "Is it greater?", 5 > -2
print "Is it greater or equal?", 5 >= -2
print "Is it less or equal?", 5 <= -2


总结

为什么 % 是求余数符号,而不是百分号?

很大程度上只是因为涉及人员选择了这个符号而已。一般而言它是百分号没错,就跟 100% 表示

百分之百一样。在编程中除法我们用了 /,而求余数又恰恰选择了 % 这个符号,仅此而已。

% 是怎么工作的?

换个说法就是“X 除以 Y 还剩余 J”,例如“100 除以 16 还剩 4”。 % 运算的结果就是 J 这部

分。

运算优先级是什么样子的?

美国我们用 PEMDAS 这个简称来辅助记忆,它的意思是“括号、指数、乘、除、加、减”——

Parentheses Exponents Multiplication Division Addition Subtraction ——这也是 Python 里的

运算优先级。

为什么 / 除法算出来的比实际小?

其实不是没算对,而是它将小数部分丢弃了,试试 7.0 / 4.0 和 7 / 4 比较一下,你就看出不同了。

习题4、变量(variable)和命名

#!/usr/bin/python
# -*- coding:utf-8 -*-

cars = 100
space_in_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
car_driven = drivers
carpool_capacity = car_driven * space_in_car
average_passengers_per_car = passengers / car_driven

print "There are", cars, "cars availabe."
print "There are only", drivers, "drivers available."
print "There will be", cars_not_driven, "empty cars today."
print "We can transport", carpool_capacity, "people today."
print "We have", passengers, "to carpool today."
print "We need to put about", average_passengers_per_car, "in each car."


总结

= 和 == 有什么不同?

=
(single-equal) 的作用是将右边的值赋予左边的变量名。
==
(double-equal) 的作用是检查左右离岸边是否相等。习题 27 中你会学到 == 的用法。

写成
x=100
而非
x = 100
也没关系吧?

是可以这样写,但这种写法不好。操作符两边加上空格会让代码更容易阅读。

print 时词语间的空格有没有办法不让打印出来?

你可以通过这样的方法实现:
print "Hey %s there." % "you"
,后面马上就会讲到。

怎样倒着读代码?

很简单,假如说你的代码有 16 行,你就从第 16 行开始,和我的第 16 行比对,接着比对第 15

行,以此类推,直到全部检查完。

习题5、更多的变量和打印

#!/usr/bin/python
# -*- coding:utf-8 -*-

"""
格式化字符串的使用
"""

my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth

# this line is triccky, try to get it exactly right
print "If I add %d, %d and %d I get %d." % (
my_age, my_height, my_weight, my_age + my_weight + my_height)


总结

这样定义变量行不行:
1 = 'Zed Shaw'


不行。 1 不是一个有效的变量名称。变量名要以字母开头。所以 a1 可以,但 1 不行。

%s
,
%r
,
%d
这些符号是啥意思?

后面你会详细学到更多,现在可以告诉你的是它们是一种“格式控制工具”。它们告诉 Python 把

右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上。

还是不懂,“格式控制工具”是啥?

要明白一些描述的意义,你得先学会编程才更容易理解,你可以把这样的问题记录下来,看后面的

内容会不会向你解释这些东西。

如何将浮点数四舍五入?

你可以使用
round()
函数,例如:
round(1.7333)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: