Iv. python data types

Source: Internet
Author: User

Python data type

Numbers, strings, lists, tuples, dictionaries


(1) Number type

---integer int range (-2147483648 to 2147483648)

>>> num1=123

>>> type (NUM1)

<type ' int ' >


---Long integer log out of int range

>>> num2=9999999999999999

>>> type (num2)

<type ' Long ' >

>>> num3=123l

>>> type (NUM3)

<type ' Long ' >

---floating-point float

>>> num4=12.0

>>> type (NUM4)

<type ' float ' >

---complex type complex

>>> num5=3.14j

>>> type (NUM5)

<type ' Complex ' >


(2) String type

A set of quotes (single quotes, double quotes, Sanchong quotes) that can protect numbers, letters, symbols (non-special system symbols)

>>> str1= ' Hello World '

>>> type (STR1)

<type ' str ' >

\ escape character, quotation marks must appear in pairs

>>> say= "Let ' s \" Go\ ""

>>> Print Sqy

\ n line break escape character

>>> say= "Let ' s \n\" go\ "\"

>>> print say

Let ' s

"Go"

Index values

>>> a= "ABCDE"

>>> A[0]+a[1]

' AB '

Slice take value

>>> A[1:4] (does not contain the 4th value)

' BCD '

>>> A[:4] (values from the beginning to the 4th bit)

' ABCD '

>>> A[::1] (step value is 1 value)

' ABCDE '

>>> A[::2] (step value is 2 value)

' Ace '

>>> A[-2:-4:-1] (reverse value)

' DC '


(3) Sequence: list, tuple, string

Two features of the sequence: index operator and slice operator

--The index operator fetches a specific item from the sequence

--The slice operator gets a slice of the sequence, which is part of the sequence

Basic operation of the sequence:

1. Len () to find the sequence length

>>> Len (a)

5

2. + Connect two sequences

>>> b= ' 12345 '

>>> a+b

' abcde12345 '

3. * Repeating sequence elements

>>> a*5

' Abcdeabcdeabcdeabcdeabcde '

4. In to determine if the element is in the sequence (the return value is true to indicate that it is contained, false means not included)

5. Max () returns the maximum value

6. Min () returns the minimum value

7. CMP (TUPLE1,TUPLE2) compares 2 sequence values (the return value is 0 means the same, and the return value is 1)

(4) tuples () tuple (immutable type of data)

Tuples and lists are very similar, except that tuples and strings are immutable, that is, you cannot modify tuples

--The tuple is defined by a comma-separated item in parentheses

--tuples are typically used when a statement or user-defined function is able to take a set of values completely, that is, the value of the tuple being used does not change

To create a tuple:

1. An empty tuple consists of a pair of empty parentheses

A= ()

2. Tuples that contain a single element

b= (1,)

3. General tuples

C= (A, B)

>>> t= ("Milo", "male")

>>> type (t)

<type ' tuple ' >

>>> T[0]

' Milo '

>>> name,age,sex=t

>>> Name

' Milo '

>>> Age

30

>>> Sex

' Male '


(5) Lists [] list (variable type of data)

A list is a data structure that handles a set of ordered items, that is, you can store a sequence of items in a list.

To represent a list, including multiple comma-separated numbers, or a string

List operations:

--Take value

~ Slices and indexes

~ list[]

--Add

~ List.append ()

--Delete

~ del (list[])

~ List.remove (list[])

--Modification

~ list[]=x

--Find

~ Var in list

Use objects and classes to better understand the list

Object = Property + method

A list is an example of using objects and classes

When you use the variable i and assign a value to it, such as an assignment integer 5, you can assume that you have created an object of class int I

Class also has a method, that is, simply define the ground function for the class

Only objects in that class can use these features

For example:

Python provides a Append method for list classes, which lets you add an item at the end of the list

Mylist.append (' an item ') adds a string to the list mylist. Note that using the dot number to use the object's method

Class also has variables, variables defined only for classes

These variables/names can be used only in objects of this class

Use a dot number, such as Mylist.field.

(6) Dictionary {}

Dictionary is the only mapping type (hash table) in Python

The Dictionary object is mutable, but the keys of the dictionary must use immutable objects, and different types of key values can be used in a dictionary.

Set a dictionary

dic={' name ': ' Loyu ', ' age ': all, ' gender ': ' Male '}

>>> dic[' name ']

' Loyu '

>>> dic[' age ']

90

>>> dic[' gender ']

' Male '

modifying values

>>> dic[' gender ']= ' 1234567890 '

>>> DIC

{' Gender ': ' 1234567890 ', ' age ': +, ' name ': ' Loyu '}

Delete

Del dic[' name '] removes the element with a key value of a in the dictionary

Dic.pop (' 90 ') Delete and return the element with the key ' 90 '

Dic.clear () Delete all elements of a dictionary

del dic Delete entire dictionary

Dic.keys () returns the list of keys in the dictionary

Dic.items () Returns a list of key-value pairs of tuples

>>> Dic.keys ()

[' Gender ', ' age ', ' name ']

>>> Dic.items ()

[(' Gender ', ' 1234567890 '), (' Age ', ' + '), (' Name ', ' Loyu ')]

This article from "Meteor Yu" blog, declined reproduced!

Iv. python data types

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.