[100 days proficient in python] Day3: python's basic data types and data type conversion

1. The basic data types of python

Here are some common examples and detailed explanations:

  1. Integer (int): Represents an integer value. For example:

    x = 5: Assign the integer value 5 to the variable x.

    y = -10: assigns the integer value -10 to the variable y.

  2. Floating-point number (float): represents a number with a fractional part. For example:

    pi = 3.14: Assign the floating point value 3.14 to the variable pi.

    price = 9.99: Assign the floating point value 9.99 to the variable price.

  3. Boolean (bool): A value representing True or False. For example:

    is_true = True: Assign the boolean value True to the variable is_true.

    is_false = False: Assign the boolean value False to the variable is_false.

  4. String (str): Represents text data, consisting of a series of characters. For example:

    name = "John": Assign the string "John" to the variable name.

    message = 'Hello, World!': Assign the string "Hello, World!" to the variable message.

  5. List (list): Represents an ordered mutable collection that can contain elements of different types. For example:

    numbers = [1, 2, 3, 4]: Creates a list containing integers.

    fruits = ["apple", "banana", "orange"]: Creates a list containing strings.

  6. Tuple: Represents an ordered immutable collection that can contain elements of different types. For example:

    point = (2, 3): Creates a tuple containing two integers.

    person = ("John", 25, "USA"): Creates a tuple containing strings and integers.

  7. Set (set): Represents an unordered collection of unique elements. For example:

    numbers = {1, 2, 3, 4}: Creates a collection containing integers.

    letters = {"a", "b", "c"}: Creates a collection containing characters.

  8. Dictionary (dict): represents a collection of key-value pairs. For example:

    student = {"name": "John", "age": 20, "grade": "A"}: Create a dictionary containing student information.

    prices = {"apple": 0.99, "banana": 0.5, "orange": 0.75}: Create a dictionary containing fruit prices.

These are examples of primitive data types commonly found in Python. By using these data types, you can work with various types of data and perform corresponding operations.

 The above sample codes for various data types are as follows

# 数字类型
num_int = 10
num_float = 3.14
num_complex = 2 + 3j

# 字符串类型
str_single_quotes = 'Hello, World!'
str_double_quotes = "I'm a Python programmer."
str_triple_quotes = '''This is a multi-line string.
It can span multiple lines.'''

# 列表类型
list_numbers = [1, 2, 3, 4, 5]
list_strings = ['apple', 'banana', 'cherry']
list_mixed = [1, 'two', 3.0, [4, 5]]

# 元组类型
tuple_numbers = (1, 2, 3, 4, 5)
tuple_strings = ('apple', 'banana', 'cherry')
tuple_mixed = (1, 'two', 3.0, (4, 5))

# 集合类型
set_numbers = {1, 2, 3, 4, 5}
set_strings = {'apple', 'banana', 'cherry'}

# 字典类型
dict_person = {
    'name': 'John',
    'age': 30,
    'city': 'New York'
}

# 布尔类型
bool_true = True
bool_false = False

# 空值类型
none_value = None

# 打印各种数据类型的值
print("数字类型:", num_int, num_float, num_complex)
print("字符串类型:", str_single_quotes, str_double_quotes, str_triple_quotes)
print("列表类型:", list_numbers, list_strings, list_mixed)
print("元组类型:", tuple_numbers, tuple_strings, tuple_mixed)
print("集合类型:", set_numbers, set_strings)
print("字典类型:", dict_person)
print("布尔类型:", bool_true, bool_false)
print("空值类型:", none_value)

The result of the operation is as follows:

Two python data type conversion

 When working with data in Python, it may be necessary to convert between different data types. The following are some common data type conversion sample codes:

1. Convert a string to an integer:

str_num = "123"
int_num = int(str_num)
print(int_num)  # 输出:123

2. Convert an integer to a string:

int_num = 123
str_num = str(int_num)
print(str_num)  # 输出:"123"

3. Convert the string to a float:

str_num = "3.14"
float_num = float(str_num)
print(float_num)  # 输出:3.14

4. Convert the floating-point number to an integer (truncate the decimal part):

float_num = 3.14
int_num = int(float_num)
print(int_num)  # 输出:3

5. Convert the list to a tuple:

my_list = [1, 2, 3]
my_tuple = tuple(my_list)
print(my_tuple)  # 输出:(1, 2, 3)

6. Convert the tuple to a list:

my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print(my_list)  # 输出:[1, 2, 3]

7. Convert the keys of the dictionary to a list:

my_dict = {"a": 1, "b": 2, "c": 3}
keys_list = list(my_dict.keys())
print(keys_list)  # 输出:['a', 'b', 'c']

Guess you like

Origin blog.csdn.net/qq_35831906/article/details/131716981
Recommended