Python学习笔记(五)--Python数据类型-数字及字符串

时间:2022-11-08 14:39:46

Python数据类型:
123和'123'一样吗?
>>> 123=='123'
False
>>> type(123)
<type 'int'>
>>> type('123')
<type 'str'>
>>>

Python中的数字类型有以下5种:
1.数字:
2.字符串:''
3.列表:[]
4.元组:()
5.字典:{}

代号依次为int() or float() or long(),'',[],(),{}
>>> a=int()
>>> b=''
>>> c=[]
>>> d=()
>>> e={}

>>> type(a),type(b),type(c),type(d),type(e)
(<type 'int'>, <type 'str'>, <type 'list'>, <type 'tuple'>, <type 'dict'>)

>>> f=float()
>>> g=long()
>>> type(f),type(g)
(<type 'float'>, <type 'long'>)

不同数据类型(主要是数字和字符串)不能相加减!!!
>>> 123+'123'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> 123+123.0
246.0
>>> 123+99999999999999999999999999999
100000000000000000000000000122L

raw_input--输出的是字符串,如果需要输入数字 则需要转换数据类型

int(raw_input())

++++++++
1.数字:+
++++++++
int整型(-2147483648<x<2147483647)

float浮点型
例如:0.0,12.0,-18.8,3e+7
示例:
>>> num=0.5
>>> type(num)
<type 'float'>
>>> num=2.0
>>> type(num)
<type 'float'>

long长整型
-范围很大,几乎可以说任意大的整数均可以存储。
-为了区分普通整数和长整数,需要在整数后加L或小写l
示例:
>>> g=2147483647
>>> type(g)
<type 'int'>
>>> g=2147483648
>>> type(g)
<type 'long'>

>>> a=123
>>> type(a)
<type 'int'>
>>> a=123L
>>> type(a)
<type 'long'>
>>> a=123l
>>> type(a)
<type 'long'>

复数型(complex)
-python对复数提供内嵌支持,这是其他大部分软件所没有的。
复数举例:
3.14j,8.32e-36j

>>> num=3.14j
>>> type(num)
<type 'complex'>
>>> num1=8.32e-36j
>>> type(num1)
<type 'complex'>

+++++++++++++
2.字符串:'' String
+++++++++++++
-使用引号定义的一组可以包含数字,字母,符号(非特殊系统符号)的集合。

Strval='This is a test!'
Strval="This is a test!"
Strval='''This is a test!'''
--主要就是引号(单引号,双引号,三重引号)的应用
三重引号(docstring)通常用来制作字符串,在面向对象时详解

>>> str1='Hello World'
>>> type(str1)
<type 'str'>
>>> str2="Hello World"
>>> type(str2)
<type 'str'>

--单引号和双引号定义的字符串其实是一样的,没有区别,因为输出的值最后都是单引号
>>> str1
'Hello World'
>>> str2
'Hello World'

>>> print str1
Hello World
>>> print str2
Hello World

在单双引号混用的时候:
1.成对出现
2.内单外双或内双外单
3.如果字符串内含单引号,则外面用双引号
详细可参考本人之前在博客园发表的一篇文章
Python中的引号用法总结 - 明媚如你。 - 博客园 http://www.cnblogs.com/helloworldcc/p/7243388.html

>>> say='Let's go'
File "<stdin>", line 1
say='Let's go'
^
SyntaxError: invalid syntax
>>> say="Let's go"
>>> say
"Let's go"
>>> say="Let's "go""
File "<stdin>", line 1
say="Let's "go""
^
SyntaxError: invalid syntax
>>> say="Let's \"go\"" #用反斜杠转义成普通符号来处理
>>> print say
Let's "go"
>>> say
'Let\'s "go"'

>>> mail = 'Tom:hello i am jack'
>>> print mail
Tom:hello i am jack
>>> mail = 'Tom:\n Hello\n I am jack' #转义字符\n表示换行
>>> print mail
Tom:
Hello
I am jack
>>>

>>> mail="""Tom:
... I am jack
... goobye
... """
>>> print mail
Tom:
I am jack
goobye

----用一对三双或三单引号来定义字符串,效果还是不错滴,可以保证格式化输出

>>> mail="""Tom:
... I am jack
... Goodbye
... """
>>> mail
'Tom:\n I am jack\n Goodbye\n '
>>> print mail
Tom:
I am jack
Goodbye
转义字符介绍文章:
Python 转义字符详细介绍_python_脚本之家 http://www.jb51.net/article/108990.htm
Python转义字符 - AllenW - 博客园 http://www.cnblogs.com/allenblogs/archive/2011/04/28/2031477.html

三重引号--注释或者制造字符串

>>> mail="""Tom:
... '''格式化输出’‘’
... How is everything going?
... I miss you very much.Do you miss me,too?
... Goodbye"""
>>> print mail
Tom:
'''格式化输出’‘’
How is everything going?
I miss you very much.Do you miss me,too?
Goodbye

Tips:
(1)单引号中可以使用双引号,中间的会当作字符串输出

(2)双引号中可以使用单引号,中间的会当作字符串输出

(3)三单引号和三双引号中间的字符串在输出时保持原来的格式。

交互模式下的注释:
>>> #换行录好了
... for i in range(10):
... print i
...
0
1
2
3
4
5
6
7
8
9

字符串的索引和切片:
按照索引取值(索引是从0开始的,切片不取结尾的,加号可以连接2个字符串):
>>> a='abcde'
>>> a[1]
'b'
>>> a[0]
'a'
>>> a[0:2]#切片,最多可以给3个参数:起始值,结束值,步长
'ab'
>>> a[0]+a[1]
'ab'
>>> a[:4]#表示从第一个字符开始切片,0-3(切片不取索引结尾值):abcd
'abcd'
>>> a[2:4]#取的是2,3索引对应的值即2+1,3+1
'cd'
>>> a[4:]#表示从第5个(索引为4)开始取
'e'
>>> a='abcdefghijk'
>>> a[0:6:2]
'ace'
>>> a[::2]
'acegik'
>>> a[::3]
'adgj'
>>> a[-1]#负数索引是从字符串的最右边往左取的
'k'
>>> a[-4:-1]#不包含-1索引对应的值即k
'hij'
========>
倒计时(需要导入time模块中的sleep函数)
>>> a = range(10,-1,-1)
>>> a
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

插入一个知识点--占位符%s:

>>> a="There are %s birds on the tree."%(25)
>>> print a
There are 25 birds on the tree.

>>> a="There are %s birds on the tree."%(raw_input('输入一个数字: '))
输入一个数字: 200
>>> print a
There are 200 birds on the tree.
>>>