Python Beginner-01 data types

Source: Internet
Author: User

Data type: Computer as the name implies (can do computing machine), the computer can not only handle the numerical value, but also processing pictures, text, audio, video, Web pages and other kinds of data, different data will define different data types.

Variables: Except that the memory location is used to store values, depending on the type of a variable, the interpreter allocates memory and determines how it is stored in memory.

Python variables are not mandatory types and can be assigned different types multiple times.

Python has five standard data types: numbers, strings, lists, tuples, dictionaries.

First, the number

There are four different numeric types: Int,long,float,complex complex (plural contains an ordered pair represented as a + BJ, where A is a real part, and B is the imaginary real floating-point number of a complex number.) )

Second, string

In Python, a string is determined to be a set of consecutive characters between quotes.

Python allows you to use the slice operator in any subset of single or double quote strings ([] and [:]), indexing the start and end of a string starting at 0 (-1). The string concatenation operator of the plus sign (+) symbol, while an asterisk (*) indicates a repeat operation. Third, List

The list is the most versatile Python composite data type. The list contains items that are separated by commas and that are contained in square brackets ([]). To some extent, the list resembles an array in C, and one of the differences between them is that all items that belong to one list can be of different data types.

Values stored in a list can be accessed using the slice operator ([] and [:]) with an index starting at 0, at the beginning of the list and ending with-1. plus sign (+) symbolic list concatenation operator, asterisk (*) repeat operation four, tuple

A tuple is a sequence data type similar to a list. A tuple of values separated by several commas. Unlike lists, tuples are enclosed in parentheses.

The main difference between a list and a tuple is that the list is enclosed in parentheses ([]) and their elements and sizes can be changed, while tuples are in parentheses () and cannot be updated. A tuple can be considered a read-only list. Five, dictionaries

A python dictionary is a hash table type. They are made up of key-value pairs, like associative arrays or hashes in Perl. A dictionary key can be almost any Python type, but it is usually a number or a string. Value can be an object of any python.

The dictionary is represented by curly braces ({}), which can be assigned values and are accessed with square brackets ([]). Instance:

#-*-coding:utf-8-*-
' Created on
2015-7-8

@author: Dong '
if __name__ = ' __main__ ':
    # string
    varstr = ' dong '
    varstr_ = ' dong '
    #数字
    varint = 1
    varlong = 1l
    varfloat =
    0.1 Varcomplex = 1+3j
    #列表
    varlist = [' Dong ', ' Dong ', 89,3000l,22+3j]
    #元组
    vartuple = (' Dong ', ' Dong ', 89,3000L,22+3J)
    #字典
    vardict = {' name ': ' Dong ', ' age ':-', ' Like ': 22+3j}
    ' '
    %s ' is a placeholder for the string, which the placeholder will then mention. Type () built-in function, determining function of type
    ' '
    print (' VARSTR data type:%s '%type (VARSTR) +--varstr_ data type:%s%type (varstr_))
    Print (' Varint data type:%s '%type (varint) +--varlong data type:%s "%type (varlong)
    +--varfloat data type:%s"%type (varfloat) + " --varcomplex data type:%s '%type (varcomplex))
    print (' varlist data type:%s '%type (varlist))
    print (' vartuple data type:%s ' %type (vartuple))
    print (' vardicti data type:%s '%type (vardict))

Run Result:

VARSTR data type: <type ' str ' >--varstr_ data type: <type ' str ' >
Varint data type: <type ' int ' >--varlong data type: <type ' long ' >--varfloat data type: <type ' float ' >-- Varcomplex data type: <type ' complex ' >
Varlist data type: <type ' list ' >
Vartuple data type: <type ' tuple ' >
Vardicti data type: <type ' dict ' >

There are also Boolean values, only true and false. Null value is represented by none

Data type Conversions

There are several built-in features that are converted from one data type to another. These functions return a new object that represents the converted value.

function Description

int (x [, Base])

Converts an X to an integer. Cardinality is specified as base, if X is a string.

Long (x [, Base])

Converts an X to a long integer. Cardinality is specified as base, if X is a string.

Float (x)

Converts an X to a floating-point number.

Complex (real [, Imag])

Create a complex number.

STR (x)

The Transform object x is a string representation.

REPR (x)

Object x into an expression string.

eval (str)

Computes a string and returns an object.

Tuple (s)

Converts s to a tuple.

List (s)

Convert s to a list.

Set (s)

Converts s to a set.

Dict (d)

Create a dictionary. D A required (key, value) tuple sequence.

Frozenset (s)

Converts s to a frozen set.

Chr (x)

Converts an integer to a character.

UNICHR (x)

Integer to a Unicode character.

Ord (x)

Converts a single character to an integer value.

Hex (x)

Converts an integer to a hexadecimal string.

Oct (x)

Converts an integer to a eight-binary string.



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.