Python 基础学习 1
一.Python的第一个程序
1 print("hello,world!")
二.变量的命名规则
1.数字字母下划线组成
2.不能以数字开头,不能有特殊字符和空格
3.不能以保留字命名
4.不能以中文命名
5.定义的变量名应该有意义
6.驼峰式命.下划线分割单词
7.变量名区分大小
三.if条件判断
1 if a<b:
2 print("Yes")
3 else:
4 Print("No")
1 if a<b:
2 print("a<b")
3 elif a==b:
4 print("a==b")
5 else:
6 print("a>b")
四.注释方法
1 #单行注释
2 """多行注释"""
3 '''多行注释'''
五.Python输入
1 input("请输入:")
六.Python输出
1 print("asd"+"jkl")
字符串的拼接使用“+”,int型与string型不能拼接,需要强制类型转换。
七.算术运算符
加+ 减- 乘* 除/ 取余% 取商// 幂运算**
八.逻辑运算符
非not 并and 或or 优先级依次为not,and,or。
九.print()end结尾
1 print()#默认为print(end="\n"),想要输出在一行可写为print(end="")
十.while循环
1 n=0
2 while n<10:
3 print(n)
4 n+=1
break为终止当前循环体,continue为结束当次循环。
1 age=30
2 while True:
3 input_age = int(input("Age is :"))
4 if input_age == age:
5 print("It's right.")
6 break
7 elif input_age > age:
8 print("It's bigger.")
9 else:
10 print("It's smaller.")
11 print("End")
十一.while和else的配套使用
1 num = 1
2 while num <= 5:
3 num += 1
4 print(num)
5 else:
6 print("This is else statement")
如果使用break,那么else也不会执行。
十二.while的嵌套使用--输出九九乘法表
1 row = 1
2 while row<=9:
3 col = 1
4 while col <= row:
5 print( str(col)+"*"+ str(row) +"="+str(col * row), end="\t")
6 col += 1
7 print()
8 row += 1

浙公网安备 33010602011771号