Detailed parsing of data types for variables in Python

Source: Internet
Author: User
Tags python list
A variable is simply a reserved memory location used to store values. This means that when a variable is created, it retains some space in memory.

Depending on the data type of a variable, the interpreter allocates memory and determines how it can be stored in the reserved memory. So, by assigning variables of different data types, you can store integers, decimals, or characters in these variables.
Variable assignment:

Python's variables do not have to explicitly declare reserved memory space. When assigning a value to a variable, the declaration will automatically occur. equals sign (=) to assign a value to the variable.

operand = The left side of the operator is a variable, and the operand = the name of the right side of the operator stores the value in the variable. For example:

#!/usr/bin/pythoncounter =     # an integer assignmentmiles  = 1000.0    # A floating pointname  = "John"    # A Stringprint counterprint milesprint Name

Here, the assigned values 100,1000.0 and "John" are given to variables counter,miles and respectively respectively. When running this program, this will produce the following results:


Multiple assignments:

Python allows you to specify a value for several variables at the same time. For example:

A = b = c = 1

Here, the integer object creates a value of 1, and all three variables are assigned to the same memory location. You can also separate multiple objects into multiple variables. For example:

A, b, C = 1, 2, "John"

Here, two integer objects are assigned to variables A and B with values 1 and 2, and the string object with the value "John" is assigned to variable C.
Standard data types:

The data stored in memory can be of various types. For example, a person's age is stored as a numeric value and his address is stored as an alphanumeric character. Python is used to define the various standard types of operations for each person in the storage method.

Python has five standard data types:

    • Digital
    • String
    • List
    • Meta-group
    • Dictionary

Python numbers:

Numeric data types store values. They are immutable data types, which means changing the result value of a new assigned object's numeric data type.

When assigning a value to the object they create. For example:

var1 = 1VAR2 = 10

You can also use the DEL statement to delete some objects. The syntax for the DEL statement is:

Del Var1[,var2[,var3[....,varn] []]

You can also use the DEL statement to delete individual or multiple objects. For example:

Del Vardel var_a, Var_b

Python supports four different numeric types:

    • int (signed integer)
    • Long (longer integers [can also be represented in eight and hexadecimal])
    • Float (floating point real value)
    • Complex (plural)

For example:

Here are some examples of numbers:

Python allows you to use a lowercase l to denote long integers, but it is recommended that you use only one uppercase L to avoid the same as the number 1, and Python displays a long integer with a capital L.

The complex number contains an ordered pair represented as a + BJ, where A is the real part and B is the imaginary part real floating point number of the complex number.

Python string:

The string in Python is determined to be a contiguous set of characters between the quotation marks. Python is allowed in any pair of single or double quotation marks. Subset of strings, you can use the slice operator ([] and [:]) to index the beginning and end of a string starting at 0 (-1).

The string join operator for the plus (+) sign, and an asterisk (*) indicates a repeat operation. For example:

#!/usr/bin/pythonstr = ' Hello world! ' Print STR     # Prints complete Stringprint str[0]    # Prints first character of the Stringprint Str[2:5]   # Prints C Haracters starting from 3rd to 5thprint str[2:]   # Prints string starting from 3rd characterprint str * 2   # Prints string of Timesprint str + "TEST" # Prints concatenated string

This will produce the following results:

Hello world! Hllollo world! Hello world! Hello world! Hello world! TEST

Python list:

The list is the most versatile Python composite data type. The list contains items that are separated by commas and enclosed in square brackets ([]). To some extent, lists are similar to arrays in C, and one difference between them is that all items that belong to a list can be of different data types.

Values stored in a list can be accessed using the slice operator ([] and [:]) with the index starting at 0, at the beginning of the list and ending with-1. The plus sign (+) symbol lists the join operator, and the asterisk (*) repeats the operation. For example:

#!/usr/bin/pythonlist = [' ABCD ', 786, 2.23, ' John ', 70.2]tinylist = [123, ' John ']print list     # Prints complete LISTP Rint list[0]    # Prints first element of the Listprint List[1:3]   # Prints elements starting from 2nd till 3rd print List[2:]   # Prints elements starting from 3rd elementprint tinylist * 2 # Prints List of timesprint list + tinylist # Prints concatenated lists

This will produce the following results:

[' ABCD ', 786, 2.23, ' John ', 70.200000000000003]abcd[786, 2.23][2.23, ' John ', 70.200000000000003][123, ' John ', 123, ' John ' [' ABCD ', 786, 2.23, ' John ', 70.200000000000003, 123, ' John ']

Python tuples:

Tuples are similar to the series data types in a list. A tuple is a value separated by several commas. Unlike lists, however, 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. Tuples can be thought of as read-only lists. For example:

#!/usr/bin/pythontuple = (' ABCD ', 786, 2.23, ' John ', 70.2) Tinytuple = (123, ' John ') print tuple      # Prints complete Li Stprint tuple[0]    # Prints first element of the Listprint Tuple[1:3]   # Prints elements starting from 2nd till 3rd p  Rint tuple[2:]    # Prints elements starting from 3rd elementprint Tinytuple * 2  # Prints List of Timesprint tuple + Tinytuple # Prints concatenated lists

This will produce the following results:

(' ABCD ', 786, 2.23, ' John ', 70.200000000000003) ABCD (786, 2.23) (2.23, ' John ', 70.200000000000003) (123, ' John ', 123, ' John ') (' ABCD ', 786, 2.23, ' John ', 70.200000000000003, 123, ' John ')

The following is an invalid tuple because we are trying to update a tuple, which is not allowed. A similar operation is possible in the list:

#!/usr/bin/pythontuple = (' ABCD ', 786, 2.23, ' John ', 70.2) list = [' ABCD ', 786, 2.23, ' John ', 70.2]tuple[2] = 1000
  # Invalid syntax with tuplelist[2] = $   # Valid syntax with List

Python dictionary:

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

The dictionary is composed of curly braces ({}), which can be assigned a value and accessed with square brackets ([]). For example:

#!/usr/bin/pythondict = {}dict[' one '] = "This was one" dict[2]   = "This is the" and "tinydict = {' name ': ' John ', ' Code ': 6734, ' d Ept ': ' Sales '}print dict[' one ']    # Prints value for ' one ' keyprint dict[2]      # Prints value for 2 keyprint tinydict
  # Prints complete Dictionaryprint tinydict.keys ()  # Prints all the Keysprint tinydict.values () # Prints all the Val UEs

This will produce the following results:

This was Onethis is two{' dept ': ' Sales ', ' Code ': 6734, ' name ': ' John '}[' dept ', ' Code ', ' name ' [' Sales ', 6734, ' John ']

Dictionaries have the concept of element ordering. Its elements are unordered.
Data type conversions:

Sometimes, you might need to perform a conversion between built-in types. Conversions between types, simply use the class masterpiece as a function.

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

  • 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: 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.