python全栈笔记
1、文件首行用来指定编码方式:
#-*- encoding:utf-8 -*-
2、变量:
(1)必须是由数字、字母、下划线任意组合,且不能数字开头。
(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)变量名必须具有可描述性。建议用下划线隔开,例如:age_of_oldboy。
(4)不能是中文。
#设置变量x,yx = (1+2+3+4)
print(x)
print(x*5)
y = x*5
print (y+100-45+2)
#变量指向age1 = 12
age2 = age1
age3 = age2
age2 = 100
print (age1,age2,age3)
3、常量:
(1)π
(2)大写一般为常量
(3)其他
BIR_OF_CHINA = 19491001
4、注释:
单行注释:#
多行注释:"""注释内容""" ,"""注释内容"""
5、基础数据类型初识:
print (x,type(x)) #判断数字类型
(1)int 运算符:+, - ,*, / ,%, ** ,//。
(2)str python当中凡是用引号引起来的都是字符串。
print("i"m a student") #字符串可以用单引号/双引号包括,如果字符串内有单引号,就用双引号包括,反之则用单引号。a = "steven"
b = "lily"
c = a + b #字符串可相加:字符串的拼接。
print(c)
$stevenlily
d = a * 2 #字串符可想乘:字符串打印几次 str * int
print (d)
msg = """锄禾日当午,
汗滴禾下土。
谁知盘中餐,
粒粒皆辛苦。
"""#三个单引号引起来并予以赋值变量,变成了一个长字符串,而不是注释的作用了。print(msg)
(3)bool 返回 Ture or False
a = 3b
= 2print (a>b) #判断True False
6、用户交互:input
(1)等待输入
(2)将你输入的内容赋值给了前面的变量
(3)input出来的数据类型全部是str
#用户交互name = input ("请输入你的名字:")
age = input ("请输入你的年龄:")
print ("我的名字是"+name,"我的年龄是"+age+"岁")
$请输入你的名字:x
请输入你的年龄:y
我的名字是x 我的年龄是y岁
7、条件语句
(1)if:
if...if #两个条件都会执行
if...elif #若if条件成立则不会执行elif条件。
if 5 > 4 :print (666)print (777)print (111)if True :print (666)print (777)if False :print (555)print(777)
if 4 > 5 :print ("我请你喝酒")else :print ("喝什么酒")if 4 > 5 :print ("我请你喝酒")print ("喝什么酒")
num = input ("请输入您猜的数字:")if num == "1" :print ("恭喜你,答对了")elif num == "2" :print ("恭喜你,答对了")elif num == "3" :print ("恭喜你,答对了")else :print ("你猜错了")
scroe = int(input("请输入您的分数:")) #int(str)改变数据类型if scroe >= 90 :
print ("A")
elif scroe >= 80 :
print ("B")
elif scroe >= 70 :
print ("C")
elif scroe >= 60 :
print ("D")
else :
print ("you"re fail!")
输入分数评定等级,90以上A,80以上B,70以上C,60以上D以及不及格
username = input ("请输入用户名:")password
= input ("请输入密码:")if username == "xinxin" :if password == "abcd1234" :print ("欢迎您!!!")else :print ("密码错误!!!")else :print ("无此账号!!!")
if的嵌套:输入账号密码,正确提示欢迎,错误提示错误。账号错误提示无此账号
(2)while
a、无限循环
#无限循环print("开始")
while True :
print("我们不一样")
print("在人间")
print("痒")
ptint("结束")
b、终止循环:
改变条件,使其不成立。
#方法一:count = 1
flag = True
#标志位
while flag :
print (count)
count = count + 1
if count > 100 :
flag = False
#方法二:
count = 1
while count <= 100 :
print (count)
count = count + 1
#方法三
count = 1
while 1 :
print (count)
count = count + 1
if count > 100 :
break
计数1~100
# i = 0#
sum = 0#
while i < 100:#
i = i + 1#
sum = sum + i#
print(sum)
相加1~100
break。直接终止
print("开始")while True:print("222")print("333")breakprint("444")
print("结束")
i = 0while 1:i
= i + 1if i > 100:
break
print(i)
break计数1~100
continue。跳出循环重新开始执行
print ("开始")count
= 1while count <= 100 :
print (count)
count = count + 1
continue
continue计数1~100
i = 0while i < 100:i
= i +1if 95 > i > 5:
continue
print(i)
不回显6到94间的数字
(3)while循环使用else语句
count = 0while count < 5:print(count,"小于5")count
+=1else:
print(count,"大于或等于5")
(4)for 循环:
ss = "1A2B3C4D5E6F!?"for i in ss: # i是变量,ss可以是列表,元组,字典。i 在 ss中有限循环。
print(i)
#场景应用:敏感词。
sss = "你好,hello!"
if"hello"in sss:
print("您的评论中有英文")
课后练习
#方法1:count = 0
while count < 101:
print(count)
count = count + 2
#方法2:
count = 1
while count < 101:
if count % 2 == 0:
print (count)
count = count + 1
#方法3:
count = 1
while True :
if count % 2 == 0 :
print (count)
count = count + 1
if count >=101 :
break
练习1:1-100求偶数
#方法1:count = 1
while count < 101:
print(count)
count = count + 2 #count += 2
#方法2:
count = 1
while count < 101:
if count % 2 == 1:
print (count)
count = count + 1
#方法3:
count = 1
while True:
if count % 2 == 1:
print (count)
count = count + 1
if count >=101:
break
练习2:1-100求奇数
#方法1:i = 1
sum1 = 0
sum2 = 0
while i < 100:
if i % 2 == 0:
sum1 = sum1 + i
else:
sum2 = sum2 + i
i = i + 1
print(sum2 - sum1)
#方法2:
i = 1
sum = 0
while i < 100:
if i % 2 == 0:
sum = sum - i
else:
sum = sum + i
i = i + 1
print(sum)
#方法3:
i = 0
j = -1
sum = 0
while i < 99:
i = i + 1
j = -j
if i % 2 == 0:
sum = sum - i
else:
sum = sum + i
print(sum)
练习3:求1 - 2 + 3 - 4... + 99 所有数的和
# 方法1:i = 1
sum1 = 0
while i < 100:
if i == 88:
i = i + 1
continue
elif i % 2 != 0:
sum1 = sum1 + i
else:
sum1 = sum1 - i
i = i + 1
print(sum1)
#方法2:
i = 0
j = -1
sum = 0
while i < 99:
i = i + 1
j = -j
if i == 88:
continue
else:
sum = sum + i * j
print(sum)
拓展练习3:计算1 -2 + 3 ... + 99 中除了88以外的所有数的总和
# 计算1 -2 + 3 ... - 99 中除了88以外的所有数的总和i = 1
sum1 = 0
while i < 100:
if i == 88:
i = i + 1
continue
elif i % 2 != 0:
if i == 99:
i = i + 1
sum1 = sum1 - i
sum1 = sum1 + i
else:
sum1 = sum1 - i
i = i + 1
print(sum1)
拓展练习3:计算1 -2 + 3 ... - 99 中除了88以外的所有数的总和
#方法1:count = 0
while count < 10:
count = count + 1
if count == 7:
print ("")
else:
print(count)
#方法2:最优解
count = 0
while count < 10:
count = count + 1
if count == 7:
continue
print(count)
#方法3:
count = 0
while count < 10:
count = count + 1
if count == 7:
pass
else:
print(count)
练习4:使用while循环输入1 2 3 4 5 6 8 9 10
i = 0while i < 3:username
= input ("请输入用户名:")password
= input ("请输入密码:")if username == "xinxin"and password == "abcd1234" :print ("欢迎您!!!")else :print ("错误!请重新输入")i
+= 1
练习5:用户登录(三次机会)
name = "xinxin"pwd
= "123456"i1
= 3i2
= 3while i2 > 0:
mz = input("请输入用户名:")
i1 = 3
if mz == name:
while i1 > 0:
mm = input("请输入密码:")
i1 = i1 - 1
if mm == pwd:
print("欢迎您,登录成功")
break
else:
print("请重新输入密码")
if i1 == 0:
print("次数已经用完,请重新登录。")
break
else:
print("用户名错误,请重新输入:")
i2 = i2 - 1
if i2 == 0:
print("次数已经用完,请重新登录。")
break
已经实现用户名密码分别三次循环
8、格式化输出:
(1)%占位符
%s 字符串
%d 数字
%% 百分号输出
name = input("请输入姓名:")age
= input("请输入年龄:")job
= input("请输入工作:")hobbie
= input("请输入兴趣:")msg
= """--------------- info of %s ---------------name: %s
age: %d
job: %s
hobbie:%s
我的心情指数:100%%
--------------- end ---------------
""" %(name,name,int(age),job,hobbie)print(msg)
username = "xinxin"password
= "abcd1234"i
= 0while i < 3:name
= input("请输入账号:")psw
= input("请输入密码:")if name == username and psw == password:print("欢迎您")breakelse:
print("账号密码错误,请重新输入,剩下%d次机会" % (2 - i))
if (2-i) == 0:
result =input("是否还想再试试?Yes")
if result == "Yes":
i = 0
continue
i = i + 1
else:print("goodbye")
#思考自己写的这个如何进入上述代码的循环
username = "xinxin"
password = "abcd1234"
i = 0
while i < 3:
name = input("请输入账号:")
psw = input("请输入密码:")
if name == username and psw == password:
print("欢迎您")
break
else:
if name == username and psw != password:
print("密码错误,剩下%d次机会" % (2 - i))
elif name != username and psw != password:
print("账号密码错误,请重新输入,剩下%d次机会" % (2 - i))
i += 1
用户登录(三次机会)
(2)format
# format 格式化输出的三种玩法:#
第一种:s3 = """my name is {}, i am {} year old,
i like {}, again, i am {}.""".format("xxx","02","playgame","xxx")
print(s3)
# 第二种:
s4 = """my name is {0}, i am {1} year old,
i like {2}, again, i am {0}.""".format("xxx","02","playgame")
print(s4)
n = input("name:")
a = input("age:")
h = input("hobby:")
s4 = """my name is {0}, i am {1} year old,
i like {2}, again, i am {0}.""".format(n,a,h)
print(s4)
# 第三种:
n = input("name:")
a = input("age:")
h = input("hobby:")
s5 = """my name is {name}, i am {age} year old,
i like {hobby}, again, i am {name}.""".format(name=n,age=a,hobby=h)
print(s5)
9、初始编码:电脑的传输,储存实际上都是用0和1来表示。
(1)ASCII码:设计之初只为美国本土考虑,7位足以,作者预留了1位(bit),遂形成了8位表示一个字节(byte),且所有的ASCII码最左边一位全部都是“0”
8bit == 1byte
1024byte == 1kb
1024kb == 1MB
1024MB == 1GB
1024GB == 1TB
Bin(二进制)
Oct(八进制)
Dec(十进制)
Hex(十六进制)
缩写/字符
解释
0000 0000
0
0
00
NUL(null)
空字符
0000 0001
1
1
01
SOH(start of headline)
标题开始
0000 0010
2
2
02
STX (start of text)
正文开始
0000 0011
3
3
03
ETX (end of text)
正文结束
0000 0100
4
4
04
EOT (end of transmission)
传输结束
0000 0101
5
5
05
ENQ (enquiry)
请求
0000 0110
以上是 python全栈笔记 的全部内容, 来源链接: utcz.com/z/538094.html