python语法集合之一

第一个程序

将下面的内容写到一个文件中,取名为ex1.py。这种命名方式很重要,Python文件最好以.py结尾

println "Hello World"

在终端运行python ex1.py结果如下:

Hello World

注意:如果你看到关于ASCII编码的错误,那就在你的Python脚本的最上面加入下面这行代码:

# -*- coding: utf-8 -*-

这样你就在脚本中使用了Unicode UTF-8编码,这些错误就不会出现了,例如

# -*- coding: utf-8 -*-
print "今天是星期六,不用上班"

运行结果如下:

今天是星期六,不用上班

注释和#号

程序里的注释很重要,它们可以用自然语言告诉你某段代码的功能是神马,想要临时移除一段代码时,你还可以用注释的方式临时禁用这段代码。

# A comment, this so you can read your program later.
# Andthing after the # is ignored by python.

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

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

print "This will run."

运行结果:

I cold have coe like this.
This will run.

数字和数学计算

每一种编程语言都包含处理数字和进行数学计算的方法,Python里的运算优先级”括号、指数、乘、除、加、减”

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

运行结果:

I will now count my chickens:
Hens 30
Roosters 82
Now I will count the eggs:
7
Is it true that 3 + 2 < 5 - 7?
False
What is 3 + 2? 5
What is 5 - 7? -2
Oh, that's why it's False.
How about some more.
Is it greater? True
Is it greater or equal? True
Is it less or equal? False

变量和命名

在编程中,变量只不过是用来指代某个东西的名字,程序员通过使用变量名可以让他们的程序阅读起来更像自然语言

cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars-drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print "There are",cars,"cars available."
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."

运行结果:

There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

学会如何创建包含变量内容的字符串(格式化字符串)

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 taill." % my_height
print "He's %d pounds heavy." % my_weight
print "Actuall 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 tricky, 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_height+my_weight)

运行结果:

Let's talk about Zed A. Shaw.
He's 74 inches taill.
He's 180 pounds heavy.
Actuall that's not too heavy.
He's got Blue eyes and Brown hair.
His teeth are usually White depending on the coffee.
If I add 35, 74,and 180 I get 289.

字符串和文本

我们将键入大量的字符串、变量、格式化字符,并且将它们打印出来,我们还将联系使用简写的变量名

x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who know %s and those who %s." % (binary,do_not)

print x
print y

print "I said: %r." % x
print "I also said: '%s'." % y
hilarious = False
joke_eveluation = "Isn't that joke so funny?! %r"

print joke_eveluation % hilarious

w = "This is the left side of..."
e = "a string with a right side."

print w + e

运行结果:

There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.

参数,解包和变量

下面我们讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你编写的.py程序)。下列代码就是一个可以接受参数的脚本

# conding: utf-8
from sys import argv

script, first, second, third,last=argv

print "The script is called:",script
print "Your first variable is:",first
print "Your second variable is:",second
print "You third variable is",third
print "You last variable is",last

第2行我们有一个import语句,这是你将python的特性引入脚本的方法。

argv即所谓的“参数变量”这是一个非常标准的编程术语,这个变量保存着你运行python脚本时传递给python脚本的参数。

第3行将argv解包”(unpack),与其将所有参数放到同一个变量下面,不如将每个数赋值给一个变量:script、first、secode、thrid和last。

解包:把argv中得东西解包,将所有的参数依次赋值给左边的这些变量

上面代码运行结果如下:

➜  Python  python ex11.py lucy lili jack soon
The script is called: ex11.py
Your first variable is: lucy
Your second variable is: lili
You third variable is jack
You last variable is soon

其实可以将first,second,third,last替换成任意三样东西,你可以将它们换成任意你想要的东西

argv和raw_input()有什么不同? 不同点在于用户输入的时机,如果参数是在用户执行命令时就要输入,那就是argv,如果是在脚本运行过程中需要用户输入,那就使用raw_input()。

提示和传递

让我们使用argv和raw_input一起来向用户提供一些特别的问题,让它显示一个简单的>作为提示符

#coding:utf-8
from sys import argv

script,user_name=argv
prompt='>'

print "Hi %s, I'm the %s script." % (user_name,script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes=raw_input(prompt)

print "Where do you like %s?" % user_name
lives=raw_input(prompt)

print "What kind of computer do you have?"
computer=raw_input(prompt)

print """
Alright, So you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
"""  % (likes,lives,computer)

运行结果:

➜  Python  python ex12.py Jack
Hi Jack, I'm the ex12.py script.
I'd like to ask you a few questions.
Do you like me Jack?
>yes
Where do you like Jack?
>San Francisco
What kind of computer do you have?
>Tandy 100

Alright, So you said 'yes' about liking me.
You live in 'San Francisco'. Not sure where that is.
And you have a 'Tandy 100' computer. Nice.
----EOF-----

Categories: python Tags: python