Python data type

Source: Internet
Author: User

First, data types in python

data types in Python include: Integer, Float, Boolean, String type

Integral type (int) and floating-point (float)

the integer type in Python is only int, no short,long; float float No Double

Eg:a = 20; b=-50;  c=0; d=2.093

Boolean type ( True/false)

Boolean type true and False in Python the first letter must be uppercase; nonzero is true

Common string manipulation

String also has a lot of built-in methods to manipulate strings, the common method is as follows, the following comment with whether or not, the return is a Boolean value:

Operation of the list

A list is an array, and you can store different types of data in the same list.

List1 = [1,2,3,4] # an ordinary array

List2 = [' Marry ', ' Lily ', [[Monkey ']] # Two- dimensional array

List3 = [' name ', ' sex ', [' Lily ', 124,[' aaaa ', ' BBB ']] # three-dimensional array

You can access the list by subscript and add, delete, change, and check the list as follows:


msg = ' Hello '
name = [' Liuli ', ' Yanghai ', ' Dahuang ']
#增
Name.append (msg) #添加新成员, and stitching to the last face of the list
Name.insert (1,msg) #在下表为1的位置上插入 ' Hello ', the other one to move backwards
Print (name)
#改, modify the specified element by subscript
name[2]= ' Sulie ' #修改指定位置的元素, no replace method
#查, the specified element is output by subscript
Print (name[0]) # Gets the first element
Print (Name[-1]) #-1 represents the last element
#删
Name.remove (' hello ') #删除指定的值, and can only delete one
Del Name[1] #删除指定位置的值
Name.pop () #不传入下标的话, remove the last value, and pass in the subscript to delete the value at the specified position
# name.clear () #清空列表
Print (name)

Slice, slice is another way to get the value of the list, it can get the elements of the specified list in bulk, it can be understood that, starting from the first few elements, to the end of the first few elements, get the value between them, the format is Name:[1:10]. Features: After the package is not wrapped, starting from 1 to the nineth element, a total of nine values are intercepted. Print (Name[2:6:2]) # intercept subscript from 2 to 6 End step 2

Meta-group

tuples In fact, the same as the list, not the same, the value of the tuple can not be changed, once created, it can no longer change, for example, to save the database connection information, the connection information in the program can not be changed, if the database is not connected, the program is finished Duzi, so that you can use the tuple , the tuple, but also prompted others, see is a tuple words, it means that this value can not be changed, the definition of the tuple is used (), parentheses, tuple only two methods, that is, count and index

#+++++++++++++++++ tuple ++++++++++
Mysql_coon = (' 192.168.1.109 ', ' root ', ' 123456 ', 3306, ' my_db ') # define Tuples
Mysql_coon = (1,2,3,4) #元组不可变更, but the variable that defines the tuple is a value that can be re-assigned
Print (Mysql_coon)

Dictionary

The dictionary is shaped like{Key:value}, throughKeytraversal. For example: Your work number can be used asKeyvalue, your personal information (list) is a very long and many types of data (name, age, job, contact ....) )。 Note:Keyvalue must be enclosed in quotation marks, andKeyvalues cannot be duplicated,valuecan repeateg:{'2045':[Wang Lei, at, automated test engineers,1753492922]}

#字典内置方法: Change, check
dic={' stu1 ': ' cc ', ' stu2 ': ' Andashu ', ' stu3 ': ' Niuniu '}
Print (Dic.values ()) #打印所有value
Print (Dic.keys ()) #打印所有的key
Print (Dic.setdefault (' stu1 ', ' Fengluo ')) #如果这个key存在的话, then do not move it, do not exist, add a
Dic2 = {' stu1 ': ' Sriba ', ' stu10 ': ' Baidu '}
Dic.update (DIC2) #更新字典值, if the key exists, it will be updated, if it does not exist, add print (Dic.items ()) #字典转换成一个list
dic={' stu1 ': ' cc ', ' stu2 ': ' Andashu ', ' stu3 ': ' Niuniu '}
#字典循环遍历
For K in DiC:
Print (K,dic[k]) #打印key和value的值, recommended this way, faster
For k,v in Dic.items ():
Print (K,V) #打印key和value的值, this is not recommended, because the dictionary will be converted into a list, inefficient

Python data type

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.