基础&运算符
今日概要
- 循环
- 字符串格式化
- 运算符
- 编码
- 博客&git
内容回顾&补充
-
计算机基础
-
安装解释器
- py2
- py3
-
语法
- print/input
- 整形int /字符串 str / 布尔值 boolen
- 条件语句
- and运算符
- 变量
-
练习
评分规则: A >= 90 B >= 80 C >= 70 D 其他 # PEP8规范 score=input('请输入成绩') #不规范 score = int(input('请输入成绩')) if score >=90: print('A') elif score>=80: print('B') elif score>=70: print('C') else: print('D')
-
问题
- if 缩进
- pycharm —code — reformatcode #自动遵循PEP8规范
- 字符串之间比较是否字符相同,字符是否在字符串中
- 数字不能与字符串拼接也不能与字符串比较
-
实例
message = '''欢迎致电10086 1.话费查询; 2.流量办理; 3.业务办理; 4.人工服务''' print(message) index = int(input('请输入你要选择的服务:')) if index == 1: print('话费查询') elif index == 2: print('流量服务') elif index == 3: content='''业务办理 1.修改密码; 2.更改套餐; 3.停机;''' value = int(input('请输入要办理的业务:')) if value == 1: print('修改密码') elif value == 2: print('更改套餐') elif value == 3: print('停机') else: print('错误') elif index == 4: print('人工服务') else: print('输入错误') #此代码嵌套太多,并不规范
今日内容
while
-
循环语句
while True: print('死循环') #while True --> print -->while True... while 1>0: print('死循环') count = 1 value = count + 1 print(value) count_one = 1 count_one = count_one + 1 print('count_one')
通过循环让count每次循环都加1 count = 1 while True: print(count) count += 1 通过循环, 1 2 3 。。10 count = 1 while count <= 10: print(count) count += 1
在 pycharm中先打断点,然后通过debug模式运行就可以看到代码执行过程
实例
#打印1 2 3 4 5 6 8 9 10 count = 1 while count<=10: if count != 7: print(count) count += 1 count = 1 while count<=10: if count == 7: pass else: print(count) count += 1
-
while else结构
-
关键字:break 终止当前循环
while True: print(666) break print('结束')
-
练习
通过break实现1~10 count = 1 while True: if count == 10: break count += 1 print('结束') while True: #break 终止当前循环 print('你好') while True: print(666) break break
-
关键字:continue
count = 1 while count <= 10: print(count) continue #本次循环如果遇到continue,则不在继续往下走,而是回到while条件位置。 count += 1 示例:1234568910 count = 1 while count <= 10: if count == 7: count += 1 continue print(count) count += 1
-
while else
count = 1 while count < 10: print(count) count = count + 1 else: # 不在满足while后的条件时,触发。 或 条件 = False print('else代码块') print('结束') count = 1 while True: print(count) if count == 10: break count = count + 1 else: print('else代码块') print('结束')
-
总结
- while 基本结构
- break
- continue
- while else
-
字符串格式化
name = input('')
do = input('')
temp = "%s在家时,%s"%(name,do)
print(temp)
temp = "我是%s,年龄%d,职业%s."%("alex",73,'酱鸡腿')
print(temp)
name='alex'
temp = '%s现在手机电量是100%%'%(name)
print(temp)
- %s #字符串占位符
- %d #时间占位符
- %% #符号%
运算符
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c *= a 等效于 c = c * a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a |
%= | 取模赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c **= a 等效于 c = c ** a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
value = 11 % 3
print (value)
练习题:打印1~100之间的奇数。
count = 1
while count <= 100:
val = count % 2
if val == 1:
print(count)
count += 1
+ - * / // %
练习题:1~100之间所有的数相加
total = 0
count = 1
while count <=100:
total = total + count
count += 1
print(total)
count = 1
while count <= 10:
print(count)
count +=1 # count = count + 1
数字转字符串
v1=6666
v2=str(v1)
字符串转数字
v1="666"
v2=int(v1)
数字转布尔值
v1=9
v2=bool(v1)
print(v2) # True
v1=0 #只有数字为0
v2=bool(v1)
print(v2) # False
字符串转布尔值
v1="0"
v2 = bool(v1)
print(v2) #都是True
v1="" #空字符串
v2 = bool(v1)
print(v2) #False
布尔值转为其他
v1=True
v2=int(v1)
print(v2) #1
v1=False
v2=int(v1)
print(v2) #0
#转字符串
v1=True
v2=str(v1)
print(v2) #True
v1=False
v2=str(v1)
print(v2) #False
对于or,如果遇到
value = 1 or 9
第一个值如果是转成布尔值如果是真,则value=第一值
第一个值如果是转成布尔值如果是假,则value=第二值
如果有多个or条件,则从左到右依次进行上述流程.
示例:
v1= 0 or 1
v2 = 8 or 10
v3 = 0 or 9 or 8
对于and,如果遇到
第一个值如果是转成布尔值如果是True,则value=第二值
第一个值如果是转成布尔值如果是False,则value=第一值
如果有多个and条件,则从左到右依次进行上述流程.
示例:
v1 = 1 and 9
v2 = 1 and 0
v3 = 0 and 7
v4 = 0 and ""
v5 = 1 and 0 and 9
编码
- 编码扩展
- ascii
- Unicode
- ecs2
- ecs4
- uft-8 ,中文用3字节
- utf-16
- gbk ,中文用2字节
- gb2312 ,中文用2字节
- 单位
- 单位转换
博客
博客园开博客
-
注册
-
申请博客
-
写博客
- 随笔 随便看
- 文章 ,有url能看
- 日记 自己看
- 皮肤
- 选项 markdown
- typora 源码模式发布博客
git
-
安装git软件
-
码云注册(保存代码)
-
写作业并提交码云
-
在某个文件夹下写作业
-
写完之后,在此文加下gitbash
-
- git init,初始化文件夹 - git add . 将当前文件夹中的所有文件收集出来 - git commit -m "第一天作业" 做个记录 - git config --global user.email "名字@邮箱" - git config --global user.name "名字" - git remote add origin https://gitee.com/yihutu/Python.git #执行一次,给url起别名 origin - git push -uorigin master
-