2、python——缩进问题、内建函数、python的应用领域、PEP8规则、语法检查工具

156 阅读4分钟

目录

1、缩进问题

2、内建函数

3、id

4、help

5、dir

6、type

7、python的应用领域

8、input   

8.1、若要进行数值计算,要进行类型转换

8.2、隐藏密码输入,涉及到getpass模块

9、print功能大赏

9.1、sep

9.2、end

9.3、file

10、python之禅

11、PEP8规范

12、语法检查工具

12.1、Pycharm语法检查工具

12.2、flake8语法检查工具


1、缩进问题

#python中使用缩进来区分代码段

#同一等级的代码,不要使用缩进
#每条语句的后面可以添加分号,也可以不添加
#一行中的输入多条语句,使用;隔开
#续航使用\隔开
a = 2
if a == 1:   #python以缩进在确定代码段的,且这里是":"不是";"
    print("ok")  #这个代码段表示前边的命令成立才能够执行,所以要顶格写

2、内建函数

#不需要额外的操作,拿来就用,内嵌在解释器内部
 print("sanchuang")    #向屏幕输出你传入的参数

3、id

#查看对象的内存地址

a = "sanchuang"
print(id(a))

4、help

#  查看帮助信息

help(print)   #函数也可以作为参数去传入

5、dir

#查看对象的属性

#属性的使用  通过  变量名,属性名   方式去访问
print(dir(a))
print(a.count('a'))

带__的不用理会,后边的才是该对象的属性
这个为print(a.count('a'))输出的结果,a="sanchuang",就是统计了该字符串中a的个数

6、type

#查看对象类型

#int  整形
#str  字符串类型
b = 10
print(type(a))
print(type(b))

7、python的应用领域

# 1、爬虫
# 2、大数据(海量的数据) —— 数据分析,数据挖掘  —— pandas
# 3、运维方向,脚本  —— 平台 (自动化)  节省时间、成本,减少人为失误
# 4、web开发  ———— 小游戏  flask
# 5、人工智能 —— 机器学习

8、input   

#input  接受用户从键盘的输入,接受的类型都是字符串
username = input("please input your name:")
password = input("please input password:")
print(username, type(username))
print(password, type(password))

8.1、若要进行数值计算,要进行类型转换

因为input接收的是字符串类型

username = input("please input your name:")
password = int(input("please input password:"))
print(username, type(username))
print(password, type(int(password)))

 

username = input("please input your name:")
password = int(input("please input password:"))
print("username", type(username))   #普通字符串需要打引号,变量名不用打引号
print(password, type(int(password)))

8.2、隐藏密码输入,涉及到getpass模块

#getpass  这是一个模块
import getpass
username = input("please input your name:")
password = getpass.getpass("please input password")
print("your name is:", username)
print("your password is:", password)

这个用xshell里的python3来运行,PyCharm对这个隐藏输入不友好,但是在PyCharm终端(Terminal)里边可以运行

9、print功能大赏

# help(print)
# print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

9.1、sep

print('a', 'b', 'c', 7)
# sep 是指定分隔符,之前默认为空格,现在改为&&&
print('a', 'b', 'c', 7, sep='&&&')

9.2、end

# end:指定追加符
print(1, end="%")
print(2, end="%")
print(3)

9.3、file

# file指定输出到哪里,默认输出到屏幕,还可以输出到文件
# 以读写的形式打开文件file.txt
f = open("file.txt", 'w+')
print("a", "b", "c", file=f)

 

10、python之禅

import this

11、PEP8规范

12、语法检查工具

12.1、Pycharm语法检查工具

12.2、flake8语法检查工具