python基础
安装python3环境
1 2 3 4 5 6 7 8 9 10 11 | yum -y install zlib-devel bzip2 -devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel yum -y install gcc g++ mkdir /usr/local/python3 tar xf Python-3.7.3. tar .xz -C /usr/local/python3/ cd /usr/local/python3/Python-3 .7.3/ . /configure --prefix= /usr/local/python3 make && make install [root@jiagoushi python3] # vim /etc/profile.d/python.sh export PYTHON_HOME= /usr/local/python3 export PATH=$PYTHON_HOME /bin :$PATH [root@jiagoushi python3] #exec bash |
运行Python文件
1 2 3 4 | [root@jiagoushi ~] # cat test.py print( 'chenxi' ) [root@jiagoushi ~] # python test.py chenxi |
打印汉字
1 2 3 4 5 6 7 | [root@jiagoushi python3] # cat test-2.py #-*- encoding:utf-8 -*- Python2 默认不支持中文 print( '我爱中国' ) [root@jiagoushi python3] # python test-2.py 我爱中国 [root@jiagoushi python3] # python3 test-2.py 我爱中国 |
数值运算
1 2 3 4 5 6 7 8 | [root@jiagoushi python3] # cat test-3.py #-*- encoding:utf-8 -*- #print('我爱中国') print(1+2+3+4+5) print((1+2+3+4+5)*5) [root@jiagoushi python3] # python3 test-3.py 15 75 |
数值运算
1 2 3 4 5 6 7 8 9 10 | [root@jiagoushi python3] # cat test-3.py #-*- encoding:utf-8 -*- #print('我爱中国') print(1+2+3+4+5) print((1+2+3+4+5)*5) print((1+2+3+4+5)*5+100-45+2) [root@jiagoushi python3] # python3 test-3.py 15 75 132 |
Python注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@registry test ] # cat test-1.py #_*_ encoding:utf-8 _*_ print ( "我爱中国" ) x = 10+10 print(x) [root@registry test ] # python test-1.py 我爱中国 20 [root@registry test ] # cat test-1.py #_*_ encoding:utf-8 _*_ print ( "我爱中国" ) '' ' x = 10+10 print(x) '' ' [root@registry test ] # python test-1.py 我爱中国 |
变量就是将一些运算结果存到内存中,便后续调用
变量的定义规则
1.必须由数字,字母,下划线任意组合,以便后续代码调用;且数字不能为开头
2.不能用python关键字
3.变量要具有可描述性
4.不能是中文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [root@registry test ] # cat test-1.py #_*_ encoding:utf-8 _*_ print ( "我爱中国" ) '' ' x = 10+10 print(x) '' ' '' ' t-t = 2 print(t-t) t_t = 3 3t_t = 7 *r = 89 _ = "cx" _vd = "print" '' ' cx1 = 12 cx2 = cx1 cx3 = cx2 cx1 = 9 print(cx1,cx2,cx3) |
常量:一直不变的量
注释:方便别人理解代码,也方便自己
单行注释符“#”
多行注释符'''注释内容''' 或者"""注释内容"""
基础数据类型
数字:int
python2:
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1;
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1;
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1;
python3:
理论上长度是无限的(只要内存足够大)
1 2 3 4 5 6 | [root@registry test ] # cat test-2.py print(100, type (100)) print( '100' , type ( '100' )) [root@registry test ] # python test-2.py (100, < type 'int' >) ( '100' , < type 'str' >) |
字符串相加(叫拼接)不可相减,可相乘数字
1 2 3 4 5 6 7 | [root@registry test ] # cat test-3.py a = "晨曦" b = "哈哈" c = a + b print(c) [root@registry test ] # python3 test-3.py 晨曦哈哈 |
相乘
1 2 3 4 5 6 | [root@registry test ] # cat test-3.py a = "晨曦" b = "哈哈" c = a + b print(c) print ( "晨曦" * 3) |
大的字符串变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [root@registry test ] # cat test-3.py a = "晨曦" b = "哈哈" c = a + b print(c) print ( "晨曦" * 3) e = '' '你好 晨曦 你好 小贱人 '' ' print(e) [root@registry test ] # python3 test-3.py 晨曦哈哈 晨曦晨曦晨曦 你好 晨曦 你好 小贱人 |
输入
1 2 3 4 5 6 7 8 | [root@registry test ] # cat test-4.py name = input( '请输入名字' ) age = input( '请输入年龄' ) print(name,age) [root@registry test ] # python3 test-4.py 请输入名字chenxi 请输入年龄16 chenxi 16 |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏