python

Python: 
    1.概述: 
        1.语言
        2.开发起来简洁 =》 java 
    2.使用场景: 
        1.数据分析  =》 numpy 、pandas 
        2.web 开发  =》 用的不多 、 百度
        3.游戏开发 =》 用的不多
        4.AI (机器学习、深度学习)
        5.爬虫

    
1.部署安装:

    1.安装: 
        1.python 原生的安装包  www.python.org
        2.anaconda 安装 =》 
            1.python 有的
            2.有一些丰富的第三方库

        anaconda:
            1.下载安装包
            2.安装 =》 next 

2.开发python :
    1.ide :
        1.pycharm  =》 idea 
        2.jupyter  =》web 版的开发

3.python 语法
    1.基本数据结构
        1.数值型
            int        整型
            float    小数
        
        2.字符串    
            str

    1.注释
        1.单行注释 # 
        2.多行注释 :
            ''' 
                xxxx
            '''

            """
            xxxx 
            """

    2.数值类型
        1.int 

n1 = 10
print(n1)
print(type(n1))
    2.float
n2 = 10.1
print(n2)
print(type(n2))

    3.str:
        1.单引号 或者双引号 【普通字符串的定义】
        'a' "abc"
        2.多行字符串【跟多行注释有点类似】
        """
            asdas
        """
s1 = 'longshao'
s2 = "qiaoshao"
print(s1)
print(s2)
print(type(s1))
print(type(s2))
s3 = '''
xinwei 说咱班美女多,我说 真的多
'''
print(s3)
print(type(s3))


4.运算符
    算数运算符:+、-、*、/、%、**、//:
n1 = 5
n2 = 10
n3 = 2
print(n1 + n2)
print(n1 - n2 )
print(n1 * n2 )
print(n1 / n2 )
print(n1 % n2 )
print(n1 ** n3 )
print(n1 // n3 )    
    比较运算符: == < > != <= >=
        1.python 中: 
            bool 
n1 = 10
n2 = 20
print(n1 == n2 )
print(n1 <= n2 )
print(n1 >= n2 )
print(n1 != n2 )
print(n1 > n2 )
print(n1 < n2)
print(type(n1 == n2))
    赋值运算符:= += -= /= %= //= **=
n1 = 10
n2 = 20
print(n1)
print(n2)
print("n1=",n1,'n2=',n2)
n2 +=n1
print("n1=",n1,'n2=',n2)
n2 -=n1
print("n1=",n1,'n2=',n2)
n2 /=n1
print("n1=",n1,'n2=',n2)
n2 *=n1
print("n1=",n1,'n2=',n2)
n2 **=n1
print("n1=",n1,'n2=',n2)
n2 //=n1
print("n1=",n1,'n2=',n2)
n2 %= n1
print("n1=",n1,'n2=',n2)
    逻辑运算符:and or not 
n1 = True
n2 = False
print(n1 and n2)
print(n1 or n2 )
print(not n1 )
    成员运算符:in 、not in 
s1 = "ningshao"
print('n' in s1)
print('x' not in s1)

5.流程控制:
    1.分支结构:if 
    2.循环:
        for、while

    1.if 
if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]

类型转换:
    # 1.类型转换
n1 = 1
print(n1)
# print(n1+"1")
print(n1,"1")
print(str(n1) + "1")
print(int("123") + 20)

if:
score = float(input("请输入一个数字:"))
if(score<60):
    print("不及格")
elif (score<80):
    print("良")
elif (score <=100):
    print("优秀")
else:
    print("输入有误")

2.for 
for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

java 集合有哪些:

Collection:
    List: 存储元素,有序、可重复
    Set: 存储元素,不可重复、无序
Map:
    kv:存储一对一对的数据

l1 = [1,2,3,4,"abc","456"]
for el in l1 :
    print(el)
else:
    print("xxx")

3.while 
while_stmt ::=  "while" assignment_expression ":" suite
                ["else" ":" suite]

flag  = float(input("请输入一个数字:"))
while(flag == 1):
    score = float(input("请输入一个数字:"))
    if (score < 60):
        print("不及格")
    elif (score < 80):
        print("良")
    elif (score <= 100):
        print("优秀")
    else:
        print("输入有误")
else:
    print("退出while")


    4.break  continue
    L1= [1,2,3,4,5]
    for el in L1:
        if(el == 3):
            # break
            continue
            print(el)
        else:
            print(el)

6.常见的数据结构
    1.str : 
        1.常用的函数
        2.字符串插值
    2.集合: 
        List :有序的,数据可重复【列表】
        Set :无序的 数据不可重复    【集合】
        tuple:【元组】
        dict: kv 【字典】 =》 kv

1.str
s1 = "NingShao"
s2 = "XinWei"
print(s1)
print(s2)
print(s1 + s2)

# 转义
s3 = "flink\nspark"
print(s3)
s4 =r"flink\nspark"
print(s4)

# 函数
print(len(s1))
print(len(s2))

print(s1.upper())
print(s2.lower())

s5= "abcDDD"
s5 = s5.replace('D',"d")
print(s5) #abcddd =>1 2

s6 = "a,a,a,b,b,b"

L2 = s6.split(",")
print(L2)

# 字符串插值
name = "ningshao"
age = 40
print(name,age)
print("name is : "+name,"age is : ",age)
print(f"name is {name} , age is {age}")
print("name is {0} , age is {1}".format(name, age))


2.集合: 
    List : 有序的集合,元素可以重复
list 类型
L1 = [1,2,3,4,"abc"]

# 1.取值
print(L1[0])
print(L1[0:3]) #左闭右开
print(L1[-1])

# 2.设置值
L1[0]="ningshao"
print(L1)

# 3.添加值
L1.append(1)
L1.append("xuanxuan")
L1.append(1)
print(L1)

# 4.移除值
L1.pop(0)
print(L1)
L1.remove("xuanxuan")
print(L1)

del L1[0]
print(L1)

# 5.函数
print(len(L1))
print(L1.__len__())

L2 = [1,2,3,4,1,6,5]
print(L2)
L2.sort()
print(L2)

L2.sort(reverse=True)
print(L2)

l3 = sorted(L2,reverse=False)
print(l3)


2.tuple  元组
        元组 使用小括号 ,列表使用 方括号
    注意: 
        元组里面的元素是不能被修改的
    tuple    
    t1 = (1,1,1,12,3,"ns","zhege","xw")
    print(t1)
    print(type(t1))

    # 1.取值
    print(t1[0])
    print(t1[0:3])

    # 2.添加值

    t2 = t1.__add__((1,2,3))
    print(t2)
    print(t1 + (1, 2, 3))

    # 2.函数
    print(len(t1))
    n = t1.index("zhege")
    print(n)
    m = t1.count(1)
    print(m)

3.set 
    : 不可重复 无序
s1 = {1,1,1,2,3,4,5,"abc"}
print(s1 )
# 1.添加元素
s1.add(10)
s1.add("zs")
print(s1)

s1.remove(10)
print(s1)

s1.pop()
s1.pop()
print(s1)
# 交 并
print(s1 & {2, 3, 4})
print(s1 | {2, 3, 4})

4.dict 
    kv 
d = {"name":"yusang","age":21,"city":"鞍山"}
print(d)
print(type(d))

# 1.取值
print(d.get("name"))
print(d.get("age"))
print(d.get("city"))

# 2.添加kv
d["gender"]='男'
print(d)

d["age"]=18
print(d)

d.pop("name")
print(d)

# 函数
print(d.items())
print(d.keys())
print(d.values())

for k,v in d.items():
    print(f"key is {k} , value is {v}")

集合常用函数:
    1.enumerate:返回list 下标+value
l1 = [1,2,3,4]
for index,value in enumerate(l1):
    print(index,value)

    2.zip():将多个list 匹配 形成 一个
l1 = ["name","age","gender"]
l2 = ["zs",21,"女"]
print(zip(l1, l2))
print(list(zip(l1, l2)))
for k,v in list(zip(l1, l2)):
print("key is {0}, value is {1}".format(k,v))

    3.reversed()  :对元素进行 降序
print(range(1, 10))
# for el in range(1,10,2):
#     print(el)
for i in reversed(range(1, 10, 2)):
    print(i)

7.推导式
    1.python 特性 快速构建 集合
        1.list 推导式
        [x for x in iterable]
l1 = []
for el in range(5):
    l1.append(el)
print(l1)

l2 = [el for el in range(5)]
print(l2)

        2.set 推导式
s1 = {el for el in range(10) if el > 4}
print(s1)
        3.字典推导式
d1 = {key:key*2 for key in range(10) if key < 3}
print(d1)


1.函数: 
    函数 =》方法 【区别不大】
    scala 【有区别】

    1.内置函数 【 常用类的api】
        1.数学相关的 【math 包下的】
    2.自定义函数
    3.匿名函数【没有名字的函数】


    1.内置函数 【 常用类的api】
    print(abs(-10)) #绝对值
    print(max(1, 2, 3, 4, 5))
    print(min(1, 2, 3, 4))
    print(sum([1, 2, 3]))

    import math
    print(math.sqrt(16))
    print(math.ceil(3.2))
    print(math.floor(3.5))

    2.自定义函数 

    def play():
        print("hello play play")

    play()

    def myAbs(num):
        if(num >= 0):
            return num
        else:
            return -num


    print(myAbs(0))

    # 1.Default Argument Values
    def play_01(name,age=30):
        print(f"{name} is playing xxx,and his age is {age}")

    play_01("longshao")
    play_01("longshao",21)
    play_01(name="longshao",age=21)
    play_01(age=21,name="longshao")


    def play_02(*args,name="ns"):
        print(args[0],args[1],args[2],name)

    play_02(1,2,3)

    3.匿名函数
    def play():
        print("hello play play")
    play()

    p = (lambda :print("hello play play"))
    p()

    def f1(x,y):
        return x*y
    print(f1(1, 2))

    p1 = (lambda x,y: x*y)
    print(p1(2, 3))

    格式化输出:

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


    print(1,"1",sep=",")
    print(str(1)+"1")

2.格式化输出
name="ns"
age=21
print(f"{name},{age}")
print("{0},{1}".format(name,age))

3.异常

java 异常:Throwable
    error : 
    exception:
        1.编译时异常
        2.运行时异常

    异常处理:
        1.try [catch ] ..[finally]  
        2.throws 
        3.throw  

python:
    异常处理: 
    1.try except 
    2.finally
    3.raise 主动抛出异常

语法结构: 
    try: 
        //可能发生异常的代码
    except xxxERROR 
        todo。。。。
    except xxxERROR 
        todo。。。。
    except xxxERROR 
        todo。。。。
    finally:

def f1(n1,n2):
    if(n1 >0 and n2 >0):
        return n1+n2
    else:
        raise BaseException("输入的值不是正数")

try:
    print(f1(-1, 3))
except BaseException as e:
    print(e)


4.导包(import)
    import xxx 
    from xxx import xx 
    import xx as x 
    from xxx import *

5.面向对象

    class 对象
    1.
        动物 =》 类 
        猫 =》 对象
    2.类的定义
        1.属性+方法+构造器

        2.Python中 通过构造器定义属性

        3.构造器:
            __init__()  => 构造器
            self =》 this
def __init__(self):

时间类:
from datetime import datetime
dt = datetime(2022,3,30)
print(dt)
print(dt.year)
print(datetime.now())
s1="2022-03-01"
dt2= datetime.strptime(s1,'%Y-%m-%d')
print(type(dt2))

文件读写:

io: 
java:
    io:    
        字节流:
            inputStream
            outputStream 
        字符流:
            Reader
            Writer
1.读文件 
    1.open 
    2.read 
    3.close

    with open
f = open(r"D:\sxwang\project\pycharm\python-sk\data\1.txt")
res = f.read()
print(res)
f.close()

with open(r"D:\sxwang\project\pycharm\python-sk\data\1.txt") as f:
    res = f.read()
    print(res)

f = open(r"D:\sxwang\project\pycharm\python-sk\data\1.txt")
for line in f:
    print(line,end="")
f.close()

with open(r"D:\sxwang\project\pycharm\python-sk\data\1.txt") as f:
    line = f.readline()
    while (line):
        print(line, end="")
        line = f.readline()

with open(r"D:\sxwang\project\pycharm\python-sk\data\1.txt",'a') as f:
    f.write("a,a,a,a\n")
    f.writelines(["1,1,1,1\n","2,2,2,2\n"])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值