Python_day01-PyCharm the initial settings (known), the first Python program, know Python

The initial set of PyCharm (know)

aims

  • Restore the original setting of PyCharm
  • The first time you start PyCharm
  • Create a Python project
  • Set the font PyCharm
  • PyCharm upgrades and other

PyCharm's official website address is: https://www.jetbrains.com/pycharm/

01. restore the original setting of PyCharm

PyCharmThe configuration information is stored in the user's home directory in the .PyCharmxxxx.xdirectory, and xxxx.xrepresents the currently used PyCharmversion number

To restore the PyCharminitial setting can be performed according to the following steps:

    1. Close running PyCharm
    1. Execute the following command in the terminal end, delete PyCharmthe configuration directory:
$ rm -r ~/.PyCharm2016.3
    1. Restart PyCharm

02. The first time you start PyCharm

  1. Import Configuration Information
  2. Select License Agreement
  3. The initial configuration interface

2.1 Import Configuration Information

  • In the first start PyCharm, it will first prompt the user whether to import configuration information before
  • If this is the first time, just click the OK button

001_PyCharm import configuration information -w603

2.2 Select License agreement

  • PyCharm is a paid software, the purchase price is 199 $ / year or 19.90 $ / month
  • But PyCharm provides a version of the free use of students and teachers
  • Commercial versions will be prompted to enter your registration information, or choose a free evaluation

002_ activate the license -w495

2.3 PyCharm initial configuration interface

  • In the initial configuration interface, by Editor colors and fontsselecting the editor color scheme

003_PyCharm initial configuration interface -w491

2.4 Welcome Screen

  • After the basic configuration of all the work, you can see PyCharmthe welcome screen , and through the Welcome screen you can begin to develop a Python project

004_PyCharm welcome page -w664

03. Create / Open a Python project

3.1 Project

  • Development project is to develop a special function to solve a complex business software
  • Usually every project will have a separate dedicated directory for storing all project-related documents
    • A project usually contains several files

3.2 Open Python project

  • Click on Open button and browse to the previously saved directory Python file , either open the project
  • After opening, will create a new directory in a .ideadirectory for storing information related to the project , such as: interpreter version , the project contains files etc.
  • 第一次打开项目,需要耐心等待 PyCharm 对项目进行初始设置

005_ open an existing Python project -w423

设置项目使用的解释器版本

  • 打开的目录如果不是由 PyCharm 建立的项目目录,有的时候 使用的解释器版本是 Python 2.x 的,需要单独设置解释器的版本
  • 通过 File / Settings... 可以打开设置窗口,如下图所示:

Select an item of 006_ interpreter version -w975

3.3 新建项目

1) 命名规则

  • 以后 项目名 前面都以 数字编号随着知识点递增,编号递增
    • 例如:01_Python 基础02_分支03_循环...
  • 每个项目下的 文件名 都以 hm_xx_知识点 方式来命名
    • 其中 xx 是演练文件的序号
  • 注意
      1. 命名文件名时建议只使用 小写字母数字下划线
      1. 文件名不能以数字开始
  • 通过 欢迎界面 或者菜单 File / New Project 可以新建项目

2) 演练步骤

  • 新建 01_Python基础 项目,使用 Python 3.x 解释器
  • 在项目下新建 hm_01_hello.py Python 文件
  • 编写 print("Hello Python") 代码

04. 设置 PyCharm 的字体显示

007_PyCharm设置编辑器字体-w500

008_PyCharm设置控制台字体-w500

05. PyCharm 的升级以及其他

PyCharm 提供了对 学生和教师免费使用的版本

5.1 安装和启动步骤

    1. 执行以下终端命令,解压缩下载后的安装包
$ tar -zxvf pycharm-professional-2017.1.3.tar.gz
    1. 将解压缩后的目录移动到 /opt 目录下,可以方便其他用户使用

/opt 目录用户存放给主机额外安装的软件

$ sudo mv pycharm-2017.1.3/ /opt/
    1. 切换工作目录
$ cd /opt/pycharm-2017.1.3/bin
    1. 启动 PyCharm
$ ./pycharm.sh

5.2 设置专业版启动图标

  • 专业版中,选择菜单 Tools / Create Desktop Entry... 可以设置任务栏启动图标
    • 注意:设置图标时,需要勾选 Create the entry for all users

009_创建桌面图标-w657

5.3 卸载之前版本的 PyCharm

1) 程序安装

    1. 程序文件目录
    • 将安装包解压缩,并且移动到 /opt 目录下
    • 所有的相关文件都保存在解压缩的目录中
    1. 配置文件目录
    • 启动 PyCharm 后,会在用户家目录下建立一个 .PyCharmxxx 的隐藏目录
    • 保存 PyCharm 相关的配置信息
    1. 快捷方式文件
    • /usr/share/applications/jetbrains-pycharm.desktop

ubuntu 中,应用程序启动的快捷方式通常都保存在 /usr/share/applications 目录下

2) 程序卸载

  • 要卸载 PyCharm 只需要做以下两步工作:

    1. 删除解压缩目录
$ sudo rm -r /opt/pycharm-2016.3.1/
    1. 删除家目录下用于保存配置信息的隐藏目录
$ rm -r ~/.PyCharm2016.3/

如果不再使用 PyCharm 还需要将 /usr/share/applications/ 下的 jetbrains-pycharm.desktop 删掉

5.4 教育版安装演练

# 1. 解压缩下载后的安装包
$ tar -zxvf pycharm-edu-3.5.1.tar.gz

# 2. 将解压缩后的目录移动到 `/opt` 目录下,可以方便其他用户使用
$ sudo mv pycharm-edu-3.5.1/ /opt/

# 3. 启动 `PyCharm`
/opt/pycharm-edu-3.5.1/bin/pycharm.sh

后续课程都使用专业版本演练

设置启动图标

    1. 编辑快捷方式文件
$ sudo gedit /usr/share/applications/jetbrains-pycharm.desktop
    1. 按照以下内容修改文件内容,需要注意指定正确的 pycharm 目录
[Desktop Entry]
Version=1.0
Type=Application
Name=PyCharm
Icon=/opt/pycharm-edu-3.5.1/bin/pycharm.png
Exec="/opt/pycharm-edu-3.5.1/bin/pycharm.sh" %f
Comment=The Drive to Develop
Categories=Development;IDE;
Terminal=false
StartupWMClass=jetbrains-pycharm

第一个 Python 程序

目标

  • 第一个 HelloPython 程序
  • Python 2.x3​​.x 版本简介
  • 执行 Python 程序的三种方式
    • 解释器 —— python / python3
    • 交互式 —— ipython
    • 集成开发环境 —— PyCharm

01. 第一个 HelloPython 程序

1.1 Python 源程序的基本概念

  1. Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件Python 的开发
  2. Python 程序的 文件扩展名 通常都是 .py

1.2 演练步骤

  • 在桌面下,新建 认识Python 目录
  • 认识Python 目录下新建 01-HelloPython.py 文件
  • 使用 gedit 编辑 01-HelloPython.py 并且输入以下内容:
print("hello python")
print("hello world")
  • 在终端中输入以下命令执行 01-HelloPython.py
$ python 01-HelloPython.py

printpython 中我们学习的第一个 函数

print 函数的作用,可以把 "" 内部的内容,输出到屏幕上

1.3 演练扩展 —— 认识错误(BUG)

关于错误

  • 编写的程序不能正常执行,或者执行的结果不是我们期望的
  • 俗称 BUG,是程序员在开发时非常常见的,初学者常见错误的原因包括:
    1. 手误
    2. 对已经学习过的知识理解还存在不足
    3. 对语言还有需要学习和提升的内容
  • 在学习语言时,不仅要学会语言的语法,而且还要学会如何认识错误和解决错误的方法

每一个程序员都是在不断地修改错误中成长的

第一个演练中的常见错误

  • 1> 手误,例如使用 pirnt("Hello world")
NameError: name 'pirnt' is not defined

名称错误:'pirnt' 名字没有定义
  • 2> 将多条 print 写在一行
SyntaxError: invalid syntax

语法错误:语法无效

每行代码负责完成一个动作

  • 3> 缩进错误
IndentationError: unexpected indent

缩进错误:不期望出现的缩进
  • Python 是一个格式非常严格的程序设计语言
  • 目前而言,大家记住每行代码前面都不要增加空格
  • 4> python 2.x 默认不支持中文

目前市场上有两个 Python 的版本并存着,分别是 Python 2.xPython 3.x

  • Python 2.x 默认不支持中文,具体原因,等到介绍 字符编码 时给大家讲解
  • Python 2.x 的解释器名称是 python
  • Python 3.x 的解释器名称是 python3
SyntaxError: Non-ASCII character '\xe4' in file 01-HelloPython.py on line 3, 
but no encoding declared; 
see http://python.org/dev/peps/pep-0263/ for details

语法错误: 在 01-HelloPython.py 中第 3 行出现了非 ASCII 字符 '\xe4',但是没有声明文件编码
请访问 http://python.org/dev/peps/pep-0263/ 了解详细信息
  • ASCII 字符只包含 256 个字符,不支持中文
  • 有关字符编码的问题,后续会讲

单词列表

* error 错误
* name 名字
* defined 已经定义
* syntax 语法
* invalid 无效
* Indentation 索引
* unexpected 意外的,不期望的
* character 字符
* line 行
* encoding 编码
* declared 声明
* details 细节,详细信息
* ASCII 一种字符编码

02. Python 2.x3​​.x 版本简介

目前市场上有两个 Python 的版本并存着,分别是 Python 2.xPython 3.x

新的 Python 程序建议使用 Python 3.0 版本的语法

  • Python 2.x 是 过去的版本
    • 解释器名称是 python
  • Python 3.x 是 现在和未来 主流的版本
    • 解释器名称是 python3
    • 相对于 Python 的早期版本,这是一个 较大的升级
    • 为了不带入过多的累赘,Python 3.0 在设计的时候 没有考虑向下兼容
      • 许多早期 Python 版本设计的程序都无法在 Python 3.0 上正常执行
    • Python 3.0 发布于 2008 年
    • 到目前为止,Python 3.0 的稳定版本已经有很多年了
      • Python 3.3 发布于 2012
      • Python 3.4 发布于 2014
      • Python 3.5 发布于 2015
      • Python 3.6 发布于 2016
  • 为了照顾现有的程序,官方提供了一个过渡版本 —— Python 2.6
    • 基本使用了 Python 2.x 的语法和库
    • 同时考虑了向 Python 3.0 的迁移,允许使用部分 Python 3.0 的语法与函数
    • 2010 年中推出的 Python 2.7 被确定为 最后一个Python 2.x 版本

提示:如果开发时,无法立即使用 Python 3.0(还有极少的第三方库不支持 3.0 的语法),建议

  • 先使用 Python 3.0 版本进行开发
  • 然后使用 Python 2.6Python 2.7 来执行,并且做一些兼容性的处理

03. 执行 Python 程序的三种方式

3.1. 解释器 python / python3

Python 的解释器

# 使用 python 2.x 解释器
$ python xxx.py

# 使用 python 3.x 解释器
$ python3 xxx.py
其他解释器(知道)

Python 的解释器 如今有多个语言的实现,包括:

  • CPython —— 官方版本的 C 语言实现
  • Jython —— 可以运行在 Java 平台
  • IronPython —— 可以运行在 .NET 和 Mono 平台
  • PyPy —— Python 实现的,支持 JIT 即时编译

3.2. 交互式运行 Python 程序

  • 直接在终端中运行解释器,而不输入要执行的文件名
  • 在 Python 的 Shell 中直接输入 Python 的代码,会立即看到程序执行结果

1) 交互式运行 Python 的优缺点

优点
  • 适合于学习/验证 Python 语法或者局部代码
缺点
  • 代码不能保存
  • 不适合运行太大的程序

2) 退出 官方的解释器

1> 直接输入 exit()
>>> exit()
2> 使用热键退出

在 python 解释器中,按热键 ctrl + d 可以退出解释器

001_积跬步以至千里

3) IPython

  • IPython 中 的 “I” 代表 交互 interactive
特点
  • IPython 是一个 python 的 交互式 shell,比默认的 python shell 好用得多
    • 支持自动补全
    • 自动缩进
    • 支持 bash shell 命令
    • 内置了许多很有用的功能和函数
  • IPython 是基于 BSD 开源的
版本
  • Python 2.x 使用的解释器是 ipython
  • Python 3.x 使用的解释器是 ipython3

  • 要退出解释器可以有以下两种方式:

1> 直接输入 exit
In [1]: exit
2> 使用热键退出

在 IPython 解释器中,按热键 ctrl + dIPython 会询问是否退出解释器

IPython 的安装

$ sudo apt install ipython

3.3. Python 的 IDE —— PyCharm

1) 集成开发环境(IDE)

集成开发环境(IDE,Integrated Development Environment)—— 集成了开发软件需要的所有工具,一般包括以下工具:

  • 图形用户界面
  • 代码编辑器(支持 代码补全自动缩进
  • 编译器/解释器
  • 调试器(断点单步执行
  • ……

2)PyCharm 介绍

  • PyCharm 是 Python 的一款非常优秀的集成开发环境
  • PyCharm 除了具有一般 IDE 所必备功能外,还可以在 WindowsLinuxmacOS 下使用
  • PyCharm 适合开发大型项目
    • 一个项目通常会包含 很多源文件
    • 每个 源文件 的代码行数是有限的,通常在几百行之内
    • 每个 源文件 各司其职,共同完成复杂的业务功能

3)PyCharm 快速体验

001_PyCharm的界面结构

  • 文件导航区域 能够 浏览定位打开 项目文件
  • 文件编辑区域 能够 编辑 当前打开的文件
  • 控制台区域 能够:
    • 输出程序执行内容
    • 跟踪调试代码的执行
  • 右上角的 工具栏 能够 执行(SHIFT + F10) / 调试(SHIFT + F9) 代码

002_PyCharm运行工具栏

  • 通过控制台上方的单步执行按钮(F8),可以单步执行代码

003_PyCharm调试器

认识 Python

人生苦短,我用 Python —— Life is short, you need Python

001_人生苦短我用python

目标

  • Python 的起源
  • 为什么要用 Python?
  • Python 的特点
  • Python 的优缺点

01. Python 的起源

Python 的创始人为吉多·范罗苏姆(Guido van Rossum)

002_吉多-w256

  1. 1989 年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的解释程序,作为 ABC 语言的一种继承(感觉下什么叫牛人
  2. ABC 是由吉多参加设计的一种教学语言,就吉多本人看来,ABC 这种语言非常优美和强大,是专门为非专业程序员设计的。但是 ABC 语言并没有成功,究其原因,吉多认为是非开放造成的。吉多决心在 Python 中避免这一错误,并获取了非常好的效果
  3. 之所以选中 Python(蟒蛇) 作为程序的名字,是因为他是 BBC 电视剧——蒙提·派森的飞行马戏团(Monty Python's Flying Circus)的爱好者
  4. 1991 年,第一个 Python 解释器 诞生,它是用 C 语言实现的,并能够调用 C 语言的库文件

1.1 解释器(科普)

计算机不能直接理解任何除机器语言以外的语言,所以必须要把程序员所写的程序语言翻译成机器语言,计算机才能执行程序。将其他语言翻译成机器语言的工具,被称为编译器

编译器翻译的方式有两种:一个是编译,另外一个是解释。两种方式之间的区别在于翻译时间点的不同。当编译器以解释方式运行的时候,也称之为解释器

001_编译型和解释型语言工作对比-w360

  • 编译型语言:程序在执行之前需要一个专门的编译过程,把程序编译成为机器语言的文件,运行时不需要重新翻译,直接使用编译的结果就行了。程序执行效率高,依赖编译器,跨平台性差些。如 C、C++
  • 解释型语言:解释型语言编写的程序不进行预先编译,以文本方式存储程序代码,会将代码一句一句直接运行。在发布程序时,看起来省了道编译工序,但是在运行程序的时候,必须先解释再运行

编译型语言和解释型语言对比

  • 速度 —— 编译型语言比解释型语言执行速度快
  • 跨平台性 —— 解释型语言比编译型语言跨平台性好

1.2 Python 的设计目标

1999 年,吉多·范罗苏姆向 DARPA 提交了一条名为 “Computer Programming for Everybody” 的资金申请,并在后来说明了他对 Python 的目标:

  • 一门简单直观的语言并与主要竞争者一样强大
  • 开源,以便任何人都可以为它做贡献
  • 代码像纯英语那样容易理解
  • 适用于短期开发的日常任务

这些想法中的基本都已经成为现实,Python 已经成为一门流行的编程语言

1.3 Python 的设计哲学

  1. 优雅
  2. 明确
  3. 简单
  • Python 开发者的哲学是:用一种方法,最好是只有一种方法来做一件事
  • 如果面临多种选择,Python 开发者一般会拒绝花俏的语法,而选择明确没有或者很少有歧义的语法

在 Python 社区,吉多被称为“仁慈的独裁者”

02. 为什么选择 Python?

  • 代码量少
  • ……

同一样问题,用不同的语言解决,代码量差距还是很多的,一般情况下 PythonJava1/5,所以说 人生苦短,我用 Python

03. Python 特点

  • Python 是完全面向对象的语言
    • 函数模块数字字符串都是对象,在 Python 中一切皆对象
    • 完全支持继承、重载、多重继承
    • 支持重载运算符,也支持泛型设计
  • Python 拥有一个强大的标准库,Python 语言的核心只包含 数字字符串列表字典文件 等常见类型和函数,而由 Python 标准库提供了 系统管理网络通信文本处理数据库接口图形系统XML 处理 等额外的功能
  • Python community provides a large number of third-party modules , used in a manner similar to the standard library. Their function covering scientific computing , artificial intelligence , machine learning , Web development , database interface , graphics system fields

Object-oriented way of thinking

  • Object-oriented is a way of thinking , is a programming technique
  • To solve a problem before, first consider who should do, how to do things is who 's responsibility, and finally trying to be difficult to get things done!
    • The object is who
  • To solve complex problems, you can find a number of different objects , their duties together to achieve the eventual demand

04. Python's strengths and weaknesses

4.1 advantage

  • Simple, easy to learn
  • Free, open-source
  • Object-Oriented
  • Extensive Libraries
  • Scalability
    • If you need a critical piece of code to run very fast or want some algorithms are not open, this part of the program can be used Cor C++written, and then Pythonuse them in the program
  • ……

4.2 shortcomings

  • Operating speed
  • Small domestic markets
  • Chinese lack of information

Guess you like

Origin www.cnblogs.com/jiexiaobai/p/12194873.html
Recommended