Python學習筆記(五)--Python數據類型-數字及字符串


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM