2.变量类型-python笔记

变量赋值

  变量的本质是,开辟的一块内存,用来存放值。给变量赋值很容易:
#!/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

引号之间的字符,构成字符串。
可以用+将两个字符串连起来,用*使得字符串重复自身,变长。
    

另外,字符串的子串,可以用[:]提取出来。比如:
#!/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最通用的复合数据类型。看这段代码就明白。
#!/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
列表的元素用逗号隔开,是可以对某元素多次赋值的。

元组Tuple

元组用()标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
#!/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组成。
#!/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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值