print() - 打印信息到控制台
print("Hello, World!")
len() - 计算长度
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # 输出: 5
type() - 获取类型
var = 10
print(type(var)) # 输出:
int(), float(), str()
num_str = "123"
num_int = int(num_str)
print(num_int) # 输出: 123
num_float_str = "123.45"
num_float = float(num_float_str)
print(num_float) # 输出: 123.45
num = 123
num_str_converted = str(num)
print(num_str_converted) # 输出: "123"
list(), tuple(), set()
my_tuple = (1, 2, 3)
my_list_from_tuple = list(my_tuple)
print(my_list_from_tuple) # 输出: [1, 2, 3]
my_list = [1, 2, 2, 3, 4]
my_set_from_list = set(my_list)
print(my_set_from_list) # 输出: {1, 2, 3, 4}
my_dict = dict([('a', 1), ('b', 2)])
print(my_dict) # 输出: {'a': 1, 'b': 2}
range(), sum(), min(), max()
numbers = range(1, 6)
print(sum(numbers)) # 输出: 15
print(min(numbers)) # 输出: 1
print(max(numbers)) # 输出: 5
input()
user_input = input("请输入您的名字: ")
print("你好," + user_input + "!")
round()
rounded_num = round(3.14159, 2)
print(rounded_num) # 输出: 3.14
enumerate()
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
zip()
names = ['Alice', 'Bob', 'Charlie']
ages = [24, 30, 22]
for name, age in zip(names, ages):
print(name, "is", age, "years old.")
sorted()
unsorted_list = [3, 1, 4, 1, 5, 9, 2, 6]
sorted_list = sorted(unsorted_list)
print(sorted_list) # 输出: [1, 1, 2, 3, 4, 5, 6, 9]
any(), all()
bool_list = [True, False, True]
print(any(bool_list)) # 输出: True
print(all(bool_list)) # 输出: False
bin(), oct(), hex()
decimal_number = 10
print(bin(decimal_number)) # 输出: 0b1010
print(oct(decimal_number)) # 输出: 0o12
print(hex(decimal_number)) # 输出: 0xA
chr(), ord()
char = chr(97) # ASCII code for 'a'
print(char) # 输出: a
unicode_point = ord('€')
print(unicode_point) # 输出: 8364
help() 和 dir()
# 查看列表对象的所有方法
print(dir(list))
# 获取具体方法的帮助信息,比如list的append方法
help(list.append)
input() 结合类型转换
age = int(input("请输入您的年龄: "))
print("您输入的年龄是:", age)
round() 处理浮点数
number = 3.14159
rounded = round(number, 3)
print(rounded) # 输出: 3.142
range() 在循环中的应用
for i in range(5):
print(i, end=' ')
# 输出: 0 1 2 3 4