【Python 有用的知识】——简单易操作的python小知识

110 阅读2分钟

点个赞留个关注吧!!

目录

一、获取电脑用户名

二、字符串大小写

三、运算条件(|,&==or,and)

四、判断是否联网

五、使用默认浏览器打开指定网址

一、获取电脑用户名

import getpass    # 导入模块

user_name = getpass.getuser()    
print(user_name)    # 打印用户名

二、字符串大小写

a= 'abcdeFGHIJK'
print(a.lower())   # 转为小写   abcdefjhijk
print(a.upper())   # 转为大写   ABCDEFGHIJK

三、运算条件(|,&==or,and)

# | == or     只要有一个条件满足则为True
# & == and    必须所有条件满足则为True

# '''| or'''
q = input('请问你是爷爷、奶奶、爸爸还是妈妈:')
if (q == '爷爷') | (q == '爸爸'):   # 只要满足一个条件True  则进行执行
    print('你是男性')
else:
    print('你是女性')

e = input('请问你是爷爷、奶奶、爸爸还是妈妈:')
if (e == '爷爷') or (e == '爸爸'):   # 只要满足一个条件True  则进行执行
    print('你是男性')
else:
    print('你是女性')


# '''& and'''
r = input('请问你考试考了多少分(满分100):')
if (r > '60') & (r < '80'):      # 必须满足所有条件True  才会执行
    print('及格')

t = input('请问你考试考了多少分(满分100):')
if (t > '60') and (t < '80'):      # 必须满足所有条件True  才会执行
    print('及格')

四、判断是否联网

import pywifi
from pywifi import const

wifi = pywifi.PyWiFi()  # 抓取网卡接口
iface = wifi.interfaces()[0]  # 获取网卡
if iface.status() == const.IFACE_CONNECTED:
    print('已连接网络')
else:
    print('网络有误!!!')

五、使用默认浏览器打开指定网址

import webbrowser

webbrowser.open('www.baidu.com')