javapython自学教程_python学习 ---简明 Python 教程

字符串:

1,使用单引号(')==使用双引号(")==java双引号(")

2,使用三引号('''或""") ,可以合并多行

3,转义,与java的‘\’类似。注意(在一个字符串中,行末的单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行。)

4,自然字符串通过给字符串加上前缀r或R来指定。

5,字符串不可变,与java类似。

6,字符串会按字面意义级联。

变量:(与java类似)

对象:在python中任何东西都是对象

强烈建议:坚持在每个物理行只写一句逻辑行

python对缩进要求严格

运算符:与java类似。

流程控制:

if:

ifguess == number:

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

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

elifguess < 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

while:

whilerunning:

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

for :

foriinrange(1,5):

printi

break:

break语句是用来 终止 循环语句的,即哪怕循环条件没有称为False或序列还没有被完全递归,也停止执行循环语句。

一个重要的注释是,如果你从for或while循环中 终止 ,任何对应的循环else块将不执行。

continue:

continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后 继续 进行下一轮循环。

函数:

#!/usr/bin/python

# Filename: func_doc.py

defprintMax(x, y):

'''Prints the maximum of two numbers.

The two values must be integers.'''

x =int(x)# convert to integers, if possible

y =int(y)

ifx > y:

printx,'is maximum'

else:

printy,'is maximum'

printMax(3,5)

printprintMax.__doc__

模块:

import __name__==‘__main__’ 表示主程序

你可以使用内建的dir函数来列出模块定义的标识符。标识符有函数、类和变量。

数据结构:

数组:zoo = ('wolf','elephant','penguin'),zoo[1],zoo[0]

map:注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。

ab = {'Swaroop':'swaroopch@byteofpython.info',

'Larry':'larry@wall.org',

'Matsumoto':'matz@ruby-lang.org',

'Spammer':'spammer@hotmail.com'

}

ab[‘Larry’],len(ab)

list:

shoplist = ['apple','mango','carrot','banana']

shoplist.sort(),shoplist.append('rice')

list和数组十分类似,只不过数组和字符串一样是 不可变的 即你不能修改数组.

序列:list、数组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。

shoplist[1:3],shoplist[2:],shoplist[:],shoplist[-1],shoplist[0],这些返回的都是拷贝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值