python第一课_python基础,python第一课

学习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

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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的所有数的和

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

i = 1Sum=0while i < 101:

Sum= Sum +i

i+= 1

print("1+2+3+4+5+...100=%s"%Sum)

View Code

3、输出 1-100 内的所有奇数

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

i = 1

while i < 101:if i % 2 == 1:print(i)

i+= 1

View Code

4、输出 1-100 内的所有偶数

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

i =0while i < 101:if i % 2 ==0:print(i)

i+= 1

View Code

5、求1-2+3-4+5 ... 99的所有数的和

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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、用户登陆(三次机会重试)

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值