学习python的第一天,总结和记录我的学习过程
python发展及介绍
1994年V1.0
2000年V2.0,目前最高版本2.7
2008年V3.0 ,3.0不兼容之前版本
安装python环境
python下载地址:https://www.python.org/downloads/
windows 安装python,python3.0
配置环境变量
安装IDE
pycharm,目前最流行pythonIDE 工具
打印hello world
1 #! /usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 print("Hello World")
变量
变量起名有意义,建议使用下划线分割:old_man
字符编码
ascII 美国使用,编码默认使用,编程时默认保存utf-8,读写也是utf-8
GB2312支持7000+字符,在手机,设备中使用
GBK 1830 在PC使用
UTF-8,ascii码占1个字节、欧洲字符占2个字节,东亚的字符占3个字节
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3
4 name = input("请输入你的名字")5 print("my name is",name)
输入密码不可见,使用getpass模块的getpass方法
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
3
4 importgetpass5
6 username = input("username:")7 password = getpass.getpass("password:")
if else 判断和循环
if和while的使用
1 #! /usr/bin/env python
2 #-*- coding:utf-8 -*-
3
4 old_boy = 56
5 count =06 while count < 3:7 guess_age = int(input("guess_age:"))8 if guess_age ==old_boy:9 print("your are same!")10 break
11 elif guess_age <12 print are to young else:14 older count>
16 if count == 3:17 countine_confirm = input("your have guessing?")18 if countine_confirm != 'n':19 count = 0
for 循环
1 for i in range(10):2 print("---------------",i)3 for j in range(0,10,1):4 print(j)
作业
1、使用while循环输入 1 2 3 4 5 6 8 9 10
1 i = 1
2 Sum =03 while i < 101:4 Sum = Sum +i5 i += 1
6 print("1+2+3+4+5+...100=%s"%Sum)
View Code
2、求1-100的所有数的和
i = 1Sum=0while i < 101:
Sum= Sum +i
i+= 1
print("1+2+3+4+5+...100=%s"%Sum)
View Code
3、输出 1-100 内的所有奇数
i = 1
while i < 101:if i % 2 == 1:print(i)
i+= 1
View Code
4、输出 1-100 内的所有偶数
i =0while i < 101:if i % 2 ==0:print(i)
i+= 1
View Code
5、求1-2+3-4+5 ... 99的所有数的和
i = 1Sum=0while i < 100:if Sum % 2 == 1:
Sum= Sum +iif Sum % 2 ==0:
Sum= Sum -i
i+= 1
print("1-2+3-4+5-6...99=%s" % Sum)
View Code
6、用户登陆(三次机会重试)
username = "abc"password= "123"i= 1flag=Truewhile i <= 3:
_username= input("请输入用户名:")if _username ==username:whileflag:
_password= input("请输入密码:")if _password ==password:
exit("登录成功,退出")else:print("密码错误,请重新输入")continue
else:print("输入账号错误请重新输入:")
i+= 1
continuei+= 1
View Code
12>