Python initial basic data type

Source: Internet
Author: User


The data structure is divided into two parts:

1, Single value

Digital

Integer: If it is a 32-bit machine, the number of digits of the integer is 32 bits, the value range is -2**32 ~2**31-1, namely: -2147483547~-2147483548

If it is a 64-bit machine, the number of digits of the integer is 64 bits and the value range is -2**63~2**63-1

namely: -9223372036854775808~9223372036854775807

Long integer: Larger number

Floating-point type: 3.14

plural: z = a + BJ

Boolean value: True (1), False (0)


Example ① operation:


>>> i = 123

>>> Print type (i)

<type ' int ' >

>>> j = True

>>> Print Type (j)

<type ' bool ' >

>>> name = ' Alex '

>>> print type (name)

<type ' str ' >

>>>


String


placeholder%s and%d in Python, respectively, for string and number type placeholders, followed by the type of values to be populated must be characters and numbers


Example ①

>>> name = ' I am%s '% ' Tony '

>>> Print Name

I am Tony

>>> name001 = ' I am%s,age is%d '% (' Alex ', 30)

>>> Print name001

I am Alex,age is 30

>>>

Example ②

>>> name = ' I am {0},age is {1}, come from {2} '

>>> Name.format ("Alex", "China")

' I am alex,age ', come from China '

>>>



Summary: Python placeholders can be defined by% and {}, both of which are essentially the same, just in the form of a different expression. Python has three quotes indicating that the strings are single quotes ('), double quotation marks (""), three quotation marks ("" "" ""), where single and double quotation marks denote a single line of string, and three quotation marks can represent multiple lines of string


The index of the character:

Example ①

>>> name = "Alex"
>>> name[0] #----------> Gets the 1th character of the string
A
>>> name[1] #----------> Gets the 2nd character of the string
L
>>> name[2] #----------> Gets the 3rd character of the string
E
>>> name[3] #------------> Gets the 4th character of the string
' X '
>>> Name[4]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Indexerror:string index out of range
>>>

Character slices:

Example ②

>>> print Name[0:2] #-------> Get string 1th and 2nd characters
Al
>>> print Name[0:3] #--------> Get string 1th to 3rd characters
Ale
>>> print Name[1:3] #----------> Get string 2nd to 3rd characters
Le
>>>
>>> print Name[0:] #-----------> Get character 1th and back all, i.e. all characters
Alex
>>> print name[1:] #------------> Get string 2nd and back all, that is, from the 2nd start to the back all characters
Lex
>>> print name[-1] #------------> Gets the last character of a string, that is, from the end of the string
X
>>

Note: For string slices, such as name "N:m" (both N and m are integers), the meaning is that the left side contains the right side, that is, from Nth to (m-1) characters


to find the number of characters in a string (that is, how many characters are in a string):

Example ③

>>> Print Name
Alex

>>> print len (name)
4

>>>

If you want the last character of a string, you can use the following method:

>>> Print Name
Alex

>>>

>>> print name[-1] #------------> Print the subscript of the first character of a string
X

Or

>>> print Name[len (name)-1] #----> The value after subtracting 1 from the string length as the subscript for the last character of the string
X

>>>
Note: For a string, the last value of the index (that is, subscript) is 1 smaller than the length value of the string


Remove spaces from a string

Example ④

>>> name = "Alex"
>>> Print Name
Alex
>>>
>>> print Name.strip () #-------------> Remove spaces at both ends of a string
Alex
>>> print Name.lstrip () #-------------> Remove the space to the left of the string
Alex
>>> print Name.rstrip () #--------------> Remove the space to the right of the string
Alex
>>>


Segmentation of strings

Split Method: splits a string into a list according to the specified delimiter (', ', ' a ', ' \ t ')

Example ⑤

>>> names = "Alex,eric,tony"

>>> names.split (', ')

[' Alex ', ' Eric ', ' Tony ']

>>> names.split (' a ')

[' ', ' lex,eric,tony ']

>>> names.split (' t ')

[' Alex,eric, ', ' ony ']

>>>


String Append element method append, while the memory address of the string does not change

Example ⑥

>>> name_list.append (' seven ')

>>> name_list

[' Alex ', ' Seven ', ' Eric ', ' seven ']

>>>


>>> name_list.append (' Hello ')

>>> ID (name_list)

140546186766960

>>>


>>> name_list

[' Alex ', ' Seven ', ' Eric ', ' Seven ', ' Hello ']

>>>


Method of Deleting a string element del

Example ⑦


>>> name_list

[' Alex ', ' Seven ', ' Eric ', ' Seven ', ' Hello ']

>>> del Name_list[0]

>>> name_list

[' Seven ', ' Eric ', ' Seven ', ' Hello ']

>>> del Name_list[1]

>>> name_list

[' Seven ', ' seven ', ' Hello ']

>>>


If the string is modified, the memory space will be reassigned

Example ⑧


>>> List1 = "Good Evening everyone!"

>>> ID (list1)

140546186876528

>>> List1 = "Good Morning everyone!"

>>> ID (list1)

140546186876656

>>>


2. Collection

List

Lists are concatenated into strings through join methods, and the way you connect them can be defined by themselves

Example ①

>>> name_list

[' Seven ', ' seven ', ' Hello ']

>>> "-". Join (Name_list)

' Seven-seven-hello '

>>> "". Join (Name_list)

' Seven Seven Hello '

>>> "#". Join (Name_list)

' Seven#seven#hello '

>>>


Determines whether an element is in the list with---in, and returns a Boolean value (False or True)

Example ②


>>> name_list

[' Alex ', ' Seven ', ' Eric ', ' Seven ', ' Hello ']

>>> "Alex" in Name_list

True

>>> "Tony" in Name_list

False

>>> "Seven" in Name_list

True

>>>




Meta-group

The literal difference between tuples and lists is:

A, tuples are defined with parentheses (), and the list is in brackets []

B, elements inside the tuple are not modifiable, added, deleted, and the elements in the list are available for all previous operations

C, tuples, and lists can be sliced, indexed, length by Len, inclusive (in), loop

D, if the string has been modified, then it is necessary to reallocate memory space

Example ①

List: >>>name_tuple = (' Alex ', ' Seven ', ' Eric ', ' Seven ', ' Hello ')

Tuples: >>>name_list = [' Alex ', ' Seven ', ' Eric ', ' Seven ', ' Hello ']



Example ②

>>> List1 = ["Alex", (' Seven ', ' Eric ')]

>>> Print List1

[' Alex ', (' Seven ', ' Eric ')]

>>>

In List List1:

The first element is Alex, and the second element is: ( ' Seven ', ' Eric ')

These two elements can be modified, but the elements inside the second element (' seven ', ' Eric ') cannot be modified because the second element is a tuple, and then the manipulation of our operands changes.

We often say that the list can be added, deleted, changed and so on for the smallest unit

The smallest unit of a list is an element, and the smallest element of a tuple is also a component, but in List1, (' Seven ', ' Eric ') is a list, but it is also an element of list1, and we replace it with the whole (' seven ', ' Eric '), but not to (' Seven ', ' Eric ') inside the element for further modification.


Summary:

#切片, index, Len (), include, loop for STR, list, tuple

Str: if modified, you have to reassign the space, and the memory address changes .

list: after modification, the memory space address is unchanged

Tuple: do not allow modification

Dict: Dictionary, key-value pair, dictionary unordered, special for loop. Items (),

Keys ()----> list

Vlaues ()----> list

Items ()-------> only for loop use, assign elements to K, V (k and V can be defined by themselves)


For loop structure

For Ele in (list,str,tuple)

>>> for Ele in name_list:

... print Ele

...

Rick

Cool

Eric

Seven

Hello

>>>


>>> for Ele in Name_tuple:

... print Ele

...

Alex

Seven

Eric

Seven

Hello

>>>


>>> for ele in str:

... print Ele

...

D

A

F

A

G

A

E

G

A

H

G

>>>


[Email protected] scripts]# vim find.py

#!/usr/bin/env python

#-*-Coding:utf8-*-

Name_list = [' Alex ', ' Xiaoyueyue ', ' Wenqiang ']

For Ele in Name_list:

if ele = = "Alex":

Print "SB"

#本次循环不再继续执行

Continue

if ele = = "Xiaoyueyue":

Print "Find"

#

Break



[email protected] scripts]# python find.py

Sb

Find


While loop structure

While condition:

print ' 11111 '

If the condition after the while is true (that is, the Boolean value is true), the subsequent statement is executed all the time, as follows:

650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/75/9d/wkiom1y9-eezklrxaacv2tma7oe990.jpg "title=" While.png "alt=" Wkiom1y9-eezklrxaacv2tma7oe990.jpg "/>


Tip: If the while is false, then only the ① step will be executed, and later ② will not continue


Dictionary

A dictionary stores data in the form of a key: value pair, defined as a dictionary:

person = {

"Name": ' Alex ',

"Age": ' 18 ',

"Gender": "1",

}


Person.keys () #---> All keys

Person.values () #----> so the value

Person.items () #---> All key-value pairs (elements)


The values in the dictionary appear as pairs of keys.

For k,v in Person.items ():

Print K,v


[email protected] scripts]# python dict_test.py

Gender 1

Age 18

Name Alex

[Email protected] scripts]#

The order in which the prints were found is not the order in which they were stored, so the stored data in the dictionary is unordered .


For k,v in Person.intems ():

Print K #-------> in the form of a list

Print v #-------> in the form of a list

Print items () #------> all elements

This article from "Flat Light is true" blog, please be sure to keep this source http://cryan.blog.51cto.com/10837891/1710737

Python initial basic 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.