资讯详情

第一篇:变量、数据类型与基础运算 / Variables, Data Types Basic Operations

📅 2026/10/30 9:58:41 | 华诺云谱 👁 阅读
第一篇:变量、数据类型与基础运算 / Variables, Data Types  Basic Operations
1.1 变量与赋值 / Variables and Assignment中文Python 是动态类型语言变量不需要声明类型赋值时自动推断。变量名区分大小写建议用蛇形命名法snake_case。English: Python is dynamically typed. Variables dont need explicit type declarations; the type is inferred at assignment. Names are case-sensitive, and snake_case is the recommended convention.python# 中文注释整数、浮点数、字符串、布尔值 # English comment: int, float, str, bool age 25 # int price 19.99 # float name Alice # str is_member True # bool print(type(age), type(price), type(name), type(is_member)) # class int class float class str class bool1.2 数据类型转换 / Type Conversion中文用int()、float()、str()显式转换类型。注意字符串转数字时内容必须合法否则抛ValueError。English: Useint(),float(),str()for explicit conversion. String-to-number conversion raisesValueErrorif the content isnt numeric.pythonnum_str 42 num_int int(num_str) # 字符串转整数 / str to int num_float float(3.14) # 字符串转浮点 / str to float text str(100) # 数字转字符串 / number to str print(num_int 1) # 43 print(num_float * 2) # 6.28 print(text 分) # 100分 # 非法转换会报错 / Invalid conversion raises an error try: int(abc) except ValueError as e: print(转换失败 / Conversion failed:, e)1.3 基础运算 / Basic Operators中文//是整除%是取余**是幂运算。字符串支持拼接和*重复。English://is floor division,%is modulo,**is exponentiation. Strings supportconcatenation and*repetition.pythona, b 10, 3 print(a b) # 13 加法 / addition print(a - b) # 7 减法 / subtraction print(a * b) # 30 乘法 / multiplication print(a / b) # 3.333... 真除法 / true division print(a // b) # 3 整除 / floor division print(a % b) # 1 取余 / modulo print(a ** b) # 1000 幂 / exponentiation print(Py thon) # Python print( * 10) # 1.4 字符串格式化 / String Formatting中文推荐使用 f-string它在字符串前加f花括号内可直接写表达式。English: f-strings are recommended. Prefix the string withfand put expressions directly inside braces.pythonname 小明 score 92.5 # f-string推荐 / recommended print(f{name} 的分数是 {score} 分 / {name}s score is {score}) # 保留两位小数 / keep two decimal places print(f{name}: {score:.2f}) # 旧式方法 / legacy methods print({} 得分 {}.format(name, score)) print(%s 得分 %.1f % (name, score))1.5 练习 / Exercise中文编写程序输入圆的半径输出周长和面积保留两位小数。English: Write a program that takes a circles radius and outputs circumference and area (two decimal places).pythonimport math radius float(input(请输入半径 / Enter radius: )) circumference 2 * math.pi * radius area math.pi * radius ** 2 print(f周长 / Circumference: {circumference:.2f}) print(f面积 / Area: {area:.2f})github.0voice
📝

华诺云谱内容团队

资深建站顾问 · 行业研究员

10年+企业数字化服务经验,专注智能建站、SEO优化与品牌营销,持续输出建站技巧、行业洞察与营销干货,已帮助5000+企业实现数字化增长。

你可能需要的服务

订阅华诺云谱资讯周报

每周一封,精选建站技巧、SEO与营销干货,直达邮箱。已有 8,000+ 企业主订阅,助你少走弯路。

↑