Python 基础详解

1. Python 特点

  • Python 面向对象、解析性的语言

  • 几种解析器:

  • CPython:C 语言解析 Python 语言的解析器

  • Jpython:Java 语言解析 Python 语言

  • IronPython:用 C#(.net 平台)解析

  • PyPy:是使用 Python 语言解析

  • Python 的应用广,在科学计算、自然语言处理、图形图像处理、脚本开发、Web 应用

  • Python 几种版本:

  • Python2.X

  • Python3.X

  • Python 并没有做到向下兼容,了解两个版本的区别和联系

  • 能够无缝的改进 Python2.x 的代码 d 到 Python3.x 中

2. Python 基础版本的安装

  • Python.org

  • 目前版本

  • 3.8.0

  • 3.7.8

  • 3.6.8(目前使用)

  • 2.7.17

  • Java 语言------.java-----jar 包------mvn repo-----mvn install/uninstall

  • Python-------.py-------.whl 轮子文件---pip install---pip uninstall

  • pip 的命令

  • pip install package

  • pip uninstall package

  • pip list 展示安装的包

  • pip install -U 更新方式安装

3. Anaconda 数据科学环境的安装

  • IDE--集成开发环境--Eclipse、IDEA 需要加载 JDK-Scala

  • 大数据 hadoop-spark-----CDH(各种大数据组件)

  • 数据科学环境:

  • Anaconda 是数据科学开发环境

  • 包含了基础的 python 的环境

  • 在原来的 Python 的基础上增加了 180+多个工具包

  • 安装:

  • windows 安装,直接下一步下一步,遇到需要打钩的地方尽量打上

  • Anaconda 中有哪些组件?

  • Anaconda-Navigiter

  • Anaconda-Prompt

  • Anaconda-Ipython--增强式的 Python--语法高亮等等---仍然不方便

  • Anacodna**-jupyternotebook**

  • jupyter notebook 是一个以 web 程序启动 web 网页交互式的书写代码的平台

  • 底层基于 ipython 的

  • 两种打开方式:

  • 1-界面的点击方式

  • 2-jupyter notebook 方式

  • 后缀名:.ipynb

  • 可以将 ipynb 的文件拖拽到 jupyternotebook 中启动交互式编写代码

  • Anaconda-Spyder

  • IDE

  • 4. 通过Pycharm和Anaconda的整合

  • 安装 Pycharm---jetBrain 公式

  • 安装 Anaconda

  • 第一步:建议大家更改环境变量

  • 第二步:更改解析器 Interpertor

  • 第三步:创建项目

  • 创建项目的过程中需要指定解析器

  • 第四步:写代码


* 基础代码:

  * print函数在Python2和Python3的区别

  * ```python
    #-*- coding: utf-8 -*-
    # @Time    : 2022/01/25 10:18
    # @Author  : 公众号【五分钟学大数据】
    # @Email   : load
    # @File    : 1.0HelloWorld.py
    # python2中print是语句,既可以加括号也可以不加括号
    # python3中print是函数,必须加括号
    # print "HelloWorld"
    print("HelloWorld")

 

  • conda

  • 安装包:pip install conda install

  • 卸载包: pip unistall conda uninstall

  • 更新的方式安装包:pip install -U conda install -U

  • 查看包:pip list conda list(可以查看哪些是 pip 安装的,那些是 conda 安装的)

  • Conda 命令可以创建独立的 Python 的沙箱环境

  • 在 Python3.6 中创建了一个 Python2.7

  • create -n create envName python==2.7.0

  • source active envName

  • soucre deactive envName

5. Python 的基础数据类型

  • 编码和解码


* 导包

* ```python
  import requests as re
  get = re.get("https://www.fivedata.cn")
  print(get.url)
  print(get.encoding)
  print(get.text)
  
  #sin(x)
  import math
  print(math.sin(0)) #0
  
  from math import sin
  print(sin(0))
  
  from math import sin as f
  print(f(0))
  
  import numpy as np
  print(np.random.randn(3,3))
  # [[ 1.22524688  1.40990719  1.49014802]
  #  [-1.3915769  -0.50625637 -0.38625091]
  #  [-1.39770585 -1.52630576 -0.5186793 ]]

 数据类型


## 6. 输入和输出

* 输入

* ```python
  # # raw_input将数据进行原样的输入----都是str类型
  # # input 输入的类型原样的输入-----如果输入的是数值输出的就是对应的类型
  # # 否则input输入的是字符类型的话必须加引号
  # # age = raw_input("Please input your age:")
  # # print(age)
  # # print(type(age))
  # age = input("Please input your age:")
  # print(age)
  # print(type(age))
  #python3中输入的语句合二为一input和python2的raw_input是一致的
  input1 = input("please input your age:")
  print(input1)
  print(type(input1))

 输出


* 随机数的产生

  * 为什么需要随机数?因为获取现实 的数据是非常困难的,可以模仿现实的数据去产生新数据

  * 产生一个简单的随机数

  * 产生一个符合正态分布的随机数

  * 产生一个符合卡方分布的随机数

  * ```python
    import random
    # - 产生一个简单的随机数
    print(random.random())
    print(random.randrange(1,10))
    # - 产生一个符合正态分布的随机数
    import numpy as np
    print(np.random.randn(3,3))
    # [[-0.65330669 -2.17752451 -0.59458216]
    #  [-1.25848013  1.06000312 -0.21712177]
    #  [-1.5914388   0.59092913  0.56820017]]
    # - 产生一个符合卡方分布的随机数
    # df叫做自由度--越大越接近于正态分布
    print("chisuare test:\n",np.random.chisquare(3,(3,3)))
    print("chisuare test:\n",np.random.chisquare(df=3,size=(3,3)))

 

7. Python 的数据结构

  • 列表

  • 字典

  • 元祖

  • 集合

  • 代码:


## 8. Python的列表

* list

* 切片操作:

* ```python
  #创建
  l1 = [1, 2, 3, 4]
  l2 = list((1,2,3,4))
  l3 = list({1,2,3,4})
  l4 = list({1:"apple",2:"banana",3:"ok",4:"hello"})
  print(l1)
  print(l2)
  print(l3)
  print(l4)
  num=range(10)
  print(num)#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]   range(0, 10)
  print(type(num))#<type 'list'>  <class 'range'>
  print(list(num))
  #切片操作[start:stop:step ]
  l5 = list(range(10))
  print(l5)
  print(l5[::])
  print(l5[0::])
  print(l5[0:2:])
  print(l5[0:2])
  print(l5[::-1])
  print(l5[1::2])
  print(l5[::2])
  print(l5[1:3]) #1-2

 

9. Python 的列表的函数

  • list 的多种创建方法

  • list 的切片操作

  • list 的 zip 函数和 enumerate 函数

  • list 的基本的操作函数

  l1 = list(range(10))
  print(l1)
  print(l1[0])
  # 列表操作包含以下函数:
  # 1、cmp(list1, list2):比较两个列表的元素---在Python3中取消了cmp函数,在Python2中可用的
  # 2、len(list):列表元素个数
  print(len(l1))
  # 3、max(list):返回列表元素最大值
  print(max(l1))
  # 4、min(list):返回列表元素最小值
  print(min(l1))
  # 5、list(seq):将元组转换为列表
  # 列表操作包含以下方法:
  # 1、list.append(obj):在列表末尾添加新的对象
  l1.append("apple")
  print("result value is:",l1)
  # 2、list.count(obj):统计某个元素在列表中出现的次数
  print("list count:",l1.count("apple"))
  # 3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
  l1.extend([6,7,8,9])
  print("change x value is:",l1)
  # 4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
  print(l1.index("apple"))
  # 5、list.insert(index, obj):将对象插入列表
  l1.insert(10,"pear")
  print(l1)
  # 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
  print(l1.pop())
  print(l1.pop())
  # 7、list.remove(obj):移除列表中某个值的第一个匹配项
  l1.remove("apple")
  print(l1)
  # 8、list.reverse():反向列表中元素
  l1.reverse()
  print(l1)
  print(l1[::-1])
  # 9、list.sort([func]):对原列表进行排序
  l1.sort(key=lambda x:len(str(x)),reverse=False)
  print(l1)

 

python模块详解 各个模块的详解 核心模块 1.1. 介绍 1.2. _ _builtin_ _ 模块 1.3. exceptions 模块 1.4. os 模块 1.5. os.path 模块 1.6. stat 模块 1.7. string 模块 1.8. re 模块 1.9. math 模块 1.10. cmath 模块 1.11. operator 模块 1.12. copy 模块 1.13. sys 模块 1.14. atexit 模块 1.15. time 模块 1.16. types 模块 1.17. gc 模块 更多标准模块 2.1. 概览 2.2. fileinput 模块 2.3. shutil 模块 2.4. tempfile 模块 2.5. StringIO 模块 2.6. cStringIO 模块 2.7. mmap 模块 2.8. UserDict 模块 2.9. UserList 模块 2.10. UserString 模块 2.11. traceback 模块 2.12. errno 模块 2.13. getopt 模块 2.14. getpass 模块 2.15. glob 模块 2.16. fnmatch 模块 2.17. random 模块 2.18. whrandom 模块 2.19. md5 模块 2.20. sha 模块 2.21. crypt 模块 2.22. rotor 模块 2.23. zlib 模块 2.24. code 模块 线程和进程 3.1. 概览 3.2. threading 模块 3.3. Queue 模块 3.4. thread 模块 3.5. commands 模块 3.6. pipes 模块 3.7. popen2 模块 3.8. signal 模块 数据表示 4.1. 概览 4.2. array 模块 4.3. struct 模块 4.4. xdrlib 模块 4.5. marshal 模块 4.6. pickle 模块 4.7. cPickle 模块 4.8. copy_reg 模块 4.9. pprint 模块 4.10. repr 模块 4.11. base64 模块 4.12. binhex 模块 4.13. quopri 模块 4.14. uu 模块 4.15. binascii 模块 文件格式 5.1. 概览 5.2. xmllib 模块 5.3. xml.parsers.expat 模块 5.4. sgmllib 模块 5.5. htmllib 模块 5.6. htmlentitydefs 模块 5.7. formatter 模块 5.8. ConfigParser 模块 5.9. netrc 模块 5.10. shlex 模块 5.11. zipfile 模块 5.12. gzip 模块 邮件和新闻消息处理 6.1. 概览 6.2. rfc822 模块 6.3. mimetools 模块 6.4. MimeWriter 模块 6.5. mailbox 模块 6.6. mailcap 模块 6.7. mimetypes 模块 6.8. packmail 模块 6.9. mimify 模块 6.10. multifile 模块 网络协议 7.1. 概览 7.2. socket 模块 7.3. select 模块 7.4. asyncore 模块 7.5. asynchat 模块 7.6. urllib 模块 7.7. urlparse 模块 7.8. cookie 模块 7.9. robotparser 模块 7.10. ftplib 模块 7.11. gopherlib 模块 7.12. httplib 模块 7.13. poplib 模块 7.14. imaplib 模块 7.15. smtplib 模块 7.16. telnetlib 模块 7.17. nntplib 模块 7.18. SocketServer 模块 7.19. BaseHTTPServer 模块 7.20. SimpleHTTPServer 模块 7.21. CGIHTTPServer 模块 7.22. cgi 模块 7.23. webbrowser 模块 国际化 8.1. locale 模块 8.2. unicodedata 模块 8.3. ucnhash 模块 多媒体相关模块 9.1. 概览 9.2. imghdr 模块 9.3. sndhdr 模块 9.4. whatsound 模块 9.5. aifc 模块 9.6. sunau 模块 9.7. sunaudio 模块 9.8. wave 模块 9.9. audiodev 模块 9.10. winsound 模块 数据储存 10.1. 概览 10.2. anydbm 模块 10.3. whichdb 模块 10.4. shelve 模块 10.5. dbhash 模块 10.6. dbm 模块 10.7. dumbdbm 模块 10.8. gdbm 模块 工具和实用程序 11.1. dis 模块 11.2. pdb 模块 11.3. bdb 模块 11.4. profile 模块 11.5. pstats 模块 11.6. tabnanny 模块 其他模块 12.1. 概览 12.2. fcntl 模块 12.3. pwd 模块 12.4. grp 模块 12.5. nis 模块 12.6. curses 模块 12.7. termios 模块 12.8. tty 模块 12.9. resource 模块 12.10. syslog 模块 12.11. msvcrt 模块 12.12. nt 模块 12.13. _winreg 模块 12.14. posix 模块 执行支持模块 13.1. dospath 模块 13.2. macpath 模块 13.3. ntpath 模块 13.4. posixpath 模块 13.5. strop 模块 13.6. imp 模块 13.7. new 模块 13.8. pre 模块 13.9. sre 模块 13.10. py_compile 模块 13.11. compileall 模块 13.12. ihooks 模块 13.13. linecache 模块 13.14. macurl2path 模块 13.15. nturl2path 模块 13.16. tokenize 模块 13.17. keyword 模块 13.18. parser 模块 13.19. symbol 模块 13.20. token 模块 其他模块 14.1. 概览 14.2. pyclbr 模块 14.3. filecmp 模块 14.4. cmd 模块 14.5. rexec 模块 14.6. Bastion 模块 14.7. readline 模块 14.8. rlcompleter 模块 14.9. statvfs 模块 14.10. calendar 模块 14.11. sched 模块 14.12. statcache 模块 14.13. grep 模块 14.14. dircache 模块 14.15. dircmp 模块 14.16. cmp 模块 14.17. cmpcache 模块 14.18. util 模块 14.19. soundex 模块 14.20. timing 模块 14.21. posixfile 模块 14.22. bisect 模块 14.23. knee 模块 14.24. tzparse 模块 14.25. regex 模块 14.26. regsub 模块 14.27. reconvert 模块 14.28. regex_syntax 模块 14.29. find 模块
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值