python介绍(基础1)

版权声明:转载请注明出处,否则自行负责所有后果 https://blog.csdn.net/ljx1528/article/details/83184441

Python介绍(py基础1)
1.1 py的出生与应用

python的创始人为吉多·范罗苏姆(Guido van Rossum)。1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承。  

(龟叔:2005年加入谷歌至2012年,2013年加入Dropbox直到现在,依然掌握着Python发展的核心方向,被称为仁慈的独裁者)。

1.2 python2与python3的区别

py2.x  源码重复,冗长
py3.x  源码规范化,简单化,清晰化

1.3 py的编程语言的划分

机器语言:
  优点:执行效率高
  缺点:开发效率低,学习难度大
汇编语言:
  优点:执行效率高,不如机器语言快
  缺点:学习难度大,开发效率低,比机器语言稍微好点
高级语言:

1.3.1 解释型:

代码逐行解释,逐行运行。
代表语言: python,php
优点:开发效率高,跨平台
缺点:执行效率低

1.3.2 编译型:

将代码一次性全部变易成二进制,然后执行。
代表语言:C  C++
优点: 执行效率高
缺点: 开发效率低,不能跨平台。

1.4 py的优缺点
1.4.1 优点:

    1、开发效率高,python含有N多个第三方库。
	2、高级语言,不用考虑底层或者内存级别。
	3、可拓展性。可以加入C++的程序。
	4、可嵌入性。可将python代码嵌入到C++程序中。
	5、可移植性。不同平台的移植。

1.5 缺点:

    1、执行速度相对慢。
	2、代码不能加密。
	3、线程不能利用多CPU。

1.6 py的种类

    1、Cpython: 官方推荐,用C语言写的。
	2、Jpython: 可以在java平台上运行的python。可以解释成java识别的字节码。
	3、Ironpython: 可以在.net平台上运行的,可以解释成.net识别的字节码。
	4、Ipython: 在Cpython基础上拓展的,交互式解释器。
	5、pypy: 利用JIT技术,实现了动态编译,执行速度非常快。

1.7 运行第一个python程序

print (‘hello world’)
python3.x:中英文都可以
python2.x:英文字母,特殊字符,数字都行,中文会报错。
           中文解决方法:# _*_ encoding:utf-8 _*_

1.8 变量(可变的量)
将软件的一些目录放到计算机的指定位置,方便在终端中使用。
1.8.1 why:

		'''
		print(1+2+3+4+5)
		print((1+2+3+4+5)*3/2)
		print(((((1+2+3+4+5)*3/2))+100)/22)
		'''
		x = 1+2+3+4+5
		y = x*3/2
		z = (y+100)/22
		print(z)

1.8.2 what:

变量,将程序运算的中间结果这哪是存储起来,以便后续程序调用。

1.8.3 how:

	1,变量必须要有数字,字母,下划线,任意组合。
	2,变量不能数字开头。
	3,不能是python中的关键字。
		 ['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,变量要具有可描述性。
		name = 12 错
	5,变量不建议使用中文。
	6,变量不能过长。
		fdhsjkfdsakfljfkl = 12

1.8.4 官方推荐:(变量定义的方式)
1.8.4.1 驼峰体

	AgeOfOldboy = 56
	NumberOfStudents = 80

1.8.4.2 下划线(推荐方式)

	age_of_oldboy = 56
	number_of_students = 80

1.8.5 where:

程序中无处不在,想要暂存一些数据,经常使用的。

1.9 常量

	why: 生活中π,身份证号,G,等等不变的量,程序中也是如此。
	what:常量,一直不变的量。
	how:将变量全部大写,常量。
		NAME = 'alex'
	where:	设置一些不变的量,放在文件的最上面。

1.10 注释
1.10.1 why:

学习中,文言文会出现一些晦涩难懂,典故,这样需要注释。

1.10.2 what:

帮助你解释说明。

1.10.3 how:

	单行注释:#
	多行注释:''' 被注释内容 '''  """ 被注释内容 """
	注释不宜多,宜精。

1.10.4 where:

	晦涩难懂的程序语句。
	函数,类,文件。

1.11 基础数据类型初识
1.11.1 why:
1.11.1.1 整数(int)

1,2,3,,30,100 ...
	主要用于计算,计数,+-*/%		 	 		 		 		
  在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647 		
  在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807

1.11.1.2 字符串(str)

在python中,凡是用引号引起来的数据都是字符串
单引号,双引号,三引号引起来没有任何区别
name = '太白jin星'
name = "太白jin星"
name = '''太白jin星'''
单双引号配合使用: msg = "My name is Alex , I'm 22 years old!"
多引号用于换行
msg = '''
今天我想写首小诗,
歌颂我的同桌,
你看他那乌黑的短发,
好像一只炸毛鸡。
'''
print(msg)

1.11.1.3 布尔值(bool)

True., False
	 作用:用于逻辑运算的判断

1.12 用户交互input

name = input('请输入姓名:')
age = input('请输入年龄:')
sex = input('请输入性别:')
msg = "我叫" + name + "今年" + age + "性别" + sex
print(msg)

1.13 流程控制语句if
1.13.1 a、单独if

if 2 > 3:
    print(666)
print(333)

1.13.2 b、if else. 二必选一

name = input('>>>')
if name == 'alex':
	print('He is SB')
else:
	print(666)

1.13.3 c、if elif elif …

num = int(input('>>>'))

if num == 6:
    print('请你吃饭')
elif num == 4:
    print('跟我回家...')
elif num == 2:
    print('大宝剑')

1.13.4 d、if elif elif … else
1.13.4.1 例子1:

num = int(input('>>>'))
if num == 6:
    print('请你吃饭')
elif num == 4:
    print('跟我回家...')
elif num == 2:
    print('大宝剑')
else:
    print('太笨了你....')
print(666)

1.13.4.1 例子2:

score = int(input("输入分数:"))
if score > 100:
    print("我擦,最高分才100...")
elif score >= 90:
    print("A")
elif score >= 60:
    print("C")
elif score >= 80:
    print("B")
elif score >= 40:
    print("D")
else:
    print("太笨了...E")

1.13.5 e、if 嵌套

username = input('请输入用户名:')
password = input('请输入密码:')

if username == 'alex' :
	if password == 'sb':
	    print('成功登陆')
	else:
	    print('密码错误')
else:
    print('用户名错误')

1.14 流程控制语句while
1.14.1 例子1:

while True:
    print('忐忑')
    print('大悲咒')
    print('海草')
    print('想太多')

1.14.2 例子2:

flag = True
while flag:
   print(111)
   print(222)
   flag = False
   print(333)

1.14.3 例子3:

flag = True
while flag:
   print(111)
   print(222)
   flag = False
   print(333)
print(777)

1.14.4 例子4:

flag = True
count = 1
while flag:
    print(count)
    count = count + 1
    if count == 101:
	    flag = False   


count = 1
while count < 101:
    print(count)
    count = count + 1 # count += 1

1.14.5 例子5:

# break 循环中只要遇到break就直接跳出循环
while True:
    print(111)
    print(222)
    break
    print(333)
	
flag = True
while flag:
   print(111)
   print(222)
   flag = False
   print(333)

1.14.6 例子6:

# continue: 遇到continue,跳出本次循环,继续下一次循环
while True:
    print(111)
    print(222)
    continue
    print(333)

1.14.7 例子7:

# while else: 只要while循环被break打断,则不走else程序。
count = 0
while count <= 5 :
    count += 1
    print("Loop",count)
    if count == 3: 
	    break
else:
    print("循环正常执行完啦")
print("-----out of while loop ------")

1.14.8 例子8:

count = 1
while  count <= 3:
    username = input('name>>: ')
    password = input('pass>>: ')
    if username == 'ljx' and password == '123456':
        print ('登录成功')
        while True:
            cmd = input('cmd >>: ')
            print('继续输入命令')
            if cmd == 'q':
                break
        break
    else:
        print('用户名或密码错误')
        print('还剩%d次机会' %(3 - count))
    count += 1

1.14.9 例子9:

count = 1
tag = True
while tag:
    if count > 3:
        break
    username = input('pls your name: ')
    password = input('pls your pass: ')
    if username == 'ljx' and password == '123456':
        print('登录成功!!!')
        while tag:
            cmd = input('cmd >>>: ')
            print('继续输入命令')
            if cmd == 'q':
                tag = False
    else:
        print('用户名或密码错误!')
        print('输入错误,还剩%d次机会' %(3-count))
    count += 1

1.15 文件头

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

猜你喜欢

转载自blog.csdn.net/ljx1528/article/details/83184441