Six big data types for Python learning 1,python

Source: Internet
Author: User
Tags truncated

#-*-Coding:utf-8-*-

Import keyword
Print ("Hello python!")
Print (keyword.kwlist) #查看python的关键字.
‘‘‘
#python的关键字有:
#[' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' break ',
# ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ',
# ' finally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ',
# ' is ', ' lambda ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ',
# ' return ', ' try ', ' while ', ' with ', ' yield ']
‘‘‘
#-----------
A=1
B=type (a) #取出变量a的数据类型.
Print (B==int)
C=tuple ([up]) #将列表类型转换为元组类型.
Print (Type (c))
#----------
‘‘‘
#python3的基本数据类型
#python3中有六个标准的数据类型:
#Number (number), string (string), list, tuple (tuple), sets (collection), Dictionary (dictionary).
#1. Number (numeric)
#python3支持int, Float,bool,complex (plural).
#在python3里, there is only one integer-type int, represented as a long integer, and no long in Python2.
The #type () function can be used to query the object type that the variable refers to.
‘‘‘
a,b,c,d=20,5.5,true,4+3j
Print (Type (a), type (b), type (c), type (d)) #d的类型为complex复数型.
"#此外, you can also use Isinstance to judge"
a=111
B=isinstance (A,int)
Print (B,type (b))
The difference between "#isinstance () and type () is: '
Class A:
Pass
Class B (A):
Pass
Print (Isinstance (a), a) #输出True
Print (Type (A ()) ==a) #输出True
Print (Isinstance (B (), A)) #输出True
Print (Type (B ()) ==a) #输出False
‘‘‘
#区别就是:
#type () does not consider a subclass to be a parent class type;
#isinstance () considers a subclass to be a parent class type.
#注:
#在python2中是没有布尔型的, which represents false with the number 0 and 1 for true.
# to Python3, true and false are defined as keywords, but their values are still 1 and 0, and they can be added to the numbers.
‘‘‘
A,b,c=1,2+true,3+false#true=1,false=0.
Print (A,B,C)
"#可以通过使用del语句删除单个或多个对象." For example: "'
Del A
Del B,c
"' #此时print (a,b,c) will error, Nameerror:name ' A ' is not defined. ‘‘‘
#数值运算
Print (5+4) #加法 with a result of 9
Print (5.2-2) #减法 with a result of 3.2
Print (3*7) #乘法 with a result of 21
Print (2/4) #除法 to get a floating-point number with a result of 0.5
Print (2//4) #除法 to get an integer with a result of 0
Print (9//4) #结果为2
Print (17%3) #取余 with a result of 2
Print (2**5) #乘方 with a result of 32
‘‘‘
#python可以同时为多个变量赋值, such as a,b=1,2.
#一个变量可以通过赋值指向不同类型的对象.
The #数值的除法 (/) returns a floating-point number that is rounded to use the "//" operator.
#在混合计算时, Python converts an integer to a floating-point number.
#-----------------------------------------------------------------------------------------
Numeric type instances
int:10,100,-786,080,-0490,-0x69
Float:0.0,15.20,-21.9,32.3+e18,-90.,-32.54e100,70.2-e12
COMPLEX:3.14J,45.J,9.322E-36J,.876J,-. 6545+0j,3e+26j,4.53e-7j
‘‘‘
Print ( -0x69) #结果为-105
‘‘‘
Python also supports complex numbers, which consist of real and imaginary parts, and can be represented by A+BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point.
‘‘‘
#-----------------------------------------------------------------------------------------
‘‘‘
#2. String (data type of string)
#字符串可以用 ' + ' operators are concatenated, repeating with the ' * ' operator.
‘‘‘
D= ' Nothing is impossible! '
Print (d) #输出d的所有字符
Print (d[0:-1]) #输出d的第一个到倒数第二个 all characters (not including the last one)
Print (d[0]) #输出d的第1个字符
Print (d[::-1]) #倒序输出d的所有字符
Print (D[::2]) #???
Print (D[2:5]) all characters #输出d的第三个到第五个 (not including sixth)
Print (d[2:]) #输出d的第三个到最后的所有字符
Print (d*2) #输出两遍d的所有字符
Print (d+ ' test ') #连接字符串
E= ' ABCDEFG '
Print (E[::2]) #每隔一个输出e的所有字符
‘‘‘
Output of #转义符 ' \ '
#反斜杠可以用来转义, using ' R ' allows the backslash to escape.
#另外, a backslash can also be used as a continuation character, indicating that the next line is a continuance of the previous row. You can also use the "" "" "" "" "" or "" "... \" to span multiple lines.
‘‘‘
Print (' Runoob ')
Print (' Ru\noob ')
Print (R ' Ru\noob ')
‘‘‘
#字符串的索引位置
#python中的字符串有两种索引方式, starting at 0 from left to right, starting from right to left with-1.
‘‘‘
word= ' python '
Print (word[3],word[5]) #输入变量word的第四个和第六个字符
Print (word[-1],word[-6]) #输出变量word的最后一个和倒数第六个字符
‘‘‘
#与C语言字符串不同的是, the Python string cannot be changed, and assigning a value to an index location, such as word[0]= ' m ', can result in an error.
#注意:
#1, backslashes can be used to escape, and R allows backslashes to escape.
#2, strings can be concatenated with the + operator and repeated with the * operator.
The strings in #3 and Python are indexed in two ways, starting from left to right and starting at 0 from right to left with-1.
The string in the #4, Python cannot be changed.
‘‘‘
#-----------------------------------------------------------------------------------------
‘‘‘
#3. List (lists)
#List (list) is the most frequently used data type in Python.
#列表可以完成大多数集合类的数据结构实现. The type of elements in a list can be different, it supports numbers, and strings can even contain lists (nested).
A comma-separated list of elements between #列表是写在方括号 ([]).
#和字符串一样, the list can also be indexed and truncated, and the list is truncated to return a new list containing the required elements.
#列表截取的语法格式如下:
# variable [head subscript, tail subscript]
#索引值以0为开始值, 1 is the starting position from the end.
The #加号 (+) is the list join operator, and the asterisk (*) is a repeating operation, as in the following example:
‘‘‘
l=[' ABCD ', 786,2.23, ' Runoob ', 70.2] #定义列表l
tinyl=[123, ' Runoob '] #定义列表tinyl
Print (L) #输出列表l的所有元素 with the result [' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Print (l[0]) #输出列表l的第一个元素, the result is ABCD
Print (L[1:4]) #输出列表l的第二个到第四个 (no fifth) element with the result [786, 2.23, ' Runoob ']
Print (l[2:]) #输出列表l的第三个到最后的所有元素 with the result [2.23, ' Runoob ', 70.2]
Print (L[::-1]) #倒序输出列表l的所有元素 with the result [70.2, ' Runoob ', 2.23, 786, ' ABCD ']
Print (L[::2]) #每隔一个输出列表l的所有元素 with the result [' ABCD ', 2.23, 70.2]
Print (l*2) #输出列表l两遍, results for [' ABCD ', 786, 2.23, ' Runoob ', 70.2, ' ABCD ', 786, 2.23, ' Runoob ', 70.2]
Print (l+tinyl) #连接列表l和列表tinyl, results for [' ABCD ', 786, 2.23, ' Runoob ', 70.2, 123, ' Runoob ']
Print (l*2+tinyl)
‘‘‘
#与python字符串不一样的是, the elements in the list can be changed.
#List内置了有很多方法, such as append (), Pop (), and so on, which is discussed later.
#注意:
#1, list is written between square brackets, and elements are separated by commas.
#2, like strings, lists can be indexed and sliced.
#3, list can be spliced using the + operator.
The elements in the #4 and list can be changed.
‘‘‘
‘‘‘
#4. Tuple (tuple)
#元组 (tuple) is similar to a list, except that elements of tuples cannot be modified.
#元组写在小括号 (()), the elements are separated by commas.
The element types in the @ tuple can also be different.
‘‘‘
t= (' ABCD ', 786, 2.23, ' Runoob ', 70.2)
tinyt= (123, ' Runoob ')
Print (t) #输出元组t的所有元素 with the result (' ABCD ', 786, 2.23, ' Runoob ', 70.2)
Print (t[0]) #输出元组t的第一个元素, the result is ABCD
Print (T[1:4]) #输出元组t的第二个到第四个 (no fifth) element, with the result (786, 2.23, ' Runoob ')
Print (T[::-1]) #倒序输出元组t的所有元素 with the result (70.2, ' Runoob ', 2.23, 786, ' ABCD ')
Print (t[2:]) #输出远处t的第三个到最后一个的所有元素 with the result (2.23, ' Runoob ', 70.2)
Print (T[::2]) #每隔一个输出元组t的所有元素 with the result (' ABCD ', 2.23, 70.2)
Print (t*2) #输出元组两遍 with the result (' ABCD ', 786, 2.23, ' Runoob ', 70.2, ' ABCD ', 786, 2.23, ' Runoob ', 70.2)
Print (T+TINYT) #连接元组t和元组tinyt with the result (' ABCD ', 786, 2.23, ' Runoob ', 70.2, 123, ' Runoob ')
‘‘‘
#元组和字符串类似, indexes can be indexed and indexes start at 0, and 1 is the position from the end. can also be intercepted.
#其实, a string can also be considered a special tuple.
‘‘‘
T= (1,2,3,4,5,6)
Print (t[1])
Print (T[2:5])
‘‘‘
#修改元组元素是非法的, for example, execution t[0]=11 will error, TypeError: ' Tuple ' object does not the support item assignment.
#虽然tuple的元素不可改变, but it can contain mutable objects, such as list lists.
#构造包含0个或者1个元素的元组比较特殊, the syntax format is as follows:
‘‘‘
t0= () #空元组
T1= () #一个元素, you need to add a comma after the element.
‘‘‘
#string, list, and tuple all belong to sequence (sequence).
#注意:
#1, as with strings, the elements of a tuple cannot be modified.
#2, tuples can also be indexed and sliced, in the same way.
#3, note the special syntax rules that construct tuples that contain 0 or 1 elements.
#4, tuples can also be spliced using the + operator.
‘‘‘
Import this
‘‘‘
#5. Set (collection)
A #集合 (Set) is a sequence of unordered, non-repeating elements.
#基本功能是进行成员关系测试和删除重复元素.
#可以使用大括号 {} or set () function to create a collection, note: Creating an empty collection must be set () instead of {}, because {} is used to create an empty dictionary.
‘‘‘
student={' Tom ', ' Jim ', ' Mary ', ' Tom ', ' Jack ', ' Rose '
student1={' Tom ', ' Jim ', ' Rose '}
Print (student) #输出集合student, duplicate elements are removed (no rules for output order)
#成员测试
M= ' Rose '
if (m in student):
Print (' m in the collection ')
Else
Print (' m not in collection ')
#set可以进行集合运算
A=set (' Abracadabra ')
B=set (' Alacazam ')
Print (A, b) #输出集合a和集合b with the result {' R ', ' C ', ' B ', ' d ', ' a '} {' C ', ' m ', ' Z ', ' l ', ' a '}
Print (A-B) #输出集合a与集合b的差集, the result is {' B. ', ' R ', ' d '}
Print (a|b) #输出集合a与集合b的并集 with the result {' R ', ' C ', ' B ', ' m ', ' Z ', ' l ', ' d ', ' a '}
Print (A&B) #输出集合a与集合b的交集 with the result {' C ', ' a '}
Print (A^B) #输出集合a与集合b中不同时存在的元素 (set-intersection) with the result {' R ', ' m ', ' B ', ' Z ', ' l ', ' d '}
Print (STUDENT-STUDENT1) #输出集合student与集合student1的差集 with the result {' Jack ', ' Mary '}
‘‘‘
#6. Dictionary (dictionary)
#字典 (dictionary) is another very useful built-in data type in Python.
#列表是有序的对象集合, a dictionary is a collection of unordered objects.
#两者的区别是: The elements in the dictionary are accessed by keys, not by offsets.
#字典是一种映射类型, the dictionary is identified with "{}", which is an unordered key (key): The value pair collection.
#键 (key) must use the immutable type.
#在同一个字典中, the keys (key) must be unique.
‘‘‘
di={} #创建空字典di
Print (DI)
di[' One ']= "1-Rookie Tutorial" #在字典di中创建key = ' one ', value= ' 1-Rookie tutorial ' key-value pair element.
Print (DI)
Di[2]= "2-Rookie tool" #在字典di中创建key = ' 2 ', value= ' 2-Rookie tool ' key-value pair element.
Print (DI) #输出字典di的所有元素
Print (di[' one ") #输出字典di中key = element of ' one '
Print (di[2]) #输出字典di中key = element of ' 2 '
tinydict={' name ': ' Google ', ' Code ': 1, ' site ': ' www.google.com '}
Print (tinydict)
Print (Tinydict.keys ()) #输出字典tinydict中所有的键
Print (Tinydict.values ()) #输出字典tinydict中所有的值
‘‘‘
#构造函数dict () You can build the dictionary directly from the key-value pair sequence as follows
‘‘‘
Dict1=dict (' biying ', 1), (' Google ', 2), (' Taobao ', 3)])
Print (DICT1)
Dict2={x:x**2 for x in (2,4,6,8)}
Print (DICT2)
Dict3=dict (runoob=1,google=2,taobao=3,baidu=4)
Print (DICT3)
‘‘‘
#另外, the dictionary type also has some built-in functions, such as clear (), keys (), value (), and so on.
#注意:
#1. A dictionary is a type of mapping whose elements are key-value pairs.
#2. Dictionary keywords must be immutable and cannot be duplicated.
#3. Create an empty dictionary using {}.
#Python数据类型转换
Data conversions require only the data type as a function name.
#以下几个内置的函数可以执行数据类型之间的转换. These functions return a new object that represents the value of the transformation.
#int (X[,base]) #将x转换为一个整数
#float (x) #将x转换到一个浮点数
#complex (Real[,imag]) #创建一个复数
#str (x) #将对象x转换为字符串
#repr (x) #将对象x转换为表达式字符串
#eval (str) #用来计算在字符串中的有效Python表达式 and returns an object.
#tuple (s) #将序列s转换为一个元组
#list (s) #将序列s转换为一个列表
#set (s) #转换为可变集合
#dict (d) #创建一个字典. D must be a sequence (key,value) tuple.
#frozenset (s) #转换为不可变集合
#chr (x) #将一个整数转换为一个字符
#unichr (x) #将一个整数转换为Unicode字符
#ord (x) #将一个字符转换为它的整数值
#hex (x) #将一个整数转换为一个十六进制字符串
#oct (x) #将一个整数转换为一个八进制字符串
‘‘‘
"""
#注释:
#1. single-line comment symbol #
#2. Multiline comment Character: Single quotation mark ("'), double quotation mark (\" ").
"""


Six big data types for Python learning 1,python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.