目录:
一、初识Python
二、python2.x与python3.x的区别
三、变量字符编码
四、用户输入
五、数据类型
六、格式化输出
七、if - else 循环
八、while 循环
九、for 循环
一、Python 了解
编程语言主要从以下几个角度为进行分类,编译型和解释型
静态语言和动态语言、
强类型定义语言和弱类型定义语言
编译器是把源程序的每一条语句都编译成机器语言,并保存成二进制文件,这样运行时计算机可以直接以机器语言来运行此程序,速度很快;
而解释器则是只在执行程序时,才一条一条的解释成机器语言给计算机来执行,所以运行速度是不如编译后的程序运行的快的.
编译型
优点:编译器一般会有预编译的过程对代码进行优化。因为编译只做一次,运行时不需要编译,所以编译型语言的程序执行效率高。可以脱离语言环境独立运行。
缺点:编译之后如果需要修改就需要整个模块重新编译。编译的时候根据对应的运行环境生成机器码,不同的操作系统之间移植就会有问题,需要根据运行的操作系统环境编译不同的可执行文件。
解释型
优点:有良好的平台兼容性,在任何环境中都可以运行,前提是安装了解释器(虚拟机)。灵活,修改代码的时候直接修改就可以,可以快速部署,不用停机维护。
缺点:每次运行的时候都要解释一遍,性能上不如编译型语言。
二、python与python3的区别
(1)在python3里是默认就支持UTF8格式,不需要在程序开头表明
(2)在python2里的 raw_input 就等于 3里的 input raw_input 2.x == input 3.x
以后慢慢补充:
(3)某些库更改了名称
三、变量字符编码
1 #_*_coding:utf-8_*_ 2 # Author:land 3 4 age_of_cat = 22
5 name = "land"
变量定义的规则:
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 以下关键字不能声明为变量名
#驼峰体
AgeOfOldboy = 56 #即首字母大写
NumberOfStudents = 80
#下划线(推荐使用)
age_of_oldboy = 56
number_of_students = 80
变量三要素:id,type,value
#1 等号比较的是value,
#2 is比较的是id
#强调:
#1. id相同,意味着type和value必定相同
#2. value相同type肯定相同,但id可能不同,如下
四、用户输入
1 name = input("name:")
2 age = int(input("age:")) #类型转化
3 print(type(age))
4 job = input("job:")
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1 # Author:land
2 import getpass
3 username = input("username:")
4 #password = getpass.getpass("password:")
5 # getpass 在pycharm中不好使
6 password = input("password:")
7
8 print(username,password)
五、数据类型
1、数字
2 是一个整数的例子。
长整数 不过是大一些的整数。
3.23和52.3E-4是浮点数的例子。E标记表示10的幂。在这里,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是复数的例子,其中-5,4为实数,j为虚数,数学中表示复数是什么?。
int(整型)
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
跟C语言不同,Python的长整数没有指定位宽,即:Python没有限制长整数数值的大小,但实际上由于机器内存有限,我们使用的长整数数值不可能无限大。
注意,自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数,所以如今在长整数数据后面不加字母L也不会导致严重后果了。
float(浮点型)
浮点数用来处理实数,即带有小数的数字。类似于C语言中的double类型,占8个字节(64位),其中52位表示底,11位表示指数,剩下的一位表示符号。
complex(复数)
复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
"hello world"
name
=
"land"
print
"i am %s "
%
name
#字符串是 %s;整数 %d;浮点数%f
字符串常用功能:
- 移除空白
- 分割
- 长度
- 索引
- 切片
1 name_list = ['gaogaofan', 'seven', 'eric']
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
5、元组(不可变列表)
6、字典(无序)
1 person = {"name": "land", 'age': 22}
2 或
3 person = dict({"name": "land", 'age': 22})
# Author:land info = { 'name':'gaofan', 'hobbies':['basketball','eat','sleep'], 'company_info':{ 'company_name':'EasyStack', 'type':'OpenStack', 'how_many_people':200, } } print(info) #打印信息 print(info['company_info']['company_name']) #取出公司名称 students = [ {'name':'gaogaofan','age':18,'sex':'famel','hobbies':['basketball','run','swamming']}, {'name':'renrenji','age':19,'sex':'boy','hobbies':['eat','run']}, {'name':'dadachang','age':20,'sex':'man','hobbies':['eat','sleep']}, ] print(students) print(students[0]) #取出列表第一个元素 print(students[0]['name']) #取出第一个元素里的name print(students[0]['hobbies'][1]) #取出hobby里的run
常用操作:
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
六、格式化输出
#格式化输出 #%s是占位符,可以接受字符串,也可以接受数字 name='renjie' age='18' print('My name is %s and age is %s !!!'%(name,age)) #%d数字占位符:只能接收数字 print('My name is %s and age is %d !!!'%(name,age)) #会报错因为此时age是字符串
name=input('your name>>:') age=input('your age>>:') job=input('your job>>:') hobby=input('hobby>>:') height=input('your height>>:') mes_g = ''' ----------info is %s--------- name : %s age : %s job : %s hobby : %s height : %s '''%(name,name,age,job,hobby,height) print(mes_g)
七、if -----else 循环
1、
#练习一
girle_age = 20 height = 170 weight = 110 if girle_age > 18: print('Helle,Can I add a WeChat?') else: print('Bye Bye') if girle_age >= 18 and height > 165 and weight == 110: print("Your are so beautifull !!!") else: print('Bye Bye ')
#练习二
9 age_of_cat = 22
10
11 for i in range(3):
12 guess_age = int(input("guess age:"))
13 if guess_age == age_of_cat:
14 print("Yes,you got it")
15 break
16 elif guess_age > age_of_cat:
17 print("think smaller.....")
18 elif guess_age < age_of_cat:
19 print("think bigger....")
20
21 else:
22 print("You have tried too many times....fuck off!!!")
#练习三
today = input('>>:') if today == "Monday": print('Continue Work....') elif today == "Tuesday": print('Continue Work....') elif today == "Wednesday": print('Continue Work....') elif today == "Thursday": print('Continue Work....') elif today == "Friday": print('Continue Work....') elif today == "Saturday": print('See a movie....') elif today == "Sunday": print('On a Date....') else: print(''' Your must inpute one Monday to Sunday ''')
#练习四: today = input('>>>:') if today == 'Saturday' or today == 'Sunday': print('Today is happy day.......') elif today == 'Monday' or today == 'Tuesday' or today == 'Wend' or today == 'Thursday' or today == 'Fdjnvd': print('Working..............') else: print(''' must inpute one day ''')
#练习五
1 today = input('>>>:') 2 if today in ['Saturday','Sunday']: 3 print("today is happy ....") 4 elif today in ['Monday','Tuesday','Wednesday','Thursday','Friday']: 5 print('Today is a Work day ----') 6 else: 7 print('gun gun gun .............')
八、while 循环
1 # Author:land
2 count = 0
3 while True:
4 print("count:",count)
5 count = count + 1
6 if count == 10:
7 break ##break用于退出本层循环
#打印0-10 count = 0 while count <= 10: print('count:',count) count += 1 #打印0-10之间的偶数 while count <= 10: if count%2 == 0: print('count:',count) count += 1
1 #打印0-10之间的奇数 2 while count <=10: 3 if count%2 == 1: 4 print('conut:',count) 5 count += 1
1 #练习,要求如下: 2 # 1 循环验证用户输入的用户名与密码 3 # 2 认证通过后,运行用户重复执行命令 4 # 3 当用户输入命令为quit时,则退出整个程序 5 name = 'gaofan' 6 passwd = 'zxcvbnm' 7 while True: 8 int_name = input('Inpute you name>>:') 9 int_passwd = input('Inpute you passwd>>:') 10 if int_name == name and int_passwd == passwd: 11 while True: 12 cmd = input('inpute you cmd>>:') 13 if not cmd: 14 continue 15 if cmd == 'q': 16 break 17 print('run %s' %cmd) 18 else: 19 print('username or passwd is error !!!') 20 continue 21 break
1 #使用tag 2 name = 'gaofan' 3 passwd = 'zxcvbnm' 4 tag = True 5 while tag: 6 int_name = input('inpute your name>>>:') 7 int_passwd = input('inpute your passwd>>>:') 8 if int_name == name and int_passwd == passwd: 9 while tag: 10 cmd = input('inpute you want cmd:') 11 if not cmd: 12 continue 13 if cmd == 'q': 14 tag = False 15 continue 16 print('run %s...'%cmd) 17 else: 18 print("username or passd is error,please inpute again")
1 #1. 使用while循环输出1 2 3 4 5 6 9 10 2 count = 0 3 while count <=9: 4 count += 1 5 if count == 7 or count == 8: 6 continue 7 print(count)
count = 1
while count <= 10:
if count == 7:
count += 1
continue
print(count)
count +=
1 #求1-100的所有数的和 2 count = 1 3 sum = 0 4 while count <=100: 5 sum += count 6 count += 1 7 print(sum)
1 #3. 输出 1-100 内的所有奇数 2 count = 1 3 while count <= 100: 4 if count%2 == 1: 5 print(count) 6 count += 1
1 #4. 输出 1-100 内的所有偶数 2 count = 1 3 while count <= 100: 4 if count%2 == 0: # 或者 if count%2 != 1 5 print(count) 6 count += 1
1 #5. 求1-2+3-4+5 ... 99的所有数的和 2 res = 0 3 count = 1 4 while count <= 99: 5 if count%2 == 0: 6 res -= count 7 else: 8 res += count 9 count += 1 10 print(res)
1 #6. 用户登陆(三次机会重试) 2 count = 0 3 while count < 3: 4 name = input('name:') 5 passd = input('passwd:') 6 if name == 'gaofan' and passwd == 'zxcvbnm': 7 print('login success...') 8 break 9 else: 10 print('username or passwd is error.....') 11 count += 1
1 #7要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出 2 girle_age = 18 3 count = 0 4 while count < 3: 5 int_age = input('inpute you want age>>>:') 6 if int_age == girle_age: 7 print('login success......') 8 break 9 count += 1
1 #要求:允许用户最多尝试3次 2 # 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 3 # 就继续让其猜3次,以此往复,如果回答N或n,就退出程序 4 # 如何猜对了,就直接退出程序 5 6 girle_age = 18 7 count = 0 8 while True: 9 if count == 3: 10 choice = input('contuine inpute Y OR break inpute N >>>:') 11 if choice == 'Y': 12 count = 0 13 elif choice == 'N': 14 break 15 else: 16 print('please input Y or N....') 17 18 guess = int(input('inpute you guess>>>:')) #注意把字符串转化为整型 19 if guess == girle_age: 20 print('you got it.....') 21 break 22 count += 1
1 # 让用户输入用户名密码 2 # 认证成功后显示欢迎信息 3 # 输错三次后退出程序 并打印错误提示: 4 dic2 = {'name':'gaofan','passwd':'123456'} 5 count = 0 6 while count <= 3: 7 if count == 3: 8 print('You inpute error more three....') 9 break 10 int_name = input('Inpute your username>>>:') 11 int_passwd = input('Inpute your passwd>>>:') 12 if int_name == dic2['name'] and int_passwd == dic2['passwd']: 13 print('loging success......') 14 break 15 else: 16 count += 1
1 #支持多个用户登录 (通过字典存多个账户信息) 2 #用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(把用户锁定的状态存到文件里) 3 4 #定义用多个户名密码 5 dic={ 6 'renji':{'passwd':'zxcvbnm'}, 7 'gaofan':{'passwd':'zxcvbnm'}, 8 'dachang':{'passwd':'zxcvbnm'}, 9 } 10 f = open('db.txt','r') 11 lock_user = f.readlines() 12 f.close() 13 count = 0 14 count1 = 0 15 16 while True: 17 int_name = input('Please inpute your name>>:') 18 if count == 3: 19 print('Too much inpute and you already locked') 20 break 21 if not int_name in dic: 22 print('username is error...!') 23 count += 1 24 if int_name in lock_user: 25 print('username already locked...!!') 26 exit() 27 28 if int_name in dic: 29 count -= 2 30 int_passwd = input('Please inpute your passwd>>:') 31 if int_passwd == dic[int_name]['passwd']: 32 print('login success.....!') 33 break 34 else: 35 print('secret is error...!!!') 36 count1 += 1 37 if count1 == 2: 38 print('you inpute error times more than three, will be locked..eee!!!') 39 f = open('db.txt','w') 40 f.write('%s'%int_name) 41 f.close() 42 break
九、for 循环
1 # Author:land
2 for i in range(0,10):
3 if i < 5:
4 print("loop",i)
5 else:
6 continue
#continue用于退出本次循环,继续下一次循环
1 # Author:land 2 # age_of_cat = 22 3 # guess_age = int(input("guess age:")) 4 # 5 # if guess_age == age_of_cat: 6 # print("Yes,you got it") 7 # elif guess_age > age_of_cat: 8 # print("think smaller.....") 9 # elif guess_age < age_of_cat: 10 # print("think bigger....") 11 12 age_of_cat = 22 13 count = 0 14 while count < 3: 15 guess_age = int(input("guess age:")) 16 if guess_age == age_of_cat: 17 print("Yes,you got it") 18 break 19 elif guess_age > age_of_cat: 20 print("think smaller.....") 21 elif guess_age < age_of_cat: 22 print("think bigger....") 23 count += 1 24 if count == 3: 25 continue_confirm = input("do you wang continue guess?:") 26 if continue_confirm != "n": 27 count = 0 28 29 30 #else: 31 # print("You have tried too many times....fuck off!!!")