python基础一(连续更新)
2018-11-07 14:44
169 查看
1.python历史
-
1.1宏观上:python2 与 python3 区别
python2 源码不标准,混乱,重复代码太多, python3 统一 标准,去除重复代码。
2.python的环境
-
2.1 编译型:一次性将所有程序编译成二进制文件
缺点:开发效率低,不能跨平台。 优点:运行速度快。 同类型:C,C++等等。
-
2.2 解释型:当程序执行时,一行一行的解释
优点:开发效率高,可以跨平台。 缺点:运行速度慢。 同类型:python ,php,等等。
-
2.3 运行第一个py文件
python3x :python 文件路径 回车 python2x :python2 文件路径 回车 python2 python3 区别:python2默认编码方式是ascii码 解决方式:在文件的首行:#-*- encoding:utf-8 -*- python3 默认编码方式utf-8
3.变量
变量:就是将一些运算的中间结果暂存到内存中,以便后续代码调用。
-
3.1必须由数字,字母,下划线任意组合,且不能数字开头。
dou _isu s23 都是可以的 123 1sa #112 %23 &dsh *sa11_ 都是不可以的
-
3.2不能是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']
-
3.3变量具有可描述性
-
3.4不能是中文
4.常量
一直不变的量。 π BIR_OF_CHINA = 1949
5.注释
方便自己方便他人理解代码 单行注释:# 多行注释:'''被注释内容''' """被注释内容"""
6.用户交互—input
-
6.1等待输入
-
6.2将你输入的内容赋值给了前面变量
-
6.3input出来的数据类型全部是str
例如:
7.基础数据类型初始
数字:int 12,3,45 + - * / ** % 取余数
例如:
a = 10 % 3 print(a) ####此运行结果为1
8.if
if 条件:
结果
if a > b # 如果a 大于b print(a) # 输出a else: # 输出a print(b) # 输出b
9.while
while 条件: 循环体 无限循环。 终止循环: 1,改变条件,使其不成立。 2,break 3. continue 4. while TRUE 没有while 1 的效率高
count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" #######代码结果######### The count is: 0 Thecount is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!
10.练习题
-
10.1 使用while循环输入 1 2 3 4 5 6 8 9 10
-
10.2 求1-100的所有数的和
-
10.3 输出 1-100 内的所有奇数
-
10.4 输出 1-100 内的所有偶数
-
10.5求1-2+3-4+5 … 99的所有数的和
-
10.6 用户登陆(三次机会重试)
11 .答案在此,请做完之后查看
# 1.使用while循环输入 1 2 3 4 5 6 8 9 10 i = 0 while i < 10: i += 1 if i == 7: continue #或者pass else: print(i) # 总结:break是直接跳出循环,continue是跳出本次循环,然后继续执行下次循环;pass只是一个占位符的作用; # 2、求1-100的所有数的和 i = 1 sum = 0 while i <= 100: sum += i i = i + 1 print(sum) # 3、输出 1-100 内的所有奇数 i = 0 sum = 0 while i < 100: i = i + 1 if i % 2 != 0: sum += i print(sum) # 4、输出 1-100 内的所有偶数 i = 0 sum = 0 while i < 100: i = i + 1 if i % 2 == 0: sum += i print(sum) # 5、求1-2+3-4+5 ... 99的所有数的和 # 方式一: i = 0 sum = 0 while i < 100: i = i + 1 if i % 2 != 0: sum += i else: sum -= i print(sum) # 方式二: i = 0 sum1 = 0 sum2 = 0 while i<100: i = i + 1 num1 = i%2 if num1 == 1: sum1 = sum1 + i else: sum2 = sum2 + i print(sum1-sum2) # 6.用户登陆(三次机会重试) i = 0 while i < 3: username = input('请输入账号:') password = int(input('请输入密码:')) if username == '咸鱼哥' and password == 123: print('登录成功') else: print('登录失败请重新登录') i += 1
相关文章推荐
- 移除连续重复的字符串元素 分类: python 小练习 python基础学习 divide into python 2014-01-01 15:53 392人阅读 评论(0) 收藏
- python基础内容概要(实时更新)
- Python基础练习代码持续更新。。。。。。。
- Python基础教程(第2版 修订版) 勘误合集 *持续更新
- Python 3.5基础学习(一) ----更新
- Python基础语法——持续更新中
- python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
- python基础——持续更新中
- python基础知识总结(不断更新)
- Python基础笔记(持续更新)
- Python系统学习系列——Python基础(1)(持续更新)
- Python基础笔记,后续更新
- python基础知识总结(不断更新。。。。)
- python基础(持续更新)
- Python基础、函数及其相关函数库(Numpy、TensorFlow)常用函数集锦(更新中,更新日期:2017-08-03)
- python自保留基础语法(不断更新)
- Python 基础语法(四)
- Python数据分析:scikit-learn基础(一)
- 跟我一起学python(一),python基础