一、模块初识
Python的强大之处在于他有强大的标准款和三方库,几乎你想实现的功能都有相应的Python库支持。
sys
import sys
print(sys.path)#打印环境变量
print(sys.argv)#
import os #跟操作系统交互
# os.system("dir") #system执行命令,不保存结果
cmd_res = os.popen("dir").read()
print("--->",cmd_res)
os.makedir("new_dir")
os
import os
# cmd_res = os.system("dir")
# print("--->" , cmd_res)
cmd_res = os.popen("dir").read() #内存对象地址
print("--->" , cmd_res)
# os.makedirs("new_dir")
msg = '我是一个兵'
print(msg)
print(msg.encode(encoding='utf-8'))
print('------->',msg.encode(encoding='utf-8').decode(encoding='utf-8'))
python 的运行过程
当Python程序运行时,编译的结果是保存在位于内存中的PycodeObject中,当Python 程序运行结束时,Python解释器则将PycodeObject 写回到pyc文件中。
二次运行时程序会在硬盘中寻找pyc 并与源代码比较那个最新,如果源代码最新则重新编译,反之则直接运行。
二、数据类型
1、byte类型
2、三元运算
result = 值1 if 条件 else 值2
如果条件为真:result = 值1
如果条件为假:result = 值2
a ,b,c = 1,2,3
d = a if bprint(d)
3、整型
int 32位机器 只能存-2**31 --2**31-1,即4亿多个数字
64位机器 存 -2**63--2**63-1
long 长整型
float 浮点 包含小数点
4、布尔值
真或假
1或0
msg = '我爱你中国'
print(msg)
print(msg.encode(encoding='utf-8'))
print((msg.encode(encoding='utf-8')).decode(encoding='utf-8'))使用
5、列表的使用
names = ["zhangyang", "abc", "lucy", "xiaoming"]
names.append("leihaidong") #增加
names.insert(1,"chenronghua")#特点位置增加
names[2]= "dog" #改
#删除
# names.remove("chenronghua")
# del names[1]
#names.pop() #默认删除最后一个,输入下标
print(names)
print(names[0])
print(names[0:2])
print(names[-1])
print(names[-2:])
print(names.index("dog"))#查询位置
print(names[names.index("dog")])#查询位置
print(names.count("chenronghua")) #查询重复几个
#print(names.clear())#清空
names.reverse() #反转
print(names)
names.sort()#按ASCII顺序排序
print(names)
names2 = [1,2,3,4]
names.extend(names2)
print(names,names2)
#del names2
names = ["zhangyang", "abc", "lucy",["alex","jack"], "xiaoming"]
name2 = names.copy()
print(names)
print(name2)
names[2] = '潘金莲'
names[3][0]= "ALEXANDER"
print(names)
print(name2)
#深copy
import copy
names = ["zhangyang", "abc", "lucy",["alex","jack"], "xiaoming"]
name2 = copy.deepcopy(names)
print(names)
print(name2)
names[2] = '潘金莲'
names[3][0]= "ALEXANDER"
print(names)
print(name2)
for i in names:
print(i)
names = ["zhangyang", "abc", "lucy",["alex","jack"], "xiaoming"]
print(names[0:-1:2])
print(names[::2])
用户交互程序
# 用户交互程序
username = input("username")
passwd = input("password")
print(username,passwd)
print("-----------------------------------------")
##################
name = input("name")
age = input("age:")
job = input("job:")
salary = input("Salary")
info0 = '''
-------- info of '''+ name +'''----------
Name:''' + name + '''
Age:'''+ age + '''
jog:''' + job + '''
salary:''' + salary
info1 = '''
--------info of %s ---------
Name:%s
Age:%s
Job:%s
Salary:%s
''' %(name,name,age,job,salary)
print(info1)
# %s 是占位符 %d 代表整型
info2= '''
--------info of {_name} ---------
Name:{_name}
Age:{_age}
Job:{_jog}
Salary:{_salary}
'''.format(_name = name,
_age = age,
_jog= job,
_salary = salary)
info3= '''
--------info of {0}---------
Name:{0}
Age:{1}
Job:{2}
Salary:{3}
'''.format(name,age,job,salary)
print(info3)
#不推荐 info0(占用较多内存) 推荐使用info2 方式
password.py
#Author:Hiro
# import getpass
# username = input("username:")
# password = getpass.getpass("password:") #在pycharm不好用 只能在命令行
# print(username,password)
#####--------------------------
_username = 'alex'
_passwd = 'abc123'
username = input("username:")
password = input("password:")
if _username == username and _passwd == password:
print("Welcom user {name} login...".format(name = username))
else:
print("Invilid username or password pls try again")
#----------------------------------------
age_of_oldboy = 56
guess_age = int(input("guess age :"))
if guess_age == age_of_oldboy:
print("yes,you got it")
elif guess_age > age_of_oldboy:
print("think smaller")
else:
print("think bigger")