Python -- basic data type (variable / immutable type)


Python -- basic data type

Assignment format:

  • Variable name assignment symbolic value

How to view data types:

  • type( )

1. Integer int

Integer is generally used to store integers, etc

#int instance
age = 18
num = 100

2. Floating point float

Floating point type is generally used to store decimals, etc

#float instance
NUM = 3.14
num = 1.456789

Note: integer and floating point types can be called number types, which are mainly used for mathematical operations, type conversion, etc

3. String str

String format

Strings are mainly used to record descriptive data

Definition: strings need to be enclosed in quotation marks, single quotation marks, double quotation marks and three quotation marks (three quotation marks without assignment symbol are comments)

String definition format:
#Empty string
s1 = ''
#Non empty string
 Mode 1: #Single quotation mark
num = 'one two three'

Mode 2:  #Double quotation mark
num = "one two three"

Mode 3:  #Three quotation marks: three single quotation marks
num = '''one  two  three'''
num = """one two three"""

String nesting

Why are there so many kinds of single quotation marks, double quotation marks or three quotation marks? If you need to use quotation marks multiple times in a string, you should pay attention to the positions of the beginning and end of quotation marks. Examples are as follows:

#Error demonstration
word = 'The teacher said:'learn python You can get a high salary.''

The diagram is as follows:

In this way, the python interpreter will think that the string has only 'the teacher says' and the following empty string'.

#Correct demonstration, quotation marks nested
word = 'The teacher said:"learn python You can get a high salary."'
word1 = "The teacher said:'learn python You can get a high salary.'"

So there's no problem. Pay attention to the front and back positions of quotation marks!

4. List list

  • The list is used to store multiple data and can easily take out any number.

  • Methods of getting elements: index and slice

Definition: the list is enclosed in square brackets. Multiple elements can be stored inside. The elements are separated by commas. The element type can be any data type.

List definition format:
#Empty list
lst1 = []

#Non empty list
lst2 = [1,2,3,'python']
lst3 = [1,2,3,'python',['java','php','c++']]

Subscript position of list element

Index and slice: string, list, common

Examples of sub index values in the list are as follows:

There are two ways to index: positive index and negative index
 According to the position of the element, decide which method to use. Use a positive index at the front of the list and a negative index at the end

# Index value, python
lst2 = [1, 2, 3, 'python']
print(lst2[3])	#just
print(lst2[-1])	  #negative
# Nested values, take out java
lst3 = [1, 2, 3, 'python', ['java', 'php', 'c++']]

The principle of slice value is left closed and right open, excluding the last bit. Examples of slice value are as follows:

#Take out elements of numeric type, 123456789
lst4 = [123,456,789,'name','age','hobby']
print(lst4[0:3]) #The fourth element is not included, so the third 789 is taken

5. Dictionary dict

Dictionaries can store data more accurately in the form of key value pairs

Definition: the dictionary is enclosed in braces / curly braces. It can store multiple elements. Elements are separated by a comma. The corresponding format is KV key value pair, {k:v}.

  • explain:
    • K is information about the descriptive nature of V (usually a string)
    • V is real data, equivalent to the value of a variable, and can be any data type
# Dictionary instance:
#Empty dictionary
dic = {}
#Non empty dictionary
dic1 = {'name': 'HammerZe', 'age': 18, 'hobby': 'girl'}
#The dictionary cannot use index value. It can only be obtained through K

6. Boolean ball

Boolean value is used to judge the right or wrong of things. There are only two states: True and False

Special attention! Boolean variable names are generally defined starting with is!

Examples are as follows:

is_right = True
is_delete = False
is_alive = True

  • Data type conversion

    In python, all data types can be converted to Boolean values
    Boolean values of False are:
    0,None, '', [], {} ······, all other cases are True

7. Tuple tuple

Tuples are almost the same as lists, but they are immutable, and the elements in lists are mutable

Definition: enclosed in parentheses, multiple elements can be stored. Elements are separated by commas. Elements do not support modification

#Examples are as follows:
#Empty tuple
tup = ()
#Non empty tuple
tup1 = (1, 2, 3, 4, 5, 6,)
tup2 = (1, 2, 3, 4, 5, 6)
print(tup1 == tup2) #The result is True, and it is OK to have one more comma after it

8. Set set

Sets can be de duplicated and relational operations

Definition: enclosed by braces / curly braces, multiple elements can be stored. Elements are separated by commas, which is different from dictionaries.

#Examples are as follows
#Empty set
s = set()
# Non empty set
s1 = {1, 2, 3, 4, 5, 6, 7}

Immutable data type and variable data type in python

Method to view memory address: id()

How to distinguish between variable and immutable

The address changes after the immutable data type is changed, and the address does not change after the variable data type is changed

  • Immutable data type: when the value of the corresponding variable of the data type changes, its corresponding memory address will also change. For this data type, it is called immutable data type.
  • Variable data type: when the value of the corresponding variable of the data type changes, its corresponding memory address does not change. For this data type, it is called variable data type.

Use a table to illustrate:

data type Variable / immutable
integer Immutable
character string Immutable
tuple Immutable
list variable
aggregate variable
Dictionaries variable

Examples are as follows:

#Take the variable and the immutable as examples:
a = 1
a = 2

1354793072 <class 'int'>
1354793104 <class 'int'>

#The memory address has changed, so it is an immutable type

lst = [1, 2, 3, 4, 5]
print(lst, id(lst), type(lst))
lst[0] = 'one'
print(lst, id(lst), type(lst))

[1, 2, 3, 4, 5] 2616978892744 <class 'list'>
['one', 2, 3, 4, 5] 2616978892744 <class 'list'>
#The memory address has not changed, so it is of variable type

After reading the example, do you understand the principle of variable and immutable!

Tags: Python

Posted on Thu, 04 Nov 2021 06:59:32 -0400 by xylex