变量类型-python笔记
2013-07-21 16:04
363 查看
变量赋值
变量的本质是,开辟的一块内存,用来存放值。给变量赋值很容易:[python]
view plaincopy
#!/usr/local/bin/python2.7
count1=100
count2=1000.9
count3="What the fuck."
print count1,'\n',count2,'\n',count3
连续赋同一个值:a=b=c=1,那么a b c共享同一块存储区,存储的值为1.
连续分别赋值:a,b,c = 2, 4, "hey"那就是三个不同变量。
基本数据类型
数字Numbers
赋值:val=20 val2=3.242等。不管是什么数值类型,直接赋值即可。删除赋值:del val,val2
python支持的四种数值类型:int long float complex。例子如下表
字符串String
引号之间的字符,构成字符串。可以用+将两个字符串连起来,用*使得字符串重复自身,变长。
另外,字符串的子串,可以用[:]提取出来。比如:
[python]
view plaincopy
#!/usr/bin/python
str = 'Hello World!'
print str # 打印全字符串
print str[0] # 打印第一个字符H
print str[2:5] # 打印第三到第五个字符
print str[2:] # 打印从第三个字符到末尾的所有字符
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
列表List
列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。[python]
view plaincopy
#!/usr/bin/python
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list # Prints complete list
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
3.向 list 中增加元素
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"])
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
4.搜索 list
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.index("example")
5
>>> li.index("new")
2
>>> li.index("c")
Traceback (innermost last):
File "<interactive input>", line 1, in ?
ValueError: list.index(x): x not in list
>>> "c" in li
False
列表的元素用逗号隔开,是可以对某元素多次赋值的。
5.从 list 中删除元素
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.remove("z")
>>> li
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("new")
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("c")
Traceback (innermost last):
File "<interactive input>", line 1, in ?
ValueError: list.remove(x): x not in list
>>> li.pop()
'elements'
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
6.list 运算符
>>> li = ['a', 'b', 'mpilgrim']
>>> li = li + ['example', 'new']
>>> li
['a', 'b', 'mpilgrim', 'example', 'new']
>>> li += ['two']
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
>>> li = [1, 2] * 3
>>> li
[1, 2, 1, 2, 1, 2]
元组Tuple
元组用()标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。[python]
view plaincopy
#!/usr/bin/python
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print tuple # Prints complete list
print tuple[0] # Prints first element of the list
print tuple[1:3] # Prints elements starting from 2nd till 3rd
print tuple[2:] # Prints elements starting from 3rd element
print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
字典Dictionary
字典用{ }标识。字典由索引(key)和它对应的值value组成。[python]
view plaincopy
#!/usr/bin/python
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
对字典dict,one 、2就是索引。而"This is one"和"This is two"就是相对应的值。
字典的元素是没有排序的。
dict.keys()返回所有key,dict.values()返回所有value值。
变量类型转换
int(x [,base])Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
float(x)
Converts x to a floating-point number.
complex(real [,imag])
Creates a complex number.
str(x)
Converts object x to a string representation.
repr(x)
Converts object x to an expression string.
eval(str)
Evaluates a string and returns an object.
tuple(s)
Converts s to a tuple.
list(s)
Converts s to a list.
set(s)
Converts s to a set.
dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
Converts s to a frozen set.
chr(x)
Converts an integer to a character.
unichr(x)
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
Converts an integer to a hexadecimal string.
oct(x)
Converts an integer to an octal string.
相关文章推荐
- Python学习笔记(二)Python的数据类型和变量
- Python3学习笔记-数据类型和变量
- Python学习笔记之二 变量以及基本类型
- A Byte of Python 笔记(2)基本概念:数、字符串、转义符、变量、标识符命名、数据类型、对象
- Python学习笔记--(一)变量和简单数据类型
- 学习笔记:Python基础 —— 数据类型和变量
- 廖雪锋笔记1---python变量类型
- Python3学习笔记——数据类型和变量
- Python笔记——基本语法:标识符、数据类型、变量、运算符及表达式/编码风格
- Python笔记(七)--变量类型之字典
- 【python学习笔记】4.数据类型和变量
- Python学习笔记-变量和数据类型
- Python3学习笔记1-变量,数据类型
- Python学习笔记-1(变量、类型、函数、类、作用域)
- python学习笔记2—python文件类型、变量、数值、字符串、元组、列表、字典
- Python学习笔记(一)—变量与数据类型
- 【python学习笔记】4.数据类型和变量
- Python笔记:打印,变量类型,类型转换,运算符
- 2.变量类型-python笔记
- 廖雪峰python3.6教程笔记3-Python基础之数据类型和变量