一、input输入函数
注意点:input从键盘获取的数据,无论输入的是什么,得到的都是字符串类型
a = 1 while (a != '0'): a = input("please input number: ") print(a) print(type(a))
所以这里如果想要进行数值相加就会有问题
a = 1 while (a != '0'): a = input("please input a number: ") b = input("please input b number: ") print(a+b) print(type(a))
这个问题解决只需要数值转化就可以了
a = 1 while (a != '0'): a = int(input("please input a number: ")) b = int(input("please input b number: ")) print(a+b) print(type(a))
二、格式化字符输出
print(1) print(1,2,3,4) a = 1 b = 2.11834 c = 'hello' # %的格式化相关的 print("% ---- " + "a = %d b = %f c = %s" % (a,b,c)) print() # +拼接符 c += '---你好' print(c) print() # f'' 格式化输出 print("f'' ---- " + f'a = {a} b = {b} c = {c}') print() #format()输出 print("format() --- "+'a = {0} b = {1} c = {2}'.format(a,b,c)) #format()输出的参数下标举例 print("format() --- "+'a = {0} b = {1} c = {2} d = {0} e = {0} f = {1} '.format(a,b,c)) #format()输出占位符 s = 'a = {0:5d} b = {1:.2f} c = {2}'.format(a,b,c) print(s)
三、函数的基本操作
1.return返回值的问题
def get_num(): return 1 return 2 return 3 print(get_num())
2.参数传递
参数要一对一
def say_hi(name): #形参 print("Hello",name) say_hi("tom") #实参 say_hi("timi") #实参 say_hi("jack") #实参 def my_sum(a,b): #形参 print(a+b) my_sum(1,2) #实参 my_sum(2,5) #实参
3.函数为参
def get_num(a,b): k = input("请输入一个数字:") k = int(k) return k+a+b print(get_num(get_num(1,2),2))
四、运算符
1.关系比较符
def test_logic(): print(1 == 2) #False print(1 == 1) #True print('*' * 10) #十个* print(1 != 2) #True print(1 > 2) #False print(2 > 1) #True print(1 < 2) #True print(2 < 1) #False
2.逻辑运算符
def test_logic(): print(True and True) #False print(True and False) #True print(True or False) # True print(False or True) # True print(True or True) # True print(False or False) # False print(not True) # False print(not False) # True
五、if语句
1.基本应用
def is_net(age): if age >= 18: print("成年了") is_net(20)
2.if-else
def is_net(age): if age >= 18: print("成年了") else: print("未成年") is_net(20) is_net(15)
3.if-elif语句
def is_week_day(day): if day == '1' or day == '一': print('星期一') elif day == '2' or day == '二': print('星期二') elif day == '3' or day == '三': print('星期三') elif day == '4' or day == '四': print('星期四') elif day == '5' or day == '五': print('星期五') elif day == '6' or day == '六': print('星期六') elif day == '7' or day == '七': print('星期天') d = input("请输入:") is_week_day(d)
六、随机数
import random #导入模块 s = 1 while s: n = random.randint(0,2) #[0,1,2] 列表中的随机数 s = int(input("输入0断开循环:")) print("n: " , n)
七、循环
1.while语句的基本应用
import random #导入模块 s = 1 while s: n = random.randint(0,2) #[0,1,2] 列表中的随机数 s = int(input("输入0断开循环:")) print("n: " , n)
2.break语句
break用于循环里的作用是用来结束循环的,不管循环有多少次
import random #导入模块 while True: n = random.randint(0,2) #[0,1,2] 列表中的随机数 s = input("输入0断开循环:") if s == '0': break print("n: " , n)
2.continue语句
continue的作用是用来本次循环,直接进入下次循环
import random #导入模块 s = 1 while s: n = random.randint(0,2) #[0,1,2] 列表中的随机数 s = int(input("输入0断开循环:")) if s == 2: continue print("n: " , n)
3.猜拳游戏
import random #导入模块 n = random.randint(0,2) #[0,1,2] 列表中的随机数 r = int(input("请输入要出的东西0代表拳头,1代表布,2代表剪刀:")) while r != 3: if n == 0: if n == r: print("平局!") if r == 2: print("你输了!") if r == 1: print("你赢了!") elif n == 1: if n == r: print("平局!") if r == 2: print("你赢了!") if r == 0: print("你输了!") elif n == 2: if n == r: print("平局!") if r == 0: print("你赢了!") if r == 1: print("你输了!") n = random.randint(0, 2) # [0,1,2] 列表中的随机数 r = int(input("请输入要出的东西0代表拳头,1代表布,2代表剪刀,3代表结束:"))
4.三目运算符
python的三目运算符
值1 if 条件 else 值2
def test_func(): n = int(input('number: ')) s = '偶数' if n % 2 == 0 else '奇数' print(s) test_func()
5.嵌套while
输出五行*
i = 0 while i < 5: j = 0 while j < 5: print('*',end=" ") j += 1 print() i += 1
经典九九乘法表
i = 1 while i <= 9: j = 1 while j <= i: print(i,'*',j,'=',i*j,end=" ") j += 1 print() i += 1
6.for循环
range是迭代
for i in range(10): print(i)
7.for else 和 while else
for i in range(5): print(i) else: print("for over") i = 0 while i < 5: print(i) i += 1 else: print("while over")
break对于else的影响
for i in range(5): print(i) if i == 3: break else: print("for over") i = 0 while i < 5: print(i) i += 1 else: print("while over")
break打断循环不会让else执行
总结
开个新坑,最近因为一些原因要高强度学python,所以会高强度搬自己的学习笔记,希望大家多多指点,多多👍