Basic Data Types in Python

Basic Data Types in Python

  • 1. Numeric Types
    • (1) Integer
    • (2) Float
    • (3) Boolean
    • (4) Complex
  • 2. Strings
  • 3. Lists
  • 4. Tuples
  • 5. Sets
  • 6. Dictionaries

1. Numeric Types

(1) Integer

1. Integer: int In numbers, positive integers, 0, and negative integers are all referred to as integers. Example:

intvar = 1000
# Get data type
res1 = type(intvar)
print(res1)
# Get memory address
res2 = id(intvar)
print(res2)

Output:

Basic Data Types in Python

2. Binary Integer: Integers can also be represented in binary, and print automatically converts to decimal. Example:

intvar = 0b1010
print(intvar)

Output:

Basic Data Types in Python

(2) Float

1. Float: float Data that contains a decimal point is considered a float. Example:

floatvar = 3.14
print(floatvar, type(floatvar))

Output:

Basic Data Types in Python

2. Scientific Notation:

floatvar = 3.456e5 # Move decimal point 5 places to the right
print(floatvar, type(floatvar))

Output:

Basic Data Types in Python

(3) Boolean

Boolean: bool Boolean data has only two values: True and False, representing truth values. Example:

boolvar = True
print(boolvar, type(boolvar))

Output:

Basic Data Types in Python

(4) Complex

1. Complex: complex A complex number consists of a real part and an imaginary part; if there is an imaginary part, the data type is complex. 2. Representation Method One: Example:

complexvar = 3 - 91j
print(complexvar, type(complexvar))

Output:

Basic Data Types in Python

3. Representation Method Two: Syntax: complex(real, imaginary) Example:

complexvar = complex(3, -91)
print(complexvar, type(complexvar))

Output:

Basic Data Types in Python

2. Strings

1. String: str A string is enclosed in quotes. It can use single quotes, double quotes, or triple quotes. Example:

strvar1 = '123'
strvar2 = "123, number"
strvar3 = """ Escape characters: \ + character (1) Make meaningless characters meaningful (2) Make meaningful characters meaningless \n: newline \r\n: newline \t: indentation (tab, horizontal tab) \r: pull the string after \r to the beginning of the current line """
print(strvar1)
print(strvar2)
print(strvar3)

Output:

Basic Data Types in Python

2. Raw String: r + string ==> Represents a raw input string without escaping characters (equivalent to adding escape characters to all special characters in the string). Example:

strvar = r"E:\thabc_\nay"
print(strvar)

Output:

Basic Data Types in Python

3. Lists

1. List: list Define an empty list: listvar = [] Define a normal list:

listvar = [111, 3, 13, True, 3 + 4j, "abc"]
print(listvar, type(listvar))

Output:

Basic Data Types in Python

2. Positive Indexing: List elements are indexed from left to right: 0, 1, 2, 3… Example: Get values from the list

listvar = [111, 3, 13, True, 3 + 4j, "abc"]
res = listvar[2]
print(res)

Output:

Basic Data Types in Python

3. Negative Indexing: List elements are indexed from right to left: -1, -2, -3, -4, -5… Example: Get values from the list

listvar = [111, 3, 13, True, 3 + 4j, "abc"]
res = listvar[-4]
print(res)

Output:

Basic Data Types in Python

When the list is long, to get the last element, one method is to use negative indexing, another method is to use the len function to get the total length of the container type data. Example:

listvar = [111, 3, 13, True, 3 + 4j, "abc"]
res1 = len(listvar)
res2 = listvar[res1 - 1]
print(res2)

Output:

Basic Data Types in Python

4. Modifying Values in a List:

listvar = [111, 3, 13, True, 3 + 4j, "abc"]
listvar[1] = 123
print(listvar)

Output:

Basic Data Types in Python

5. Characteristics of Lists: Can be accessed, can be modified, ordered.

4. Tuples

1. Tuple: tuple Define an empty tuple: tuplevar = () Define a normal tuple: tuplevar = (False, 3 + 4j, “aaa”, 456)

Positive and negative indexing are the same as lists.

2. Accessing Elements in a Tuple: Example:

tuplevar = (False, 3 + 4j, "aaa", 456)
res = tuplevar[-3]
print(res)

Output:

Basic Data Types in Python

3. Can elements in a tuple be modified? Answer: Tuples do not support assignment. 4. Characteristics of Tuples: Can be accessed, cannot be modified, ordered. 5. Important Note about Tuples:

tuplevar = (123)
print(tuplevar, type(tuplevar))
tuplevar = (123,)
print(tuplevar, type(tuplevar))

Output:

Basic Data Types in Python

As shown, if no comma is added, the output result is an integer; if a comma is added, the output result is a tuple. Conclusion: A comma is necessary to define a tuple; the comma is the identifier that distinguishes whether it is a tuple. tuplevar = 1, 2, 3 is also a tuple.

5. Sets

1. Set: set (function: difference and complement) Define an empty set: setvar = set() Define a normal set:

setvar = {"123", "573", "Zhang San", "Li Si"}
print(setvar, type(setvar))

Output:

Basic Data Types in Python

2. Can elements in a set be accessed? Answer: No.

3. Automatic Deduplication: Example:

setvar = {"123", "123", "456", "567"}
print(setvar)

Output:

Basic Data Types in Python

As seen, sets have the function of automatic deduplication.

4. Characteristics of Sets: Unordered, cannot be modified, automatic deduplication.

6. Dictionaries

1. Dictionary: dict Stores data in key-value pairs. Syntax: {key1: value1, key2: value2, key3: value3…} Define an empty dictionary: dictvar = {} Define a normal dictionary: Example:

dictvar = {"a1": "123", "a2": "234", "b1": "345", "b2": "456"}
print(dictvar, type(dictvar))

Output:

Basic Data Types in Python

2. Accessing and Modifying Values in a Dictionary: Access: Get value by key

dictvar = {"a1": "123", "a2": "234", "b1": "345", "b2": "456"}
res = dictvar["a2"]
print(res)

Output:

Basic Data Types in Python

Modify: Similarly, modify value by key

dictvar = {"a1": "123", "a2": "234", "b1": "345", "b2": "456"}
dictvar["a2"] = "678"
print(dictvar)

Output:

Basic Data Types in Python

Leave a Comment