2.变量类型-python笔记
来源:互联网 发布:音乐系统cms 编辑:程序博客网 时间:2024/06/06 16:03
变量赋值
变量的本质是,开辟的一块内存,用来存放值。给变量赋值很容易:
#!/usr/local/bin/python2.7count1=100count2=1000.9count3="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/pythonstr = 'Hello World!'print str # 打印全字符串print str[0] # 打印第一个字符Hprint str[2:5] # 打印第三到第五个字符print str[2:] # 打印从第三个字符到末尾的所有字符print str * 2 # Prints string two timesprint str + "TEST" # Prints concatenated string
列表List
列表用[ ]标识。是python最通用的复合数据类型。看这段代码就明白。
#!/usr/bin/pythonlist = [ 'abcd', 786 , 2.23, 'john', 70.2 ]tinylist = [123, 'john']print list # Prints complete listprint list[0] # Prints first element of the listprint list[1:3] # Prints elements starting from 2nd till 3rd print list[2:] # Prints elements starting from 3rd elementprint tinylist * 2 # Prints list two timesprint list + tinylist # Prints concatenated lists列表的元素用逗号隔开,是可以对某元素多次赋值的。
元组Tuple
元组用()标识。内部元素用逗号隔开。但是元素不能二次赋值,相当于只读列表。
#!/usr/bin/pythontuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )tinytuple = (123, 'john')print tuple # Prints complete listprint tuple[0] # Prints first element of the listprint tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd elementprint tinytuple * 2 # Prints list two timesprint tuple + tinytuple # Prints concatenated lists
字典Dictionary
字典用{ }标识。字典由索引(key)和它对应的值value组成。
#!/usr/bin/pythondict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print dict['one'] # Prints value for 'one' keyprint dict[2] # Prints value for 2 keyprint tinydict # Prints complete dictionaryprint tinydict.keys() # Prints all the keysprint 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.
- 2.变量类型-python笔记
- 变量类型-python笔记
- Python自学笔记(一)变量类型
- Python学习笔记(二)变量类型
- Python学习笔记(变量类型)
- 【学习笔记】python的变量类型
- python变量以及类型(含笔记)
- Python笔记(二)--变量类型
- Python学习笔记(一)--变量类型
- Python 3.6笔记开篇-------普通变量类型
- Python笔记:打印,变量类型,类型转换,运算符
- Python学习笔记(四)----Python变量类型
- Python学习笔记之二 变量以及基本类型
- Python学习笔记-1(变量、类型、函数、类、作用域)
- Python学习笔记一 基本语法、变量类型、时间处理
- Python学习笔记(三)——变量类型
- Python学习笔记(二)-- 变量类型 + 运算符
- Python笔记(三)--变量类型之数字
- 一些常用Linux, Unix 命令
- 重写一个CCBI的加载类
- WCF和webservice的区别
- C++ tunerLib review 总结
- 在QC里,获取必填字段
- 2.变量类型-python笔记
- java学习之--poi celltype类型值
- iOS图片拉伸技巧
- salasafe
- TCP三次握手及四次挥手详细图解
- Java中ThreadLocal应用总结(1)
- Android之自定义Adapter的ListView
- poj 1258 最小生成树
- 外部表不是预期的格式 解决方案