python基础 day1 变量、常量、基础数据类型、input、if
1.编程语言分类
-
编译型
将代码一次性全部编译成二进制,然后再执行
优点:执行效率高
缺点:开发效率低,不能跨平台
代表语言:C
-
解释型
逐行解释成二进制,逐行运行
优点:开发效率高,可以跨平台
缺点:执行效率低
代表语言:python
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']
- 不能使用中文
- 不能过长
- 要具有描述性
- 推荐命名方式
- 驼峰体:AgeOfOldboy = 73
- 下划线:age_of_oldboy = 73
3.常量
- 生活中一直不变的:身份证号码,名字
- python中没有真正的常量,全部大写的变量称之为常量
- 将变量全部大写,放在文件的最上面
NAME = '杨森'
print(NAME)
4.注释
-
解释说明,便于理解代码
-
单行注释:#
-
多行注释:“”“被注释内容”“”,或者‘’‘被注释内容’‘’
-
难以理解的代码,函数,类,文件都需要注释,解释说明
5.基础数据类型
-
int(整型):1,2,123……
-
str(字符串):python中凡是用引号引起来的数据就称之为字符串
注:字符串可以与数字相乘
-
bool:只有两种形态True False
判断一个变量的数据类型
s1 = 100
s2 = '100'
print(s1,type(s1))
print(s2,tpye(s2))
6.用户交互input
- 网页、APP上输入账号密码
- input 出来的全部都是字符串类型
username = input('请输入用户名:')
password = input('请输入密码:')
print(username,',您好!')
print(username, type(username))
print(password, type(password))
小练习:让用户输入姓名,性别,年龄,然后打印一句话‘我叫: ,性别: ,今年: 。’
name = input('请输入您的姓名:')
sex = input('请输入您的性别:')
age = input('请输入您的年龄:')
msg = '我叫' + name + ',性别' + sex + ',今年' + age
print(msg)
7.流程控制语句if
- 单独if
if 1 < 2:
print("aloha")
- if else
age = int(input("请输入您的年龄:")) # 把字符串转换成数字
if age >= 18:
print("你可以做羞羞的事情了[滑稽]")
else:
print("回家喝奶去吧")
- if elife elife ……
number = 6
guess_num = int(input("猜数字(1-10):"))
if guess_num < number:
print("猜小了")
elif guess_num > number:
print("猜大了")
else:
print("猜对了")
- 嵌套的if
username = input("please enter your name:")
password = input("please enter your password:")
code = 'abcd'
enter_code = input("please enter code:")
if enter_code == code:
if username == '杨森':
if password == '1234':
print("Welcome to the Python World!")
else:
print("password error")
else:
print("user does not exist!")
else:
print("code error!")