[Python] Basic tutorial (4), Python variable type

Source: Internet
Author: User
Tags python list

PythonVariable Type

The value that the variable is stored in memory. This means that there is a space in memory when creating variables.

Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

Therefore, variables can specify different data types, which can store integers, decimals, or characters.

Assigning values to variables

Variable assignments in Python do not require a type declaration.

Each variable is created in memory and includes information about the identity, name, and data of the variable.

Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable. For example:

Assigning values to multiple variables

Standard data types

There are several types of data that can be stored in memory.

For example, a person's age can be stored in numbers, and his name can be stored in characters.

Python defines a number of standard types for storing various types of data.

Python has five standard data types:

    • Numbers (digital)
    • String (String)
    • List (lists)
    • Tuple (tuple)
    • Dictionary (dictionary)
Python numbers

Numeric data types are used to store numeric values.

They are immutable data types, which means that changing the numeric data type assigns a new object.

When you specify a value, the number object is created:

Python supports four different types of numbers:

    • int (signed integral type)
    • Long (longer integer [can also represent octal and hexadecimal])
    • Float (float type)
    • Complex (plural)

Instance

Examples of some numeric types:

    • Long integers can also use lowercase l, but it is recommended that you use uppercase L to avoid confusion with the number 1. Python uses L to display the long integer type.
    • Python also supports complex numbers, which are composed of real and imaginary parts, and can be represented by a + BJ, or complex (a, b), where both the real and imaginary part of a complex number are floating-point types.
Python string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores.

Generally recorded as:

s= "A1A2 An "(n>=0)

It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths
    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

If you want to implement a string to get a string, you can use the variable [header subscript: tail subscript], you can intercept the corresponding string, where the subscript is starting from 0, can be positive or negative, subscript can be null to take the head or tail.

Like what:

s = ' Ilovepython '

S[1:5] The result is love.

When using a colon-delimited string, Python returns a new object that contains the contiguous content identified with the offset, and the beginning of the left contains the bottom bounds.

The result above contains the value L of s[1], and the maximum range taken does not include the upper boundary, or the value p of s[5].

The plus sign (+) is a string join operator, and an asterisk (*) is a repeating operation. The following example:

Python list

The list is the most frequently used data type in Python.

A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and strings that can even contain lists (that is, nesting).

The list is identified by [], which is the most common type of composite data for Python.

The cut of the value in the list can also be used to the variable [head subscript: Tail subscript], you can intercept the corresponding list, from left to right index default 0, starting from right to left index default-1, subscript can be empty to take the head or tail.

The plus + is the list join operator, and the asterisk * is a repeating operation. The following example:

Python tuples

A tuple is another data type, similar to a list.

The tuple is identified with a "()". The inner elements are separated by commas. However, tuples cannot be assigned two times , which is equivalent to a read-only list.

The following are tuples that are not valid because tuples are not allowed to be updated. And the list is allowed to be updated:

#!/usr/bin/python#-*-coding:utf-8-*-tuple= ('Runoob',786,2.23,'John',70.2) List= ['Runoob',786,2.23,'John',70.2]tuple[2] = +# Illegal application of list[in tuples2] = +# is a legitimate application in the list
Python Dictionary

The Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.

The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

The dictionary is identified with "{}". A dictionary consists of an index (key) and a value corresponding to it.

Python Data type conversions

Sometimes, we need to convert the data-built type into the data type, and you just need to use the data type as the function name.

The following several built-in functions can perform conversions between data types. These functions return a new object that represents the value of the transformation.

[Python] Basic tutorial (4), Python variable 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.