python learning variable types


A variable is a value stored in memory, which means that a space will be opened up in memory when a variable is created. Based on the data type of the variable, the interpreter allocates the specified memory and determines what data can be stored in memory.

1, Variable assignment

1. Single variable assignment

The assignment of variables in python does not require type declaration. The equal sign = is used to assign values to variables.

# -*- coding: UTF-8 -*-
counter = 100 # Assign integer variable
miles = 1000.0 # float 
name = "John" # character string
print counter
print miles
print name

2. Assignment of multiple variables

Python allows you to assign values to multiple variables at the same time.

a = b = c = 1

In the above example, create an integer object with a value of 1, and the three variables are allocated to the same memory space.
You can also specify multiple variables for multiple objects. As follows:

a, b, c = 1, 2, "john"

In the above example, two integer objects 1 and 2 are assigned to variables a and b respectively, and the string object "john" is assigned to variable c.

2, Standard data type

Python defines five standard data types for storing various types of data.

1. Figures

Digital data types are used to store values. They are immutable data types, which means that changing the digital data type will assign a new object.
When you specify a value, the Number object will be created:

var1 = 1
var2 = 10

You can delete references to one or more objects using the del statement:

del var
del var_a, var_b

Python supports four different number types:
A. int (signed integer)
B. Long (long integer, which can also represent octal and hexadecimal)
Long (long integer, which can also represent octal and hexadecimal). Long integer can also use lowercase L, but it is recommended that you use uppercase l to avoid confusion with digit 1. Python uses l to display long integers.
Long type only exists in Python version 2. X. in versions after 2.2, int type will be automatically converted to long type after data overflow. In Python 3. X, the long type is removed and replaced with int.
C. float (floating point)
D. Complex (complex)
Python also supports complex numbers. Complex numbers are composed of real and imaginary parts, which can be represented by a + bj or complex(a,b). Both real part a and imaginary part B of complex numbers are floating-point.

2. String

String data type is used to store text. It is a string of characters composed of numbers, letters and underscores.

The string list of python has two values:

  • The left to right index starts from 0 by default, and the maximum range is 1 less than the string length
  • The right to left index starts with - 1 by default, and the maximum range is the beginning of a string

String interception:
Use [head subscript: tail subscript] to intercept the corresponding string, where the subscript starts from 0 and can be positive or negative, and the subscript can be empty to get the head or tail. The substring obtained contains the characters of the header subscript, but does not contain the characters of the tail subscript. For example:

>>> s = 'abcdef'
>>> s[1:5]

String interception and connection:
Use the plus sign + to complete string splicing, while the asterisk * is repeated output. For example:

# -*- coding: UTF-8 -*-
str = 'Hello World!'
print str           # Output full string
print str[0]        # The first character in the output string
print str[2:5]      # The string between the third and sixth in the output string
print str[2:]       # Outputs a string starting with the third character
print str * 2       # Output string twice
print str + "TEST"  # Output connected string

Output results of the above examples:

Hello World!
llo World!
Hello World!Hello World!
Hello World!TEST

3. List

List, identified by [], is the most frequently used data type in Python and an ordered collection of objects.
List can complete the data structure implementation of most collection classes. It supports characters, numbers, strings, and even lists (that is, nesting).
The variable [head subscript: tail subscript] can also be used to cut the value in the list. The corresponding list can be intercepted. The left to right index starts from 0 by default, and the right to left index starts from - 1 by default. The subscript can be empty, indicating that the head or tail is taken.
Interception and connection of list:
Like strings, the plus sign + is used to complete string splicing, while the asterisk * is repeated output. For example:

# -*- coding: UTF-8 -*-
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print list               # Output full list
print list[0]            # The first element of the output list
print list[1:3]          # Output the second to third elements 
print list[2:]           # Outputs all elements from the third to the end of the list
print tinylist * 2       # Output list twice
print list + tinylist    # Print a list of combinations

Output results of the above examples:

['runoob', 786, 2.23, 'john', 70.2]
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['runoob', 786, 2.23, 'john', 70.2, 123, 'john']

4. Tuple

Tuples are identified by (). They are another data type, similar to List, but tuples cannot be assigned twice, that is, they cannot be updated, which is equivalent to a read-only List.

# -*- coding: UTF-8 -*-
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )
list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]
tuple[2] = 1000    # Illegal application in tuple
list[2] = 1000     # There are legal applications in the list

5. Dictionary

The dictionary is identified by {} and consists of an index (key) and its corresponding value value. Dictionary is the most flexible built-in data structure type in python except list. It is an unordered collection of objects. Dictionary elements are accessed by keys, not by offsets.

Use the following:

# -*- coding: UTF-8 -*-
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'runoob','code':6734, 'dept': 'sales'}
print dict['one']          # The output key is the value of 'one'
print dict[2]              # The output key is a value of 2
print tinydict             # Output complete dictionary
print tinydict.keys()      # Output all keys
print tinydict.values()    # Output all values

The output result is:

This is one
This is two
{'dept': 'sales', 'code': 6734, 'name': 'runoob'}
['dept', 'code', 'name']
['sales', 6734, 'runoob']

3, Data type conversion

For data type conversion, you only need to take the data type as the function name.
The following built-in functions can perform conversion between data types. These functions return a new object representing the converted value.

Tags: Python

Posted on Tue, 26 Oct 2021 22:22:09 -0400 by bickyz