python基础笔记
一、变量
定义变量的规则
1、变量名只能是字母、数字、下划线的组合
2、变量名不能以数字开头
3、python中使用的关键字不能作为变量
注意:行业默认规则
1、变量命令见名知意
2、驼峰
3、字母全大写代表是常量
# 例子
name = "Mr Liao"
name1 = name
print(name, name1)
name = "Mr Lan"
print(name, name1)
二、用户交互
name = input("name:")
age = input("age:")
job = input("job")
salary = input("salary:")
info1 = '''
--- info of %s ---
name:%s
age:%s
job:%s
salary:%s
''' % (name, name, age, job, salary)
print(info1)
# 总结info1:可以用%来连接,后边括号的变量要对应,不然会报错
info2 = '''
--- info of {_name} ---
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''.format(_name=name, _age=age, _job=job, _salary=salary)
print(info2)
# 总结info2:中括号中可以用自定义名字,在format中要赋值
三、循环
1、while循环
age = 56
count = 0 #计数器
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age:
print("you get it")
break #跳出当前整个循环,下边的else不会执行
elif guess_age > age:
print("think smaller")
else:
print("think bigger")
count += 1 #计数器自增1
else: #上边代码正常执行完才执行
print("你猜的次数太多了,拜拜")
2、if语句
if条件判断的两种语法:
1)、单支判断
if 条件语句: #条件成立执行下边语句,也可以不要else
执行语句
else:
执行语句
2)、多分支判断:
if 条件判断:
执行语句
elif 条件判断:
执行语句
elif ...
执行语句
else:
执行语句
# 例子1:对输入的用户名和密码进行验证
_username = "张三"
_password = "123456"
username = input("username:")
password = input("password:")
if _username == username and _password == password:
print("welcome {name} login".format(name=username))
else:
print("invalid username or password")
print("-" * 30)
# 例子2:猜年龄大小
age = 56
guess_age = int(input("guess age:"))
if guess_age == age:
print("you get it")
elif guess_age > age:
print("think smaller")
else:
print("think bigger")
3、for循环
# 栗子1
for loop in range(10):
print(loop)
print("分割线" + "-"*30)
# 栗子2
for i in range(0, 10, 2): # range(起始位置,结束位置,步长),默认步长是1 ,括号里是左闭右开,即(1,3)是1,2不包含3
print(i)
print("分割线" + "-"*30)
# 栗子3
age = 56
for i in range(3): # range(3)即 0 1 2 循环三次
guess_age = int(input("guess age:"))
if guess_age == age:
print("you get it")
break # 跳出当前整个循环,下边的else不会执行
elif guess_age > age:
print("think smaller")
else:
print("think bigger")
else: # 上边代码正常执行完才执行
print("你猜的次数太多了,拜拜")
4、三元运算符
三元运算符语法:result = 值1 if 条件 else 值2
1、如果条件为真,result = 值1
2、如果条件为假,result = 值2
# 例子
a, b, c = 1, 3, 5
d = a if a > b else c
5、循环代码优化
#需求:猜数字,猜三次,超过三次要询问是否要继续,如果输入n,结束,否则继续猜
age = 56
count = 0 #计数器
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age:
print("you get it")
break #跳出整个循环,下边的else不会执行
elif guess_age > age:
print("think smaller")
else:
print("think bigger")
count += 1 #计数器自增1
if count == 3:
continue_confirm = input("请问你是否要继续?")
if continue_confirm != "n": # 如果输入了n,count计算器归0,循环再次开启
count = 0
四、模块初识
# python强大之处在于有非常强大的标准库和第三方库
import sys
import os
print(sys.path) # 打印环境变量
cmd_res1 = os.system("dir") # 只打印,不保存结果
cmd_res2 = os.popen("dir") # 打印并保存为一个对象
cmd_res3 = os.popen("dir").read() # 用read()方法来取出
print(cmd_res3)
未完待续...... 2019.12.15
When nothing seems to help, I go look at a stonecutter hammering away at his rock, perhaps a hundred times without as much as a crack showing in it. Yet at the hundred and first blow it will split in two, and I know it was not that blow that did it, but all that had gone before. -- Jacob Riis