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

初识Python(一)

2011-07-20 21:11 435 查看
Python语言是少有的一种可以称得上即简单功能强大的编程语言。你将惊喜地发现Python语言是多么地简单,它注重的是如何解决问题而不是编程语言的语法和结构。

Python的官方介绍是:

Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程。Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发。

Python 2.3.4的下载地址http://download.csdn.net/source/1861669 下载后就可以使用其自带的编辑器IDLE了

Python区分大小写 Python中的注释用#



Python中有4种类型的数:整数,长整数,浮点数,复数

整数:如2 长整数:大一些的整数 浮点数:如3.23 52.3E-4(
52.3 * 10
-4) 复数:如
(-5+4j)
(2.3-4.6j)


字符串

Python中字符串既可以用单引号也可以用双引号来表示,并没有区别,Python中并没有Char类型。Python中还可以用三引号来表示一个多行的字符串,在该字符串中你可以在里面任意的使用单引号或双引号,如:

'''This is a multi-line string. This is the first line.

This is the second line.

"What's your name?," I asked.

He said "Bond, James Bond."

'''

Python中也是使用\来作为转义符,如你要在一个字符串中表示一个单引号,有两种方法:

1."What's your name?" 2.'What\'s your name?'

值得注意的一件事是,在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行

如"This is the first sentence.\

This is the second sentence."

等价于"This is the first sentence. This is the second sentence."

自然字符串

如果你想要指示某些不需要如转义符那样的特别处理的字符串,那么你需要指定一个自然字符串。自然字

符串通过给字符串加上前缀r或R来指定。例如r"Newlines are indicated by \n"。

Unicode字符串

Python允许你处理Unicode文本——你只需要在字符串前加上前缀

u或U。例如,u"This is a Unicode string."。

记住,在你处理文本文件的时候使用Unicode字符串,特别是当你知道这个文件含有用非英语的语言写的文本。

Python中字符串也是不可变的,这意味着一旦你创造了一个字符串,你就不能再改变它了。

如果你把两个字符串按字面意义相邻放着,他们会被Python自动级连。例如,'What\'s' 'your name?'会被自动转为"What's your name?"。

变量

变量只是你的计算机中存储信息的一部分内存。与字面意义上的常量不同,你需要一些能够访问这些变量的方法,因此你给变量命名。其命名规则与C#相同。

对象

Python把在程序中用到的任何东西都称为对象。包括数、字符串甚至函数都是对象。

我们来看一个简单的例子

# Filename : myfirstpythonprogram.py

i = 5

print i

i = i + 1

print i

s = '''This is a multi-line string.

This is the second line.'''

print s

结果:

5

6

This is a multi-line string.

This is the second line.

我们看Python使用变量时不需要声明或定义数据类型,一条语句也没有结束符

但有时可以多条语句占一行,这时就需要用分号来区分每条语句了,如:

i=5;print i; 等价于

i=5

print i

运算符

我们主要来看一下Python中一些不一样的运算符

幂 ** 取整除 // 布尔非 not 布尔与 and 布尔或 or

控制流

if语句(Python中没有switch语句)

number = 23

guess = int(raw_input('Enter an integer : '))

if guess == number:

print 'Congratulations, you guessed it.' # New block starts here

print "(but you do not win any prizes!)" # New block ends here

elif guess < number:

print 'No, it is a little higher than that' # Another block

# You can do whatever you want in a block ...

else:

print 'No, it is a little lower than that'

# you must have guess > number to reach here

print 'Done'

# This last statement is always executed, after the if statement is executed

raw_input() 函数是接受用户输入

不同之处一个是int整数类型的转换 另一个重要点是Python的语句块并不像C语系那样用{} 而是利用缩进 并且每个判断表达式后面跟进一个冒号,相当于C语系if语句的()

while语句

number=23

run=True

while run:

guess=int(raw_input('input a number:'))

if guess==number:

print 'congratulations'

run=False

elif guess>number:

print 'No,it is large'

else:

print 'No,it is little'

注意True False 的大小写

for循环

for i in range(1, 5):

print i

else:

print 'The for loop is over'

结果:

1 2 3 4 The for loop is over

for i in range(1, 5, 2):

print i

else:

print 'The for loop is over'

结果:

1 3 The for loop is over

Python中for循环使用range函数,该函数有两个重载,(开始位置,结束位置(不包括)) (开始位置,结束位置(不包括),步长)

break语句:略

continue语句

while True:

s=raw_input('input something')

if len(s)<5:

continue

print 'oh,yes it is longer than 5'

只有输入字符的长度大于5时才执行continue下面的语句
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: