Python3.x:基础学习(1)-数据类型

Python有五种标准数据类型

1.数字
2.字符串
3.列表
4.元组
5.字典

1.数字

数字数据类型存储数字值。当为其分配值时,将创建数字对象。

num1 = 10
num2 = 20
>>> num1
10
>>> num2
20

可以使用del语句删除对数字对象的引用。 del语句的语法是

del num1[,num2[,num3[....,numN]]]]

可以使用del语句删除单个对象或多个对象。

del num1
del num1, num2
>>> num1
NameError: name 'num1' is not defined
>>> num2
NameError: name 'num2' is not defined

Python支持三种不同的数值类型 -

int(有符号整数)
float(浮点实值)
complex(复数)
Python3中的所有整数都表示为长整数。 因此,长整数没有单独的数字类型。

2.字符串

Python中的字符串被标识为在引号中表示的连续字符集。Python允许双引号或双引号。 可以使用片段运算符([]和[:])来获取字符串的子集(子字符串),其索引从字符串开始处的索引0开始,并且以-1表示字符串中的最后一个字符。

加号(+)是字符串连接运算符,星号(*)是重复运算符。

>>> str = 'csdn.net'
>>>
>>> print ('str = ', str)          	  # Prints complete string
str =  csdn.net 
>>> print ('str[0] = ',str[0])        # Prints first character of the string
str[0] =  c
>>> print ('str[2:5] = ',str[2:5])    # Prints characters starting from 3rd to 5th
str[2:5] =  dn.
>>> print ('str[2:] = ',str[2:])      # Prints string starting from 3rd character
str[2:] =  dn.net
>>> print ('str[-1] = ',str[-1])      # 最后一个字符,结果为:'!'
str[-1] =  t
>>> print ('str * 2 = ',str * 2)      # Prints string two times
str * 2 =  csdn.netcsdn.net
>>> print ('str + "TEST" = ',str + "TEST") # Prints concatenated string
str + "TEST" =  csdn.netTEST
>>>

3.列表

列表是Python复合数据类型中最多功能的。 一个列表包含用逗号分隔并括在方括号([])中的项目。在某种程度上,列表类似于C语言中的数组。它们之间的区别之一是Python列表的所有项可以是不同的数据类型,而C语言中的数组只能是同种类型。

存储在列表中的值可以使用切片运算符([]和[])来访问,索引从列表开头的0开始,并且以-1表示列表中的最后一个项目。 加号(+)是列表连接运算符,星号(*)是重复运算符。

>>> list = [ 'yes', 'no', 541 , 3.69, 'jiangsu', 77.9 ]
>>> list2 = [100,'jiangsu']
>>>
>>> print ('list = ', list)               # Prints complete list
list =  ['yes', 'no', 541, 3.69, 'jiangsu', 77.9]
>>> print ('list[0] = ',list[0])          # Prints first element of the list
list[0] =  yes
>>> print ('list[1:3] = ',list[1:3])      # Prints elements starting from 2nd till 3rd
list[1:3] =  ['no', 541]
>>> print ('list[2:] = ',list[2:])        # Prints elements starting from 3rd element
list[2:] =  [541, 3.69, 'jiangsu', 77.9]
>>> print ('list[-3:-1] = ',list[-3:-1])
list[-3:-1] =  [3.69, 'jiangsu']
>>> print ('list2*2 = ',list2*2)		  # Prints list two times
list2*2 =  [100, 'jiangsu', 100, 'jiangsu']
>>> print ('list + list2 = ',list + list2 )	# Prints concatenated lists
list + list2 =  ['yes', 'no', 541, 3.69, 'jiangsu', 77.9, 100, 'jiangsu']

4.元组

元组是与列表非常类似的另一个序列数据类型。元组是由多个值以逗号分隔。然而,与列表不同,元组被括在小括号内(())。

列表和元组之间的主要区别是 - 列表括在括号([])中,列表中的元素和大小可以更改,而元组括在括号(())中,无法更新。

元组可以被认为是只读列表。

>>> tuple = ( 'jiangsu', 345 , 3.55, 'nanjing', 44.1  )
>>> tuple2 = (888.0, 'jiangsu')
>>>
>>> print ('tuple = ', tuple)            	 # Prints complete tuple
tuple =  ('jiangsu', 345, 3.55, 'nanjing', 44.1)
>>> print ('tuple[0] = ', tuple[0])          # Prints first element of the tuple
tuple[0] =  jiangsu
>>> print ('tuple[1:3] = ', tuple[1:3])      # Prints elements starting from 2nd till 3rd
tuple[1:3] =  (345, 3.55)
>>> print ('tuple[-3:-1] = ', tuple[-3:-1])  # Prints elements starting from 2nd till 3rd
tuple[-3:-1] =  (3.55, 'nanjing')
>>> print ('tuple[2:] = ', tuple[2:])        # Prints elements starting from 3rd element
tuple[2:] =  (3.55, 'nanjing', 44.1)
>>> print ('tuple2 * 2 = ',tuple * 2)  # Prints tuple two times
tuple2 * 2 =  ('jiangsu', 345, 3.55, 'nanjing', 44.1, 'jiangsu', 345, 3.55, 'nanjing', 44.1)
>>> print ('tuple + tuple2 = ', tuple + tuple2) # Prints concatenated tuple
tuple + tuple2 =  ('jiangsu', 345, 3.55, 'nanjing', 44.1, 888.0, 'jiangsu')

5.字典

Python的字典是一种哈希表类型。它们像Perl中发现的关联数组或散列一样工作,由键值对组成。字典键几乎可以是任何Python数据类型,但通常为了方便使用数字或字符串。另一方面,值可以是任意任意的Python对象。

字典由大括号({})括起来,可以使用方括号([])分配和访问值。

>>> dict = {
    
    }
>>> dict['one'] = "This is one"
>>> dict[2]     = "This is my"
>>> dict2 = {
    
    'name': 'addr', 'code' : 1024, 'dept':'IT Dev'}
>>>
>>> print ("dict['one'] = ", dict['one'])       # Prints value for 'one' key
dict['one'] =  This is one
>>> print ('dict[2] = ', dict[2])          		# Prints value for 2 key
dict[2] =  This is my
>>> print ('dict2 = ', dict2)          		 	# Prints complete dictionary
dict2 =  {
    
    'name': 'addr', 'code': 1024, 'dept': 'IT Dev'}
>>> print ('dict2.keys() = ', dict2.keys())   	# Prints all the keys
dict2.keys() =  dict_keys(['name', 'code', 'dept'])
>>> print ('dict2.values() = ', dict2.values()) # Prints all the values
dict2.values() =  dict_values(['addr', 1024, 'IT Dev'])
>>>

猜你喜欢

转载自blog.csdn.net/u011727262/article/details/102653196
今日推荐