2021年9月开始学习Python,编程基础0。
学习内容,有些囫囵吞枣了,找不到基础方向。有自己心急的原因,想尽快学习成功。
大忌,改正。
学习内容:
1、Python安装,和基本的文件开发安装。
2、基础
2.1 Python在交互环境中的运行。
cmd中输入 python +相关路径及运行文件
2.2 语法特点
(1)注释。
单行注释用#
多行注释'''........'''
(2)文件编码声明注释。Python3中默认的是UTF-8
如果想换编码,在第一行需要注明,如下:# -*- coding:gbk -*-
也可以为:#coding=gbk
2.3 代码缩进。
缩进标识一个代码块的开始,结束标识一个代码块的结束。用缩进或者 : 标识。
2.4 编写规范
每个import语句只导入一模块,
不要字啊伟航添加分好“;”
每行最多80个字符,超过的话,建议用“()”将多行内容隐式的连接起来
2.5 命名规范
模块、包名尽量短小;大写,特殊字符用“_”;
3、保留与字符
3.1 保留字(注意,大小写的区分)
可用代码执行:import keyword keyword.kwlist
and as assert break class continue def del elif else
except finally for from False global if import in is
lambda nonlocal not None or pass raise return try True while with yield
3.2 标识符及变量类型
不能使用特殊字符、数字作为开头,建议用英文
加 " " 为字符串,标识类型为 str ;str() 函数为将数值转换为字符串
数值类的 1024,表示类型为 int
3.3 常用转义字符及其说明
\ 续航符
\n 换行符
\0 空
\t 水平制表符,用于横向跳到下一制表位
\" 双引号
\' 单引号
\\ 一个反斜杠
\f 换页
\0dd 八进制,dd代表字符,如\012代表换行
\xhh 十六进制数,hh代表字符,如\x0a代表换行
3.4 常用转换函数及作用
int(x) 转为整数类型
float(x) 转为浮点数类型
complex(real[,imag]) 创建一个复数
str(x) 将x转换为字符串
repr(x) 将x转换为表达式字符串
eval(x) 计算字符串中有效的Python表达式,并返回一个对象
chr(x) 将整数 x 转换为一个字符
ord(x) 将一个字符转换为它对应的整数值
hex(x) 转为16进制
oct(x) 转为8进制
bin(x) 转为二进制
3.5 赋值
例: x +=y, x=x+y,加赋值
% 整除余数; //,取整除赋值; 3**2,为3的平方
3.6 位运算符(二进制在运算)
&,位与 ; |,位或; ~,位取反; ^,位异或
<<,左移位; >>,右移位
4、流程控制语句
if; if else; while; for(for 迭代变量 in range());
循环嵌套:while..........:(while......:)
continue
for 迭代变量 in 对象:
if 条件表达式:
continue
pass语句
小作业尝试:
1、手机充值业务,简易:
print("欢迎使用xxx充值业务,请输入充值金额:") money = int(input("充值金额:")) print("充值成功,您本次充值",money,"元")
2、计算孩子身高
2.1 father_height =int(input("父亲的身高:")) Mather_height = int(input("母亲的身高:")) son_height = father_height*Mather_height*0.54 print("儿子的身高:",son_height)
2.2无限制计算次数:
count = 0 while True: father_height =int(input("父亲的身高:")) Mather_height = int(input("母亲的身高:")) son_height = father_height*Mather_height*0.54 print("儿子的身高:",son_height) count +=1
或者
while True:
father_height =int(input("父亲的身高:"))
Mather_height = int(input("母亲的身高:"))
son_height = father_height*Mather_height*0.54
print("儿子的身高:",son_height)
continue
暂时为找出如何不用count计数,一直操作的情况。
2.3限制计算次数后再来一次:
count = 0
while count <1:
father_height =int(input("父亲的身高:"))
Mather_height = int(input("母亲的身高:"))
son_height = father_height*Mather_height*0.54
print("儿子的身高:",son_height)
count +=1
if count == 1:
countinue_confirm =input("再来一次?")
if countinue_confirm !='n':
count = 0
3、九九乘法表
for i in range(1,10): for j in range(1,i+1): print(str(j) + "×" + str(i) + "=" + str(i * j) + "\t",end='') #"\t"为同行空位书写,end=''这个是同行和最终换行用 print('')
4、连7拍腿游戏
(1)逢7的倍数
for number in range(1,100): if number% 7 ==0: print(number)
(2)计7的倍数的次数
total = 99
for number in range(1,100):
if number% 7 ==0:
continue
else:
string = str(number) #
total -=1
print("拍腿次数:",total,"次")
(3)逢7拍腿
total = 99 for number in range(1,100): if number% 7 ==0: continue else: string = str(number) if string.endswith('7'): #字符块以7结尾的 continue total -=1 print("拍腿次数:",total,"次")
4、猜数字游戏
number = 5 for i in range(11): guess_number = int(input("请输入0~10中任何数字:")) if guess_number ==number: print("congratulation,you are right!") break elif guess_number > number : print("bigger!reinput") elif guess_number < number : print("smaller!reinput")