Python入门
1.关于Python注释
# 用#单行注释
"""
三引号多行注释
"""
2.关于解释器
- #!/usr/bin/env python属于使用linux系统时采用的位置指定
- # -*- coding: utf-8 -*- 用于python2,python3时加入了utf-8编码
3.变量
- 变量赋值
name1 = "wupeiqi" name2 = "alex"
- 变量命名
- 变量名只能是 字母、数字或下划线的任意组合
- 变量名的第一个字符不能是数字
- 变量不能是关键字
4.input输入
n1 = input("请输入用户名:") n2 = input('请输入密码:') print(n1) print(n2)
5..条件语句
1.if基本语句
if:
代码块
代码块
else:
代码块
print("....")
代码块缩进四格 用TAP键
2.嵌套
if:
if:
....
else:
....
else:
...
3.if elif (和C语言不同处)
temp = input("请输入你的成绩:") score = int(temp) if 90 <= score <=100: print("A") elif 80 <= score < 90: print("B") elif 60 <= score < 80: print("C") elif 0 <= score <= 60: print("D") else : print("输入错误")
***如果if语句后面不想执行,不能空着采用pass
if n==1:
pass
else:
print("....")
6.基本数据类型
字符串(引号):
a1="vcc"
a2='2dd'
a1='''6ddd'''
**无论用单引号还是双引号前后保持一致
加法:
a1 = "fjy"
a2 = "hello"
a3 = a1 + a2
print(a3)
fjyhello
乘法:
a1 = "fjy"
a2 = a1*10
print(a2)
fjyfjyfjyfjyfjyfjyfjyfjyfjyfjy
数字:
加减乘除,次方,余
a1=42
a2=20
a3=a1+a2
a3=a1-a2
a3=a1*a2
a3=a1/a2 # 结果为2.1
a3=4**4 # **表示几次方
a3=a1%a2 # 得到余数为2
a3=a1//a2 # 表示商为2
判断奇数偶数
a2 = input("请输入一个数字: ")
a = int(a2)
a3 = a % 2
if a3==0:
print("这是个偶数")
else:
print("这是个奇数")
*** 关于input输入,如果进行数字判断要转换为int型