python 基础教程之语法篇章——一小时入门python
概述,本文档中所有的程序内容都在linux下的Vim下面进行编辑,然后在解释器中运行
#!/usr/bin/python
#the following is code
…...
#the end
1.Hello world!——如何print输出
Print “hello world”
2.计算面积——语句分隔符+变量定义+数值运算
1 #!/usr/bin/python
2 wide=10
3 length=20
4 area=wide * length
5 print "hello world"
6 print area
3.输入与动态函数
1 #!/usr/bin/python
2 wide=10
3 length=input("the length of something")
4 area=wide * length
5 print "hello world"
6 print area
foo = input
foo("I can input now")
4.分支,循环,跳转
4.1分支
1 #!/usr/bin/python
2 tem=input("input a num")
3 if tem > 50:
4 print "more than 50"
5 else:
6 print "no more then 50"
注意:没有括号,要加冒号
# Area calculation program
print "Welcome to the Area calculation program"
print "---------------------------------------"
print
# Print out the menu:
print "Please select a shape:"
print "1 Rectangle"
print "2 Circle"
#Get the user's choice:
shape = input(">; ")
#Calculate the area:
if shape == 1:
height = input("Please enter the height: ")
width = input("Please enter the width: ")
area = height *width
print "The area is ", area
else:
radius = input("Please enter the radius: ")
area = 3.14 * (radius**2)
print "The area is ", area
1. 只使用print本身将打印出一个空行
2. ==检查两个值是否相等,与=不同,后者把表达式右侧的值赋给左侧的变量。这是一个非常重要的差别!
3. **是python的幂运算符--因此半径的平方被写成radius**2
4. print能够打印出不止一个东西。只要用逗号把它们分开就可以了。(它们在输出时会用单个空格分开。)
4.2for循环
for food in "spam", "eggs", "tomatoes":
print "I love", food
数组:
for number in range(1, 100):
print "Hello, world!"
print "Just", 100 - number, "more to go..."
time模块中的sleep:
# Spam-cooking program
# Fetch the function sleep
from time import sleep
print "Please start cooking the spam. (I'll be back in 3 minutes.)"
# Wait for 3 minutes (that is, 3*60 seconds)...
sleep(180)
print "I'm baaack :)"
# How hot is hot enough?
hot_enough = 50
temperature = input("How hot is the spam?")
while temperature < hot_enouth:
print "Not hot enough... Cook it a bit more..."
sleep(30)
temperature = input("OK, How hot is it now?")
print "It's hot enough - You're done!"
4.3while循环
def floor(number)://注意,这个地方的冒号
result = 0
while result <= number:
result = result + 1
result = result - 1
return result
函数定义与while循环,注意利用严格的缩进而不是括号来传达层次关系
两个变量可以象这样一起赋值:x, y = y, y+1
5.复杂的数据结构
#!/usr/bin/python
2 # Calculate all the primes below 1000
3 import math//类include语句
4 result = [2]
5 for test in range(3,1000):
6 bound=(int)(math.sqrt(test))//引用与数字类型转化
7 for i in range(2,bound+2):
8 if test%i==0:
9 break
10 if i==bound+1:
11 result.append(test)
12 print result
内建函数range事实上返回一个列表,可以象所有其它列表那样使用。(它包括第一个数,但是不包括最后一个数。)
列表可以当作逻辑变量使用。如果它非空,则为true,否则为false。因此,while candidates意思是“while名称为candidates的列表非空时”或者简单的说“while存在candidates时”。
你可以用if someElement in somelist来检查一个元素是否在列表中。
你可以用someList.remove(someElement)来删除someList中的someElement。
你可以用someList.append(something)为一个列表添加元素。事实上,你也可以使用“+”(象someList = someList+[something])。但是效率不是太高。
你可以通过在列表名之后加上用括号括起来的表示某元素位置的数字(很奇怪,列表的第1个元素,位置是0)来获得列表的某个元素。因此someList[3]是someList 列表的第四个元素(依次类推)。
你可以使用关键字del删除变量。它也可以用来删除列表中的元素(就象这里)。因此del someList[0]删除someList 列表中的第一个元素。如果删除前列表是[1, 2, 3],删除后就变成了[2, 3]。
6.继续抽象-对象和面向对象编程
class Oven:
def insertSpam(self, spam):
self.spam = spam
def getSpam(self):
return self.spam
对象的类用关键字class定义。
类的名称通常以大写字母开始,而函数和变量(还有属性和方法)的名称以小写字母开始。
方法(也就是让对象知道如何去做的函数和操作)的定义没有特别,但是要在类的定义里面。
所有对象的方法应当有的第一个参数叫做self(或者类似的……)原因很快就清楚了。
对象的属性和方法可以这样来访问:mySpam.temperature = 2 或者dilbert.be_nice()。
我能猜到上面例子中的某些东西你仍然不清楚。例如,什么是self?还有,现在我们有了对象菜谱(也就是类),我们怎样事实上构造一个对象呢?
我们先颠倒一下顺序。对象通过象引用函数那样引用类名来创建:
myOven = Oven()
myOven包含了一个Oven对象,通常叫做Oven类的一个实例。假设我们也构造好了一个Spam类,那么我们可象这样做:
mySpam = Spam()
myOven.insertSpam(mySpam)
myOven.spam现在将包含mySpam。怎么回事?因为,我们调用一个对象的某个方法时,第一个参数,通常称为self,总是包含对象本身。(巧妙,哈!)这样,self.spam =spam这一行设置当前Oven对象的spam属性的值为参数spam。注意它们是两个不同的事物,尽管在这个例子中它们都被称为spam。
7.python与C的一些对比
7.1逻辑运算
python使用or and not来进行逻辑运算,而C语言使用&& || ~
补充:Web开发 , Python ,