python基础

python介绍:

编程排行榜


Paste_Image.png

一种面向对象的解释性计算机设计语言,具有丰富和强大的库。

  • python定位:“优雅”、“明确”、“简单”
  • 多种应用场景:可以写工具,后台服务,移动端等等。

python解释器

编写python代码的时候一般都是以.py为后缀名的python文件,运行代码的时候就是通过python解释器运行这个.py的脚本文件。官方的python解释器是CPython

运行环境

下载对应版本的Python安装,官网地址如下

https://www.python.org/
运行get-pip.py安装pip (Py3自带pip)
https://bootstrap.pypa.io/get-pip.py
添加python目录及Scripts子目录添加至环境变量中

安装虚拟环境

安装virtualenv

pip install virtualenv

配置正确的话,会进入python解释器界面

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

编辑器选择

Sublime Text,Wing IDE,pycharm

python基础语法和概念

  • 变量
>>> x=3
>>> x*2
6
>>> x*999
2997
  • 整数、浮点数
x=567(整数)
y=5.67(浮点数)
x/y(默认返回浮点数)
x//y(默认返回整数)
  • 函数
def checkIsHasCapitalLetter(filename):
    if filename.islower() == True:
        return True
    else:
        common.outError(filename + "文件名存在大写.................")
        return Fasle           
  • 缩进,流程控制语句for,if,while等都靠缩进识别,还有函数也是
for i in range(0, 5):
    print(i)
  • 模块
>>>import common(允许使用命名空间访问common里的东西)
>>>common.count()
>>>from common import count(添加到当前命名空间)
>>>count()
  • 执行python程序
python.exe test.py
  • 大小写敏感

常用数据结构

  • 列表(list)
  • 字典(dict)
  • 集合(set)
  • 元组(tuple)

常用数据类型

  • 整数
  • 浮点数
  • 布尔值
  • 字符串

字符串基础

字符串是不可以改变的,和java一样,如若赋值,其实是新建了一个字符串
字符串定义,可以是单引号,也可以是双引号,字符拼接用+号

>>> "hello world"
'hello world'
>>> 'hello world'
'hello world'
>>> "hello" + "world"
'helloworld'
>>> "don't let go"
"don't let go"
>>> 'don\'t let go'(转义单引号)
"don't let go"
>>> "don\'t let go"
"don't let go"
>>> r"don\'t let go"(前面带"r"不转义)
"don\\'t let go"
  • 字符串查找
>>> "nothing to see here".find("see")
11
>>> "nothing to see here".find("ok")
-1
  • 字符串格式化
>>> filename = "notepad"
>>> "get file: %s ok"%filename
'get file: notepad ok'
  • UNICODE,python3字符串是以Unicode编码的
1.将其他字符串格式转为unicode:
ret=str.decode("gb2312")
ret=str.decode("ascii")
ret=str.decode("utf-8")
2.将unicode字符转为其他字符串格式:
ret=str.encode(“gb2312”)
ret=str.encode("ascii")
ret=str.encode("utf-8")
  • 正则表达式
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
  • 其他
字符串内建函数,len(), split(), upper(), strip()
多行字符串
字符串下标[0],[-2]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入...
    小小不懂11阅读 8,718评论 2 30
  • Author:ProZoom Hobby:爱折腾、爱思考,想静静的ProZoom Github --- 简书 ...
    ProZoom阅读 4,026评论 0 1
  • Python基础 数据类型和变量 数据类型电子计算机也就是我们俗称的计算机(电脑),其主要功能是完成科学计算的任务...
    獨身犬阅读 3,652评论 0 0
  • 本教程基于Python 3,参考 A Byte of Python v1.92(for Python 3.0) 以...
    yuhuan121阅读 8,331评论 1 6
  • Python是一种计算机的编程语言,它与我们日常生活中的自然语言相比存在很多的不同,自然语言在不同的语言环境下有不...
    Sun_atom阅读 689评论 0 0