Python Basic data type

Source: Internet
Author: User

A data type

Numbers (shaping, long-shaped, floating-point, plural)

String

List

Meta-group

Dictionary

Collection

Two numbers (Int,float,complex)

Role: Age, grade, salary, Social security number, QQ number and other numbers related

Defined:

age=10 #本质age =int (10)

salary=3000.3 #本质salary =float (3000.3)

Binary, decimal, octal, hex

Long Plastic (Learn)

In Python2 (there is no concept of long shaping in Python3):

>>> num=2l
>>> type (num)
<type ' Long ' >

Plural (Learn)

>>> x=1-2j
>>> X.real
1.0
>>> X.imag
-2.0

Three strings

Role: Name, gender, nationality, address and other descriptive information

Definition: in single quotation marks \ double quotes \ Three quotation marks, consisting of a string of characters

Name= ' Egon '

Common operations:

removing blank Strip

Splitting split

Length Len

Index

Slice

Other operations (including common)

#strip Split---Split name='*egon**'Print (Name.strip ('*')) Print (Name.lstrip ('*')) Print (Name.rstrip ('*')) name='Root:x:0:0::/root:/bin/bash'Print (Name.split (':')) #默认分隔符为空格name='C:/a/b/c/d.txt'#只想拿到顶级目录print (Name.split ('/',1)) name='A|b|c'Print (Name.rsplit ('|',1)) #从右开始切分 # stitching: #jointag=' 'Print (Tag.join (['Egon','say','Hello',' World'])) #可迭代对象必须都是字符串 otherwise traverse #center,ljust,rjust,zfill,expandtabs Print related name='Egon'Print (Name.center ( -,'-')) Print (Name.ljust ( -,'*')) Print (Name.rjust ( -,'*')) Print (Name.zfill ( -)) #用0填充name='Egon\thello'print (name) print (Name.expandtabs (1)) #captalize, Swapcase,titleprint (Name.capitalize ()) #首字母大写print (Name.swapcase ()) #大小写翻转msg='Egon say hi'print (Msg.title ()) #每个单词的首字母大写 #replacename='Alex Say:i has one tesla,my name is Alex'Print (Name.replace ('Alex','SB',1)) #find, Rfind,index,rindex,count--Match Find name='Egon Say hello'Print (Name.find ('o',1,3)) #顾头不顾尾, if not found, returns-1 no error, found. Display index # print (Name.index ('e',2,4) #同上, but could not find the error print (Name.count ('e',1,3) #顾头不顾尾, find all test if you do not specify a range="12eioajflasfjoa"m= Str.maketrans ("AJF","456") print (Test.translate (m)) #format的三种玩法res='{} {} {}'. Format ('Egon', -,'male') Res='{1} {0} {1}'. Format ('Egon', -,'male') Res='{name} {age} {sex}'. Format (sex='male', name='Egon', age= -) #is其他print ('===>') name='egon123'print (Name.isalnum ()) #字符串由字母和数字组成print (Name.isalpha ()) #字符串只由字母组成print (Name.isidentifier ()) Print ( Name.islower ()) Print (Name.isupper ( )) print (Name.isspace ()) print (Name.istitle ())

Four list

Features: modifiable, memory does not need to be re-opened, can iterate

#Delete Way delList = ['1','5','2','3','4']delList[0]list.sort (Reverse=true)#DescendingList.remove ('2')#list.clear ()Print(list) Li= [A]list.append (LI)#[[1, 2, 3]]List.extend ([1,23])#[' 5 ', ' 4 ', ' 3 ', [1, 2, 3], 1, [+]Li.count (5) Li.insert (9,5) Li.insert (8,9)#the input position is out of bounds and will not be error #[' 5 ', ' 4 ', ' 3 ', [1, 2, 3, 5, 9], 1, at 1 [1, 2, 3, 5 , 9]Print(List,li.count (1), Li)
Five Yuan zu

Features: Can not be modified to add or remove the iteration list and Ganso can be converted to each other, the string can be Cheng Yuanju (after iteration), the meta-ancestor converted to a string (join or for)

Common operations:

Index

Slice

Cycle

Length

Contains in

Six dictionaries

A disorder (so the element cannot be found by slicing)

1, the value of the dictionary can be arbitrary

2. List cannot be used as key

3. Delete method: Del Dic[key], Dic.pop (key) D.clear ()

4, repeat can only keep one

5. Define the dictionary:

1) Dict.fromkeys (["K1,123," 999 "],123)

2) Dic.get (key) #不存在 None exists then returns the value corresponding to this key

3) dic.update (DIC1) #将字典 x all key-value pairs are added to dictionary D (not duplicated, duplicate key-value pairs are substituted in dictionary d for key-value pairs in dictionary x)

Python Basic data type

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.