python学习笔记(一)

声明:以下内容是整理于虫师的博客,记载了自己需要的知识点。

字符串表示,str 和 repr

所有通过python打印的字符串是被引号括起来的。这是因为python打印值的时候会保持该值在python代码中的状态,而不是你希望用户所看到的状态。如果用print语句,结果就不一样了:

>>> "hello,world!"
'hello,world!'
>>> 10000L
10000L

>>> print "hello,world!"
hello,world!
>>> print 10000L

可以看到,长整型数10000L被转换成了数字10000 ,而且在显示给用户的时候也如此。 我们在这里讨论的实际上是值被转为字符的两种机制。可以通过以下两个函数来使用这两种机制:

>>> print str("hello,world!")
hello,world!
>>> print str(10000L)

>>> print repr("hello,world!")
'hello,world!'
>>> print repr(10000L)
10000L

str()函数 ,它会把值转换为合理形式的字符串,以例用户可以理解;
repr()函数,它会创建一个字符串,它以合法的python表达式的形式来表示值。

input 和 raw_input 的比较

首先看一段代码:

>>> name = input("what is your name?")
what is your name?huhu

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    name = input("what is your name?")
  File "<string>", line 1, in <module>
NameError: name 'huhu' is not defined

>>> name = input("what is name?")
what is name?"huhu"
>>> print "hello, " + name + " !"
hello, huhu !

input()函数会假设用户输入的是合法的python表达式。所以直接输入huhu 系统会提示错误,但是如果加上引号(“huhu”)就会是一个合法的字符,程序运行是没有问题的。
然而,要求用户带着引号输入他们的名字有点过份,因此,就这需要使用raw_input函数

>>> name = raw_input("what is name?")
what is name?huhu
>>> print name
huhu

当然input有特别的需要,比如要求用户输入数字时。

>>> input("enter a namber:")
enter a namber:3
>>> raw_input("enter a namber:")
enter a namber:3
'3'

乘法

>>> 'python ' * 5
'python python python python python '
>>> [25] * 10
[25, 25, 25, 25, 25, 25, 25, 25, 25, 25]

如果想创建一个占用十个元素空间,却不包括任何有用的内容的列表,可以用None

>>> sequence = [None] * 10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]

序列(字符串)乘法示例:

# 以正确的宽度在居中的“盒子”内打印一个句子

# 注意,整数除法运算符(//)只能用在python 2.2 以及后续版本,在之前的版本中,只能用普通除法(/)

sentence = raw_input("Sentence : ")

screen_width = 80
text_width = len(sentence)
box_width = text_width + 6
left_margin = (screen_width - box_width) //2

print
print ' ' * left_margin + '+' + '-' * (box_width - 2)+ '+'
print ' ' * left_margin + '|  ' + ' ' * text_width    + '  |'
print ' ' * left_margin + '|  ' +     sentence        + '  |'
print ' ' * left_margin + '|  ' + ' ' * text_width    + '  |'
print ' ' * left_margin + '+' + '-' * (box_width - 2)+ '+'

---------------------------------------------------------
输入:
>>> 
Sentence : haha! this is my box
输出:

                           +------------------------+
                           |                        |
                           |  haha! this is my box  |
                           |                        |
                           +------------------------+

列表

分片
如果求10个数最后三个数:

>>> numbers = [0,1,2,3,4,5,6,7,8,9]
>>> numbers[7:-1]   # 从第7个数到 倒数第一个数
[7, 8]              #显然这样不可行
>>> numbers[7:10]   #从第7个数到第10个数
[7, 8, 9]            #这样可行,索引10指向的是第11个元素。
>>> numbers[7:]    # 可以置空最后一个索引解决
[7, 8, 9]
>>> numbers[:3]   
[0, 1, 2]
>>> numbers[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

分片赋值

>>> name = list('huzi')
>>> name
['h', 'u', 'z', 'i']
>>> name[2:]=list('dazhi')
>>> name
['h', 'u', 'd', 'a', 'z', 'h', 'i']

List函数
List函数可以将一个字符串拆分成列表。

>>> list('chongshi')
['c', 'h', 'o', 'n', 'g', 's', 'h', 'i']

删除元素
从列表中删除元素也很容易,使用del语句来实现。

>>> names = ['zhangsan','lisi','wangwu','sunliu']
>>> del names[2]
>>> names
['zhangsan', 'lisi', 'sunliu']

列表方法
1.append
append方法用于在列表末尾追加新的对象:

>>> abc = [1,2,3]
>>> abc.append(4)
>>> abc
[1, 2, 3, 4]

2.count
count方法统计某个元素在列表中出现的次数:

>>> ['to','be','or','not','to','be',].count('to')
2
>>> x = [[1,2],1,1,[2,1,[1,2]]]
>>> x.count(1)
2
>>> x.count([1,2])
1

3.extend
extend方法可以在列表的末尾一次性追加另一个序列中的多个值。用新列表扩展原有列表:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]

4.index
index 方法用于从列表中找出某个值第一个匹配项的。 

>>> knights = ['We','are','the','kninghts','who','say','ni']
>>> knights.index('who')
4
>>> knights[4]
'who'

5.insert
insert 方法用于将对象插入到列表中:

>>> numbers = [1,2,3,5,6,7]
>>> numbers.insert(3,'four')
>>> numbers
[1, 2, 3, 'four', 5, 6, 7]

6.pop
pop 方法会移除列表中一个元素(默认是最后一个),并且返回该元素的值:

>>> x = [1,2,3]
>>> x.pop()
>>> x
[1, 2]
>>> x.pop(0)
>>> x
[2]

7.remove
remove 方法用于移除列表中某个值的第一个匹配项:

>>> x = ['to','be','or','not','to','be']
>>> x.remove('be')
>>> x
['to', 'or', 'not', 'to', 'be']

8.reverse
reverse方法将列表中的元素反向存放

>>> x = [1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]

9.sort
sort方法用于在原位置对列表进行排序。在“原位置排序”改变原来的列表,从而让其中的元素能按一定的顺序排列。

>>> x = [4,6,2,1,7,9]
>>> x.sort()
>>> x
[1, 2, 4, 6, 7, 9]

元组:不可变序列

元组与列表一样,也是一种序列。唯一的不同是元组不能改变。创建元组的语法很简单:如果你用逗号分割了一些值,那么你就自动创建了元组。

>>> 1,2,3
(1, 2, 3)
>>> (1,2,3)  # 用括号表示元组
(1, 2, 3)
>>> ()  # 空元组
()

如何实现包含一个值的元组呢?方法有点奇特—-必须加逗号,即使只有一个值:

>>> 42,
(42,)
>>> (42,)
(42,)
>>> 3 * (20+1)
>>> 3 * (20+1,)
(21, 21, 21)

tuple 函数
tuple函数的功能与list函数基本上一样:以一个序列作为参数并把它转换为元组。

>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('abc')
('a', 'b', 'c')
>>> tuple((1,2,3))
(1, 2, 3)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值