python基础知识一

一、python介绍
python是一种解释型、交互式、面向对象、动态数据类型的高级程序设计语言;
python由Guido van Rossum于1989年底用C语言编写,第一个公开发行版发行于1991年。
python代码遵循GPL(GNU General Public License)协议
二、python特点:
1.易于学习:Python有相对较少的关键字,结构简单,和一个明确定义的语法,学习起来更加简单。

2.易于阅读:Python代码定义的更清晰。

3.易于维护:Python的成功在于它的源代码是相当容易维护的。

4.一个广泛的标准库:Python的最大的优势之一是丰富的库,跨平台的,在UNIX,Windows和Macintosh兼容很好。

5.互动模式:互动模式的支持,您可以从终端输入执行代码并获得结果的语言,互动的测试和调试代码片断。

6.可移植:基于其开放源代码的特性,Python已经被移植(也就是使其工作)到许多平台。

7.可扩展:如果你需要一段运行很快的关键代码,或者是想要编写一些不愿开放的算法,你可以使用C或C++完成那部分程序,然后从你的Python程序中调用。

8.数据库:Python提供所有主要的商业数据库的接口。

9.GUI编程:Python支持GUI可以创建和移植到许多系统调用。

10.可嵌入: 你可以将Python嵌入到C/C++程序,让你的程序的用户获得"脚本化"的能力。
三、python主要应用领域:
云计算;WEB开发;科学运算、人工智能;系统运维;金融;图像GUI
四、python发展历史
1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器。Python这个名字,来自Guido所挚爱的电视剧Monty Python’s Flying Circus。他希望这个新的叫做Python的语言,能符合他的理想:创造一种C和shell之间,功能全面,易学易用,可拓展的语言。
1991年,第一个Python编译器诞生。它是用C语言实现的,并能够调用C语言的库文件。从一出生,Python已经具有了:类,函数,异常处理,包含表和词典在内的核心数据类型,以及模块为基础的拓展系统。
Granddaddy of Python web frameworks, Zope 1 was released in 1999
Python 1.0 - January 1994 增加了 lambda, map, filter and reduce.
Python 2.0 - October 16, 2000,加入了内存回收机制,构成了现在Python语言框架的基础
Python 2.4 - November 30, 2004, 同年目前最流行的WEB框架Django 诞生
Python 2.5 - September 19, 2006
Python 2.6 - October 1, 2008
Python 2.7 - July 3, 2010
In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible
Python 3.0 - December 3, 2008
Python 3.1 - June 27, 2009
Python 3.2 - February 20, 2011
Python 3.3 - September 29, 2012
Python 3.4 - March 16, 2014
Python 3.5 - September 13, 2015
五、python安装
1、 windows:
到python官网下载安装包
https://www.python.org/downloads/
配置环境变量:
【右键计算机】--》【属性】--》【高级系统设置】--》【高级】--》【环境变量】--》【在第二个内容框中找到 变量名为Path 的一行,双击】 --> 【Python安装目录追加到变值值中,用 ; 分割】
如:原来的值;C:\python27,切记前面有分号
PS: python3.5和3.6在安装时可以就可以设置环境变量。安装时有个勾选。

2、linux、Mac:系统自带python,版本可能比较老,可以更新到最新版本,也可以源码安装,网络上有很多安装的帖子。

六、python第一个程序Hello World!

ljh@ljh-py:~/python/day1$ vim HelloWorld.py #用编辑器打开添加下面内容
#!/usr/bin/python #指定由python解释器来执行
print('Hello World!')

ljh@ljh-py:~/python/day1$ python HelloWorld.py #也可以用./HelloWorld.py, 前提是要给该文件有可执行权限
Hello World!

七、变量
变量定义的规则:
变量名只能是 字母、数字或下划线的任意组合
变量名的第一个字符不能是数字
以下关键字不能声明为变量名
['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']


from ctypes.test.test_pickling import name
name = "LJH"

name2 = name
print(name,name2)

name = "Jack"

print("What is the value of name2 now?",name2)

八、字符编码

九、注释
当行注视:# 被注释内容
   多行注释:""" 被注释内容 """

十、用户输入
#name = raw_input("What is your name?") #only on python 2.x
name = input("What is your name?")
print("Hello " + name )
#输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import getpass

# 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:")

# 打印输入的内容
print(pwd)

十一、模块初始
Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持
sys
import sys
print(sys.argv)

#输出
$ python test.py helo world
['test.py', 'helo', 'world'] #把执行脚本时传递的参数获取到了

os

import os

os.system("df -h") #调用系统命令
完全结合一下  

import os,sys

os.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行

十二、数据类型
1、数字:int; long; float; complex
2、布尔值:真或假 0或1
3、字符串:
常用功能:移除空白;分割;长度;索引;切片
4、列表
基本操作:索引;切片;追加;删除;长度;循环;包含
5、元组(不可变列表)
6、字典(无序)
常用操作:索引;新增;删除;键、值、键值对;循环;长度

十三、条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。
Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行

if 语句
Python中if语句的一般形式如下所示:

if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
如果 "condition_1" 为 True 将执行 "statement_block_1" 块语句
如果 "condition_1" 为False,将判断 "condition_2"
如果"condition_2" 为 True 将执行 "statement_block_2" 块语句
如果 "condition_2" 为False,将执行"statement_block_3"块语句
Python 中用 elif 代替了 else if,所以if语句的关键字为:if – elif – else。

注意:

1、每个条件后面要使用冒号(:),表示接下来是满足条件后要执行的语句块。
2、使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
3、在Python中没有switch – case语句。

# 例1:if 基本用法

flag = False
name = 'luren'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print('welcome boss') # 并输出欢迎信息
else:
print(name) # 条件不成立时输出变量名称

#例2
var1 = 100
if var1:
print ("1 - if 表达式条件为 true")
print (var1)

var2 = 0
if var2:
print ("2 - if 表达式条件为 true")
print (var2)
print ("Good bye!")

#例3:
age = int(input("请输入你家狗狗的年龄: "))
print("")
if age < 0:
print("你是在逗我吧!")
elif age == 1:
print("相当于 14 岁的人。")
elif age == 2:
print("相当于 22 岁的人。")
elif age > 2:
human = 22 + (age -2)*5
print("对应人类年龄: ", human)

### 退出提示
input("点击 enter 键退出")

while 循环
Python中while语句的一般形式:

while 判断条件:
语句
同样需要注意冒号和缩进。另外,在Python中没有do..while循环。

以下实例使用了 while 来计算 1 到 100 的总和:

n = 100

sum = 0
counter = 1
while counter <= n:
sum = sum + counter
counter += 1

print("1 到 %d 之和为: %d" % (n,sum))

while 循环使用 else 语句
在 while … else 在条件语句为 false 时执行 else 的语句块:

实例

count = 0
while count < 5:
print (count, " 小于 5")
count = count + 1
else:
print (count, " 大于或等于 5")


for 语句
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。

for循环的一般格式如下:

for <variable> in <sequence>:
<statements>
else:
<statements>

以下 for 实例中使用了 break 语句,break 语句用于跳出当前循环体:

sites = ["Baidu", "Google","Runoob","Taobao"]
for site in sites:
if site == "Runoob":
print("菜鸟教程!")
break
print("循环数据 " + site)
else:
print("没有循环数据!")
print("完成循环!")

break和continue语句及循环中的else子句
break 语句可以跳出 for 和 while 的循环体。如果你从 for 或 while 循环中终止,任何对应的循环 else 块将不执行。
continue语句被用来告诉Python跳过当前循环块中的剩余语句,然后继续进行下一轮循环。

猜你喜欢

转载自www.cnblogs.com/jianhuali/p/9171831.html