Python运维开发之路《python基础介绍》

一. python介绍相关

 1. Python简介

Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。

- Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构。

- Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节。类似于PHP和Perl语言。

- Python 是交互式语言: 这意味着,您可以在一个Python提示符,直接互动执行写你的程序。

- Python 是面向对象语言: 这意味着Python支持面向对象的风格或代码封装在对象的编程技术。

2. Python主要应用领域

- 云计算: 云计算最火的语言, 典型应用OpenStack;

- WEB开发: 众多优秀的WEB框架,众多大型网站均为Python开发、Youtube、 Dropbox、豆瓣、知乎等典型WEB框架有Django、Flask、Web2py、AngularJS;

- 科学运算、人工智能: 典型库NumPy、 SciPy、Matplotlib、Enthought、librarys、pandas;

- 系统运维: 运维人员必备语言,升职加薪必备之选;

- 金融:量化交易,金融分析,在金融工程领域,Python不但在用,且用的最多,而且重要性逐年提高。原因:作为动态语言的Python,语言结构清晰简单,库丰富,成熟稳定,科学计算和统计分析都很牛逼,生产效率远远高于c、c++、java、尤其擅长策略回测;

- 图形GUI:PyQT、WxPython、TkInter。

二、Python基础

3. Python2与Python3相关

 1 Python2.X和Python3.X的区别
 2 
 3 What are the differences?
 4 
 5 Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
 6 
 7 Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is under active development and has already seen over five years of stable releases, including version 3.3 in 2012, 3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only available by default in Python 3.x.
 8 
 9 Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.
10 
11 Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x).
12 
13 The What's New in Python 3.0 document provides a good overview of the major language changes and likely sources of incompatibility with existing Python 2.x code. Nick Coghlan (one of the CPython core developers) has also created a relatively extensive FAQ regarding the transition.
14 
15 However, the broader Python ecosystem has amassed a significant amount of quality software over the years. The downside of breaking backwards compatibility in 3.x is that some of that software (especially in-house software in companies) still doesn't work on 3.x yet. 
16 
17 详细地址:https://wiki.python.org/moin/Python2orPython3
18 
19 - 区别一:
20         Python2:print 'Hellow world!'
21         Python3: print ('Hellow world!')
22 - 区别二:
23         Python2:raw_input
24         Python3:input

4. Python安装

1.下载安装包
https://www.python.org/downloads/
2.安装
默认安装路径:C:python2.X
3.配置环境变量
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 -->【Python安装目录追加到变值值中,用 分号做分割】如:原来的值;C:python2.X,切记前面有分号.

注:目前3.0以上新版本安装提示写入环境变量.

5. 第一行Python代码

1 - IDE中执行:
2     print ("Hello world!")
3 - Linux环境,将"print ('Hello world!')"代码写入到first.py文件中:
4     python first.py
5 - 指定解释器执行:
6     #/usr/bin/env python
7     print ('Hello world!')
8     chmod +x first.py
9     ./first.py

6. 编码格式和二进制

- 二进制十进制转换

  1   2   4   8   16  ....    65535
  1   1   1   1   1   ....    1*16
- Python2.X默认字符编码是ASSCII,Python3.X默认字符编码UFT-8.
- 声明字符编码:
 #-*- coding:uft-8 -*-
 #-*- coding:gbk -*-
- asscii码  255 1bytes  一个字符占8个字节
- unicode万国码 2bytes
- utf-8 en:1byte cn:3bytes

7. 变量的定义

- 变量:一个在内存储存数据的容器,以便后面的程序调用,变量先定义后引用;
-
变量定义规则:
- [1] 变量名只能是 字母、数字或下划线的任意组合
- [2] 变量名的第一个字符不能是数字
- [3] 以下关键字不能声明为变量名
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
- [4] 写法
    age_of_oldboy = 56
    ageOfOldboy = 56 #驼峰写法
    AGE_OF_OLDBOY = 56 #常量,定义不变的量

8. 字和字符串

数字不需要加引号
字符串必须加引号

9. 注释

单行注释:#
多行注释:"""或者'''
多行字符串:""" (标注段落)

10. 字符串格式

****Python默认输入都是字符串****
int()   #interger,字符串转数字
str()   #string,数字转字符串
type()  #查看数据类型:print(type())
strip() #去掉换行符和空格

11. 字符串格式化拼接

 1 %s 变量可以是字符和数字(string)
 2 %d 变量只能是数字,可以用来检测数据类型
 3 %f 变量只能是小数(浮点型)
 4 
 5 方案一:
 6 name = input("name:")
 7 age = input("age:")
 8 job = input("job:")
 9 salary = input("salary:")
10 
11 info = '''
12 -----info of '''+name+'''----
13 Name:'''+name+'''
14 Age:'''+age+'''
15 Job:'''+job+'''
16 Salary:'''+salary+'''
17 '''
18 print(info)
19 
20 方案二:
21 name = input("name:")
22 age = input("age:")
23 job = input("job:")
24 salary = input("salary:")
25 
26 info = '''
27 ------ info of %s------
28 Name:%s
29 Age:%s
30 Job:%s
31 Salary:%s
32 ----------END----------'''%(name,name,age,job,salary)
33 print(info)
34 
35 方案三:
36 name = input("name:")
37 age = input("age:")
38 job = input("job:")
39 salary = input("salary:")
40 
41 info2 = '''
42 ------ info of {_name}------
43 Name:{_name}
44 Age:{_age}
45 Job:{_job}
46 Salary:{_salary}
47 '''.format(_name=name,_age=age,_job=job,_salary=salary)
48 print(info2)
49 
50 方案四:
51 name = input("name:")
52 age = input("age:")
53 job = input("job:")
54 salary = input("salary:")
55 info2 = '''
56 -----info of {0}-----
57 Name:{0}
58 Age:{1}
59 Job:{2}
60 Salary:{3}
61 ------END----'''.format(name,age,job,salary)
62 print(info2)

12. if判断和while循环

if判断,判断账号密码是否正确;
1
user = 'lain' 2 passwd = '123456' 3 username = input("username:") 4 password = input("password:") 5 6 if username == user and password == passwd: 7 print("欢迎登陆!") 8 else: 9 print("账户名或密码错误,请重试!")
 while循环,猜年龄,猜3次后可选择是否继续;
1
age_of_oldboy = 56 2 count = 0 3 while count < 3: 4 guess_age = int(input("guess age:")) 5 if guess_age == age_of_oldboy: 6 print("yes.you got it.") 7 break 8 elif guess_age > age_of_oldboy: 9 print("think smaller...") 10 else: 11 print("think bigger.") 12 count +=1 13 if count == 3: 14 countine_confirm = input("do you want to keep guessing? y/n") 15 if countine_confirm != "n": 16 count = 0 17 else: 18 print("error,you idiot!")

Pycharm小记

 1 Ctrl /
 2 
 3 注释(取消注释)选择的行
 4 
 5 
 6 Shift + Enter
 7 开始新行
 8 
 9 Ctrl + Enter
10 智能换行
11 
12 TAB Shift+TAB
13 缩进/取消缩进所选择的行
14 
15 Ctrl + Alt + I
16 自动缩进行
17 
18 Ctrl + Y
19 删除当前插入符所在的行
20 
21 Ctrl + D
22 复制当前行、或者选择的块
23 
24 Ctrl + Shift + J
25 合并行
26 
27 Ctrl + Shift + V
28 从最近的缓存区里粘贴
29 
30 Ctrl + Delete
31 删除到字符结尾
32 
33 Ctrl + Backspace
34 删除到字符的开始
35 
36 Ctrl + NumPad+/-
37 展开或者收缩代码块
38 
39 Ctrl + Shift + NumPad+
40 展开所有的代码块
41 
42 Ctrl + Shift + NumPad-
43 收缩所有的代码块
 
 
 

本文来自博客园,作者:白日梦想家Zz,转载请注明原文链接:https://www.cnblogs.com/zzlain/p/5930425.html

原文地址:https://www.cnblogs.com/zzlain/p/5930425.html