python基础

安装python3环境

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文件

[root@jiagoushi ~]# cat test.py 
print('chenxi')
[root@jiagoushi ~]# python test.py 
chenxi

  打印汉字

[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 
我爱中国

  数值运算

[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

   数值运算

[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注释

[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.不能是中文

[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;

python3:

  理论上长度是无限的(只要内存足够大)

[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'>)

  字符串相加(叫拼接)不可相减,可相乘数字

[root@registry test]# cat test-3.py
a = "晨曦"
b = "哈哈"
c = a + b
print(c)
[root@registry test]# python3 test-3.py 
晨曦哈哈

 相乘

[root@registry test]# cat test-3.py
a = "晨曦"
b = "哈哈"
c = a + b
print(c)
print ("晨曦" * 3)

  大的字符串变量

[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 
晨曦哈哈
晨曦晨曦晨曦
你好
晨曦
你好
小贱人

 输入

[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

  

 

草都可以从石头缝隙中长出来更可况你呢
原文地址:https://www.cnblogs.com/rdchenxi/p/10734869.html