2024年8月8日(python基础)

一、检查并配置python环境(python2内置)
1、检测是否安装

[root@localhost ~]# yum list installed| grep python
[root@localhost ~]# yum -y install epel-release

2、安装python3

[root@localhost ~]# yum -y install python3

最新版3.12可以使用源码安装

3、查看安装版本

[root@localhost ~]# python3 --version
Python 3.6.8

4、开发工具

安装自带的idea

pycharm(付费)

anaconda (专门做数据分析)

5、修改pip镜像为清华

[root@localhost ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package 

二、数据类型和变量
三大数据类型

字符:字符串

        str

数值:整数 浮点型

        int

        float

逻辑 :True False

进入python的编辑状态

[root@localhost ~]# python3

>>> print("hello world")
hello world
>>> a=3
>>> b="abc"
>>> type(a)
<class 'int'>
>>> type(b)
<class 'str'>

>>> flag=True    逻辑类型
>>> print(flag);
True
>>> print(1==1);
True
>>> print(1!=1);
False
>>> b='abcder';
>>> b
'abcder'
>>> type(b)   字符类型
<type 'str'>
>>> c=3
>>> c
3
>>> type(c)   整数型
<type 'int'>
>>> d=3.14
>>> d
3.14
>>> type(d)    浮点型
<type 'float'>
三、数据集合

最终的计算是在python内存中计算的,必须要有指定内存空间保存数据,这些内存空间其实就是变量,使用数据集合批量管理内存空间

1、列表 list        

(java中数组和list的合体):使用最广泛的数据集合工具,当有多个数据需要管理,可以定义一个列表

(1)管理列表
python为开发提供了丰富的使用手册
help(lista)  通过上下方向键,enter,space键来翻阅信息,使用q退出查看,more less

创建列表
lista=[]
listc=[1,2,3]

修改列表

追加元素
lista.append(item)  在所有元素之后添加元素

插入元素
listb.insert(pos,item)在pos之前插入item

删除元素remove pop
list.pop()删除list中的最后一个元素
list.remove(list[index])删除序号为index的元素

修改元素
list[index]=newvalue

del list
>>> name1="张三"
>>> name2="李四"
>>> name3="王五"
>>> print(name1,name2,name3)
张三 李四 王五

>>> lista=["张三","包丽婷","包丽婷的男朋友是我嘿嘿"]
>>> type(lista)
<class 'list'>
>>> print(lista)
['张三', '包丽婷', '包丽婷的男朋友是我嘿嘿']

>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
>>> listb.append("tomcat")   #添加元素
>>> listb
['tom', 'jerry', 'tomcat']
>>> listb.insert(1,"cat")    #在第二个元素的前面添加元素
>>> listb
['tom', 'cat', 'jerry', 'tomcat']
>>> listb.pop()  #删除(当在列表中删除或者修改一个元素的时候,列表会返回新的列表)
'tomcat'
>>> listb
['tom', 'cat', 'jerry']
>>> listb.remove('cat')   #指定元素删除
>>> listb
['tom', 'jerry']
>>> listb[0]
'tom'
>>> listb[2]    #下标越界
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

>>> listb.remove(listb[0])   #删除第一个元素
>>> listb
['jerry']
2、 字典 

(1)dict

(2)dirctionary

(3)key-value,键值对

(4){"name":"张三","age":"10"}

(5)键:值

{

        "from":"me",

        "to":"you",

        "message":"你出发了吗",

        "time":"2024-7-8 9:00",

        "user":{

        "username":"abc",

        "password":"abc"

        }

}

3、 元组
>>> tup10=(1,2,3,4)
>>> tup10
(1, 2, 3, 4)
>>> tup10[0]
1
>>> tup10[3]
4
>>> tup10[1]=666   #不支持修改
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> list(tup10)
[1, 2, 3, 4]

>>> aa=list(tup10)   #变成列表可以修改
>>> aa
[1, 2, 3, 4]         #列表
>>> tuple(aa)        
(1, 2, 3, 4)         #元组
>>> dict={"a":1,"b":2,"c":3}
#list()可以把dict的key生成一个列表
>>> dict
{'a': 1, 'b': 2, 'c': 3}
>>> dict.keys()
dict_keys(['a', 'b', 'c'])
>>> dict.items()
dict_items([('a', 1), ('b', 2), ('c', 3)])
#list可以把tupl变成列表
#tupl可以把dic和list变成元组
>>> list(dict)
['a', 'b', 'c']
四、选择语句和循环语句
1、选择语句
        (1)必须缩进
        (2)if
>>> a=3
>>> b=4
>>> 
[1]+  已停止               python3   #Ctrl+z退出
[root@localhost ~]# fg    #fg回去
python3
>>> a
3
>>> b
4
if condition0:
    statement0
    if condition1:
        block1
    else:
        block2
else:
    statement1

[root@localhost ~]# vim py001.py


[root@localhost ~]# python3 py001.py   #必须缩进
  File "py001.py", line 2
    print("I`am true")
        ^
IndentationError: expected an indented block
[root@localhost ~]# vim py001.py


[root@localhost ~]# python3 py001.py
I`am true

>>> if True:
... print("I`am true")
  File "<stdin>", line 2
    print("I`am true")
        ^
IndentationError: expected an indented block
>>> if True:
...     print("I`am true")
... else:
...     print("I`am false")
... 
I`am true
(3)if多分枝
if condition0:
    block0
elif condition1:
    block1
elif condition2:
    block1
...
else:
    block2
>>> n=58
>>> if n>90:
...     print("优秀")
... elif n>80:
...     print("良好")
... elif n>70:
...     print("中等")
... elif n>60:
...     print("及格")
... else:
...     print("不及格")
... 
不及格

[root@localhost ~]# vim py002.py


[root@localhost ~]# python3 py002.py
随机分数为: 67
及格

[root@localhost ~]# vim py003.py


[root@localhost ~]# python3 py003.py
随机数为: 77
中等

(4)switch
2、循环语句
(1)for
for var in list:
    print(var)

for i in range(101): #0-100
    n=n+i
    print(n)  #1-100数字累加

在列表中循环
for var in ["a","b","c"]:
    print(var)

在字典中遍历
d={"id":1001,"name":"张三","age":19}
for var in d:
    print(d)  #将d这个字典中的key都输出
    print(d[var])  #根据key返回对应的value的值
for var in d.values():
    print(var)
    print(d[var])
for var in d.keys():
    print(var)

在元组中遍历
tup10=("a","b","v")
for var in tup10:
    print(var)

[root@localhost ~]# vim py004.py
 

[root@localhost ~]# python3 py004.py
5050

[root@localhost ~]# python -m pdb py004.py
> /root/py004.py(1)<module>()
-> n=0
(Pdb) 
(Pdb) n
> /root/py004.py(2)<module>()
-> for i in range(101):
(Pdb) n
> /root/py004.py(3)<module>()
-> n=n+i
(Pdb) n
> /root/py004.py(2)<module>()
-> for i in range(101):
(Pdb) n
> /root/py004.py(3)<module>()
-> n=n+i

1.1在列表中遍历
>>> for var in ["a","b","c"]:
...     print(var)
... 
a
b
c
>>> a=["e","f","g"]
>>> for var in a:
...     print(var)
... 
e
f
g
1.2在字典中循环遍历
>>> d={"id":1001,"name":"张三","age":18,"gender":"男"}
>>> for var in d:
...     print (var)
... 
id
name
age
gender
>>> for var in d:
...     print (var,"-",d[var])
... 
id - 1001
name - 张三
age - 18
gender - 男

>>> for var in d.values():
...     print(var)
... 
1001
张三
18
男
1.3在元组里面遍历
>>> tup10=("a","b","e")
>>> for var in tup10:
...     print (var)
... 
a
b
e
3、 案例(0-100之间可以被7整除的数)
>>> b=list(range(101))
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
>>> for i in b:
...     if i%7==0:
...             print (i,"可以被7整除")
...     
... 
0 可以被7整除
7 可以被7整除
14 可以被7整除
21 可以被7整除
28 可以被7整除
35 可以被7整除
42 可以被7整除
49 可以被7整除
56 可以被7整除
63 可以被7整除
70 可以被7整除
77 可以被7整除
84 可以被7整除
91 可以被7整除
98 可以被7整除
(2)while
while condition:
    block
    #continue,break;   也可以应用于for
>>> n=0
>>> i=1
>>> while i<101:
...     n+=i
...     i+=1
... 
>>> n
5050

>>> i=1
>>> n=0
>>> while True:  #死循环
...     print (i)

>>> while True:
...     print("abc")
...     break
... 
abc
>>> while True:
...     print("abc")
...     continue  #一直循环输出abc

>>> i=1
>>> while True:
...     if i==3:
...             continue   #一直循环但不输出内容
...     print(i)
...     i+=1
... 
1
2
#卡住
五、常用的工具api

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值