变量类型-python笔记
来源:互联网 发布:windows 升级node版本 编辑:程序博客网 时间:2024/06/05 16:35
变量赋值
变量的本质是,开辟的一块内存,用来存放值。给变量赋值很容易:
- #!/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
- >>> 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 中删除元素
6.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']
>>> 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
元组用()标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
- #!/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.
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笔记
- 2.变量类型-python笔记
- Python自学笔记(一)变量类型
- Python学习笔记(二)变量类型
- Python学习笔记(变量类型)
- 【学习笔记】python的变量类型
- python变量以及类型(含笔记)
- Python笔记(二)--变量类型
- Python学习笔记(一)--变量类型
- Python 3.6笔记开篇-------普通变量类型
- Python笔记:打印,变量类型,类型转换,运算符
- Python学习笔记(四)----Python变量类型
- Python学习笔记之二 变量以及基本类型
- Python学习笔记-1(变量、类型、函数、类、作用域)
- Python学习笔记一 基本语法、变量类型、时间处理
- Python学习笔记(三)——变量类型
- Python学习笔记(二)-- 变量类型 + 运算符
- Python笔记(三)--变量类型之数字
- K3资料
- 流程控制--python笔记
- Add, Subtract, Multiply numbers without using "+", "-", "*" operation
- linux 文件权限详解(转)
- hdu 1247
- 变量类型-python笔记
- 链接脚本(1)
- 【§写意之黄色树叶win7主题§XTSC】
- iPhone跳转的动画效果类型及实现方法 CATransition
- 如何免费访问国外信息
- Hibernate学习之入门案例笔记
- 索引,索引的建立、修改、删除
- Live55 资料
- 如何发现一个合适的程序员