python第一章

python第一章

python基本语法 输出语句print ("hello world") 变量print ("hello world") print ("hello" + " world") a = "hello" b = " world" print(a + b) 数学库 import math print(math.l...

hhehui217
May 31, 2026•2 min read

python基本语法

  1. 输出语句

    print ("hello world")

  2. 变量

    print ("hello world")
    print ("hello" + " world")
    a = "hello"
    b = " world"
    print(a + b)

2.1各种变量类型

# 定义变量
name = "小明"    # 字符串类型
age = 20        # 整数类型
height = 1.75   # 浮点数类型(小数)
is_student = True  # 布尔类型(True/False)

# 打印变量
print(name)
print(f"年龄: {age}, 身高: {height}")
  1. 数学库

    import math
    
    print(math.log2(8))
    a = 10
    b = 3
    
    print(f"{a} + {b} = {a + b}") #加法
    print(f"{a} - {b} = {a - b}") #减法
    print(f"{a} * {b} = {a * b}") #乘法
    print(f"{a} / {b} = {a / b}") #除法
    print(f"{a} % {b} = {a % b}") #取余
    print(f"{a} ** {b} = {a ** b}") #乘方

4.求解二次方程-x^2-2x+3=0

import math
a = -1
b = -2
c = 3
print((-b + math.sqrt (b ** 2- 4 * a * c))/(2 * a))
print((-b - math.sqrt (b ** 2- 4 * a * c))/(2 * a))

5.

Comments (0)