python运维(twenty-four day)

一、python基础

1、环境python2、python3

[root@python ~]# yum list installed | grep python    #检查是否有python包
[root@python ~]# yum list installed | grep epel      #检查是否有epel包
[root@python ~]# yum -y install epel-release
[root@python ~]# yum -y install python3
#最新安装3.12可以使用源码安装
[root@python ~]# python3 --version
Python 3.6.8
#进入python的编辑状态
[root@python ~]# python3  
#如果直接输入python,会进入到python2中

2、变量和数据类型

(1)三大数据类型

        字符        字符串

                str

>>> b='zhangin'
>>> b
'zhangin'
>>> type (b)
<class 'str'>

        数值        整数,浮点

                int

                float

>>> c=3
>>> c
3
>>> type(c)
<class 'int'>
>>> d=3.14
>>> d
3.14
>>> type(d)
<class 'float'>

        逻辑        True | False

>>> flag=True
>>> print(flag);
True
>>> print(1==1);
True
>>> print(1!=1)
False
>>> print("我是救世大英雄")
我是救世大英雄
>>> a=3
>>> b="abc"
>>> type(a)
<class 'int'>    #整数类型
>>> type(b)
<class 'str'>    #变量类型
>>> quit

修改pip镜像为清华
[root@python ~]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ some-package

3、数据集合

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

(1)列表

① 使用最广泛的一个数据集合工具

②在java中数组和list的综合体

③list

        当有多个数据管理,可以定义一个列表

>>> lista=["李四","王五","小江","蛋蛋"]
>>> type(lista)
<class 'list'>
>>> lista
['李四', '王五', '小江', '蛋蛋']

>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
>>> listb.append("tomcat");
>>> listb
['tom', 'jerry', 'tomcat']
>>> listb.insert(1,"xiaojiang")
>>> listb
['tom', 'xiaojiang', 'jerry', 'tomcat']
>>> del listb
>>> listb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'listb' is not defined
>>> listb=["tom","jerry"]
>>> listb
['tom', 'jerry']
>>> listb.pop
<built-in method pop of list object at 0x7f2377678e48>
>>> listb
['tom', 'jerry']
>>> listb.pop()
'jerry'
>>> listb
['tom']
#当在列表中删除或者修改一个元素的时候,列表会返回新的列表

④管理列表

#python为开发提供丰富的使用感手册
help(lista)  #通过上下方向,enter,space键来翻阅信息 使用q推出查看 more less
#创建列表
lista=[]
listc=[1,2,3]
#修改列表
#追加元素
lista.append(item)   #在所有元素之后添加元素
#插入元素
listb.insert(pos,item)  #在pos序号之前插入item

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

#修改元素
list[index]=newvalue
(2)字典

①dict

②dirctionary

③key-value   键值对

        {"name":"小江","age":"39","gender":"male"}

        键:值

{
    "from":"me",
    "to"="you",
    "message":"你吃饭了吗",
    "time"="2024-7-8 9:00:32",
    "user":{
        "username":"abc",
        "password":"abc"
        }
}

>>> a=[1,2,3]
>>> b={"username":"abc","password":"123"}
>>> a
[1, 2, 3]
>>> b
{'username': 'abc', 'password': '123'}
>>> a.append(b)
>>> b.["a"]=a
  File "<stdin>", line 1
    b.["a"]=a
      ^
SyntaxError: invalid syntax
>>> b["a"]=a
>>> a
[1, 2, 3, {'username': 'abc', 'password': '123', 'a': [...]}]
>>> b
{'username': 'abc', 'password': '123', 'a': [1, 2, 3, {...}]}
(3)元组

元组不可以修改,但是可以查看;列表可以改

tuple[index]

list(tuple)

tuple(list)

[]列表        ()元组        {}字典

list()可以把dict的key生成一个列表

list可以把tupl变成列表

tupl可以把dic和list变成元组

总结
功能指令说明
创建列表

[]

list(元组)

list(字典)

字典.keys()

字典.vlues()
字典.items()

符号本身就是列表

将元组转成列表

提取字典的key转成列表

字典中的key返回一个列表
字典中的value组成的列表
字典中的每个k-v组成元组,这些元组组成一个新个列表

修改列表

L.inster(index,value)

L.append(value)

L.[index]=value

L.pop()

del L

在索引值为index的元素之前差人一个元素
在所有元素之后添加一个元素
将索引为index元素的值修改为value
删除最后一个元素
释放L内存
查看列表

L

L[index]

显示列表中的所有数据
返回索引值为index的元素
字典的创建

{}

{k0:v0,k1:v0....}

Dict([(k0,v0),(k1,v0),(k2,v2)])

代表一个空字典

这是有初始值的列表

[]中的每个()中都是2个值,一个是key,一个是value自动解析为一个字典了

元组

(),(1,2,3,4)

也可以从dict中提取,也可以将列表直接转成元组

创建空元组,创建有初始值的元组

>>> tup10=(1,2,3,4)
>>> tup10
(1, 2, 3, 4)
>>> tup10[0]
1
>>> tup10[1]
2
>>> 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]

4、选择语句和循环语句

(1)选择语句

(必须要缩进)

①if
单分支
if condition0:
    statement0;
else:
    statement1;


>>> if True:
...     print("i am true")
... else:
...     print("i am false")
... 
i am true
多分支
import random
n=random.randint(0,100)
print("随机分数为:",n)
if n>90:
        print("优秀")
elif n>80:
        print("良好")
elif n>70:
        print("中等")
elif n>60:
        print("合格")
else:
        print("不及格")
嵌套
import random
n=random.randint(0,100)
print("随机分数为:",n)
if n>90:
        print("优秀")
else:
        if n>80:
                print("良好")
        else:
                if n>70:
                        print("中等")
                else:
                        if n>60:
                                print("合格")
                        else:
                                print("不及格")
②swith

(2)循环语句

①for
for var in list:
    print("var")


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)
​在列表中遍历
>>> for var in ["a","b","c"]:
...     print(var)
... 
a
b
c
>>> a=["e","f","g"]
>>> for var in a:
...     print(var)
... 
e
f
g
在字典中循环遍历
>>> 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
男
在元组里面遍历
>>> tup10=("a","b","e")
>>> for var in tup10:
...     print (var)
... 
a
b
e
 案例(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整除
②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
#卡住
  • 21
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive 中可以使用 UDF(User-Defined Functions)来实现将数字转化为英文格式的功能。以下是一个示例 UDF 的代码实现: ```java import org.apache.hadoop.hive.ql.exec.UDF; import java.text.DecimalFormat; public class NumberToEnglish extends UDF { private static final String[] tensNames = {"", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety"}; private static final String[] numNames = {"", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen"}; private static final String[] bigNames = {"", " thousand", " million", " billion"}; public String evaluate(Double num) { if (num == 0) { return "zero"; } DecimalFormat decimalFormat = new DecimalFormat("#.00"); String formatted = decimalFormat.format(num); String[] parts = formatted.split("\\."); long dollars = Long.parseLong(parts[0]); int cents = Integer.parseInt(parts[1]); String dollarsInEnglish = convert(dollars); String centsInEnglish = convert(cents); String result = dollarsInEnglish + " dollars"; if (cents != 0) { result += " and " + centsInEnglish + " cents"; } return result.trim(); } private static String convert(long num) { if (num == 0) { return "zero"; } String prefix = ""; if (num < 0) { num = -num; prefix = "negative"; } String current = ""; int place = 0; do { long n = num % 1000; if (n != 0) { String s = convertLessThanOneThousand((int) n); current = s + bigNames[place] + current; } place++; num /= 1000; } while (num > 0); return (prefix + current).trim(); } private static String convertLessThanOneThousand(int num) { String current; if (num % 100 < 20) { current = numNames[num % 100]; num /= 100; } else { current = numNames[num % 10]; num /= 10; current = tensNames[num % 10] + current; num /= 10; } if (num == 0) { return current; } return numNames[num] + " hundred" + current; } } ``` 这个 UDF 接受一个 Double 类型的参数,然后将其转化为英文格式的字符串。在这个 UDF 中,我们使用了一个叫做 `convert()` 的函数来将数值转化为英文形式。这个函数将数值按照千位进行分组,然后对每一组的数值进行转化,最后将所有组的结果拼接起来。 例如,要将 USD24,217.45 转化为英文格式,可以使用如下的 HiveQL 语句: ```sql SELECT NumberToEnglish(24217.45); ``` 上面的语句将返回字符串 "twenty-four thousand two hundred seventeen dollars and forty-five cents"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值