Python 基础

# Python的导入
# 第一种方法 import 包名
# import numpy
# import re
#
# # 第二种方法 from 包名 import 函数库,方法
# from matplotlib import offsetbox,dviread,pyplat
# from three_module import get_html
#
# # 第三种方法 import 包名 as 便名
# import numpy as np

# python 输入输出
# 输出,end="",控制换行
# a = 5
# print("hello world ",end="")
# print(type(a))
# # 输入
# a = input()
# print(type(a))
# int表示整形 str 字符串 list 列表 tuple 元组 dict 字典
# if int(a) == 5:
#     print("测试成功")
# else:
#     print("测试失败")
#
# b = list(a)
# print(type(b))

# 包的安装
# pip install 包名

# list 列表 存放数据的仓库 (对元素类型没有要求,对长度也没有要求)
# 数组 只能存放相同类型元素 在定义的时候往往需要指定长度
# 列表的定义,必须用方括号括起来
# 空列表, 没有任何元素
# a = []
# 定义非空列表
# b = [1,'c','abc',851,[1,2,3]]
# 列表中元素的修改
# b[0] = "hello"
# b[4] = 5
# 0,4 表示列表的索引值,从左往右,[0—n],从右往左,[-1-- -n]
# print(b[0])
# 如何取出列表中的元素,第一种方法:通过索引取值
# for i in range(len(b)):
#     print(b[i])
# 第二种方法,遍历法
# for item in b:
#    print(item)

# 成员运算符 in
# if 6 in b:
#    print("success")
# else:
#    print("not in")

# 更新列表
# list_1 = [1,2,3]
# 使用append方法,为列表添加元素
# for i in range(100):
#     list_1.append(i)
# append方法,就是在列表的最后一位添加元素,每次添加都添加到嘞列表的最后一位
# print("b=",list_1)

# # 删除列表中的元素
# del list_1[2]
# print(list_1)

# # 列表的添加
# list_2 = [4,5,6]
# list_3 = list_1 + list_2
# print(list_3)

# 对列表进行切片操作
# list_4 = ["花生","瓜子","西瓜","123","芝麻","456"]
# list_4[n:m],n<=x<m
# print(list_4[2:5])

# 统计列表的长度 len
# print(len(list_4))
# 获取列表中最大的元素
# list_5 = [1,23,41,567,2222]
# print(max(list_5))
# 获取列表中最小的元素
# print(min(list_5))

# Counter方法,快速统计列表中每个元素出现的次数
# string1="August, September and October are the months of kites flying, because the wind blows north-westwards"
# list_6 = string1.split(" ")
# from collections import Counter
# print(Counter(list_6))
# 元组 ---  内容不能修改,其他的跟列表一样
# tuple_1 = ("1",3,6,"hello",[1,2])
# tuple_1[1] = 8
# print(tuple_1)

# 字符串
# string_1 = "hello world"
# string_2 = 'hello "world 2 i" am a boy'
# string_3 = """I AM A BOY
# HELLO WORLD
# """
# 统计字符串的长度,len(),空格也在统计范围之内
# print(len(string_3))

# string_4 = string_2 + string_1
# print(string_4)

# 字符串的复制,通过*来改变
# string_5 = "哈哈哈 "
# string_6 = string_5*1000
# print(string_6)

# 字符串的转换str()来实现
# number = 5
# string_7 = str(number)
# print(type(string_7))

# 字符串的提取
# string_8 = "cat.jpg"
# print(string_8[-4:])

# 字符串的替换---replace('旧字符串',"新字符串")
# sting_9 = "hello <h1> world </h1> <em> <span>!</span>"
# sting_10 = sting_9.replace('<h1>',"").replace('</h1>',"").replace("<em>","").replace('<span>',"").replace('</span>',"")
# print(sting_10)

# 统计高频词汇
sting_11 = """The sixth of August was my mothers birthday. My father and I wanted to give her surprising
birthday presents. On their mothers birthday, I know some or my classmaes are gong tU nep te"mothers with housework; others are going to the shop and buy some flowers for their mothers.
In the morning, my father and l went shopping and bought a very nice cake.Then we went tothe market and bought some food. When we got home, my father cooked some nice food in the
kitchen.l went to my room to make a birthday cara.i wantea to len hy huuill wathea wa rlaanodmuch. Then l wrote in the card,"l love you, my dear mother!" I put it on her pllow, tnen we ceaneathe room. We felt very tired, but we were very happy.
My mother got home. We said, "Happy Birthday!" My mother smiled. We had our suppertogether. My mother said,"Thank you, my daughter!"
How happy we were!
"""

#
word_list = sting_11.split(" ")
from collections import Counter
print(Counter(word_list))

----简单函数的调用

# python 定义函数
# def 函数名(参数列表):
# 定义一个函数,增加参数,有返回值
def number_test(number1,number2):
    a = number1
    b = number2
    c = a+b

    return a,b,c

# 定义主函数来调用,一定要传参回去,调用有返回值的函数时,一定要用参数接收
# def main():
#    number1 = 4
#    number2 = 5
#    a,b,c = number_test(number1,number2)
#    print(a,b,c)

if __name__ == '__main__':
    number1 = 4
    number2 = 5
    a, b, c = number_test(number1, number2)
    print(a, b, c)
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值