Python基础【day01】:Hello World程序(二)
本节内容
- 安装
- Hello World程序
- 变量
一、Python安装
windows
1
2
3
4
5
6
7
|
1 、下载安装包 https: / / www.python.org / downloads / 2 、安装 默认安装路径:C:\Users\Administrator\AppData\Local\Programs\Python\Python35 3 、配置环境变量 【右键计算机】 - - 》【属性】 - - 》【高级系统设置】 - - 》【高级】 - - 》【环境变量】 - - 》【在第二个内容框中找到 变量名为Path 的一行,双击】 - - > 【Python安装目录追加到变值值中,用 ; 分割】 如:原来的值;C:\Users\Administrator\AppData\Local\Programs\Python\Python35,切记前面有分号 |
linux、Mac
1
2
3
|
无需安装,原装Python环境 ps:如果自带 2.6 ,请更新至 2.7 |
二、Hello World程序
在linux 下创建一个文件叫hello.py,并输入
1
|
print ( "Hello World!" ) |
然后执行命令:python hello.py ,输出
1
2
3
|
localhost:~ jieli$ vim hello.py localhost:~ jieli$ python hello.py Hello World! |
指定解释器
上一步中执行 python hello.py 时,明确的指出 hello.py 脚本由 python 解释器来执行。
如果想要类似于执行shell脚本一样执行python脚本,例: ./hello.py
,那么就需要在 hello.py 文件的头部指定解释器,如下:
1
2
3
|
#!/usr/bin/env python print "hello,world" |
如此一来,执行: ./hello.py
即可。
ps:执行前需给予 hello.py 执行权限,chmod 755 hello.py
在交互器中执行
除了把程序写在文件里,还可以直接调用python自带的交互器运行代码,
1
2
3
4
5
6
|
localhost:~ jieli$ python Python 2.7 . 10 (default, Oct 23 2015 , 18 : 05 : 06 ) [GCC 4.2 . 1 Compatible Apple LLVM 7.0 . 0 (clang - 700.0 . 59.5 )] on darwin Type "help" , "copyright" , "credits" or "license" for more information. >>> print ( "Hello World!" ) Hello World! |
对比下其它语言的hello world






三、变量
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
声明变量
1
2
3
|
#_*_coding:utf-8_*_ name = "Alex Li" |
上述代码声明了一个变量,变量名为: name,变量name的值为:"Alex Li"
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
变量的赋值
1
2
3
4
5
6
7
8
|
name = "Alex Li" name2 = name print (name,name2) name = "Jack" print ( "What is the value of name2 now?" ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构