Python面试100讲-Chapter 1-优快云

Python面试100讲-优快云

Chapter 1:基础

1. 导入Python模块

1. 导入Python模块的基本方式

import math
from math import cos,sin
from math import *

2. 为导入的Python模块指定别名

import math as m
from math import cos as c

2. 设置Python模块搜索路径的方式

1. 设置Python模块的搜索路径有几种方式

  1. 设置PYTHONPATH环境变量
  2. 添加.pth文件
  3. 通过sys.path设置路径
  4. 如果使用Pycharm,可以直接设置搜索路径

2. 永久设置Python模块搜索路径有几种方式,如何使用它们

  1. 设置PYTHONPATH环境变量
  2. 添加.pth文件
    • .pth文件放在python安装目录/site-packages
    • .pth文件名任意命名,内容为包的绝对路径,回车分割
  3. 如果使用Pycharm,可以直接设置搜索路径

3. 如何临时设置Python模块的搜索路径

  1. 通过sys.path设置路径
import sys
sys.path.append('package_path')

3. 不同数据类型的变量或值首尾相接

1. 字符串与字符串之间连接有几种方式

  1. 使用 + 连接
s1='hello'
s2='world'
s = s1 + s2
print(s)
  1. 直接连接
s = "hello""world"
print(s)
  1. 使用,(逗号)连接,标准输出的重定向
from io import StringIO
import sys
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
print("hello","world")
sys.stdout = old_stdout #恢复标准输出
result_str = result.getvalue()
print("用逗号连接",result_str)
  1. 格式化
s1='hello'
s2='world'
s = '<%s><%s>' % (s1,s2)
print(s)
  1. join方法
s1='hello'
s2='world'
s = ' '.join([s1,s2])
print(s)

2. 字符串如何与非字符串之间连接

  1. 使用加号(+),先将其他类型转换为str
n = 20 
s = 'hello' + str(n)
print(s)
  1. 格式化
s='<%s><%d><%.2f>' %('hello',20,0.04)
print(s)

3. 字符串与对象连接时如何让对象输出特定内容

使用魔术方法 __str__

class MyClass(object):
	def __str__(self):
		return 'This is my class'
my=MyClass()
s = 'hello' + str(my)
print(s)
helloThis is my class

4. 十进制、二进制、八进制和十六进制的互相转换

目标

  1. 如何表示不同进制的数值
  2. 进制之间的转换

1. 如何表示二进制、八进制和十六进制

#十进制
n1=1234
#二进制
n2=0b1101001
print(n2) #105
#八进制
n3=0o127
print(n3) #87
#十六进制
n4=0xF15
print(n4) #3861

2. 二进制、八进制和十六进制如何直接转换

# 十进制转换为二进制
print(bin(120)) # 0b1111000
print(type(bin(120))) # <class 'str'>

# 二进制转为十进制
print(int('10110', 2)) # 22

# 十六进制转换十进制
print(int('0xAE86',16)) # 44678
print(int('AE86',16)) # 44678
# 十进制转换十六机制
print(hex(54321)) # 0xd431

# 十六进制转换为二进制
print(bin(0xFF91)) # 0b1111111110010001
# 二进制转为十六进制
print(hex(0b1111111110010001)) # 0xff91

# 十进制转为八进制
print(oct(1024)) # 0o2000
# 八进制转为十进制
print(int('0o2000',8)) # 1024

print(0b111010101 * 0xef10 * 0o112 * 123) # 261252885600
  • 二进制:数值前面加0b(0B)
  • 八进制:数值前面加0o(0O)
  • 十六进制:数值前面加0x(0X)
  • 二进制转换函数:
    • bin:二进制转换函数
    • int:十进制转换函数
    • hex:十六进制转换函数
    • oct:八进制转换函数

5. 改变字符串首字母的大小写

目标:

  1. 修改字符串中的字母
  2. 拆分字符串

1. 如何改变字符串首字母的大小写

s1 = 'hello'
print(s1)
print(s1.capitalize()) # Hello

# 分片
s1 = s1[0:1] +s1[1].upper() + s1[2:]
print(s1) # 'hEllo'

2. 如果字符串中包含多个单词,如何改变每个单词首字母的大小写

s3 = 'hello world'
arr = s3.split()
new = f'{arr[0].capitalize()} {arr[1].capitalize()}'
print(new)

6. 如何检测一个字符串是否为数字

目标

字符串中与检测相关的方法

1. 如何检测字符串是否为数字(数字和字母混合形式)

s1 = '12345'
print(s1.isdigit()) # True

s2 = '1234a'
print(s2.isdigit()) # False
# 字母数字混合形式检测
print(s2.isalnum()) # True
a3 = '123_4a'
print(s3.isalnum()) # False

2. 怎样将一个字符串转换为数字才安全

#判断类型
s2 = '1234a'
if s2.isdigit():
	print(int(s2))
else:
	print('s2不是数字')
# 异常捕获
try:
	print(int(s2))
except Exception as e:
	print(e)

7. 如何反转字符串

目标

分片的使用

1. 如何反转一个字符串

s1='abcde'
s2=''
for c in s1:
	s2=c+s2
print(s2) # edcba

2. 如何用分片反转字符串

s1='abcde'
print(s1[::-1])

8. 格式化整数和浮点数

目标

  1. 整数格式化
  2. 浮点数格式化

1. 请格式化一个整数,按10位输出,不足10位前面补0

n = 1234
print(format(n,'10d')) # '      1234'
print(format(n,'0>10d')) # 0000001234
print(format(n,'0<10d')) # 1234000000

2. 格式化一个浮点数,要保留2位小数

x1 = 1234.56789
x2 = 30.1
print(format(x1,'0.2f')) # 1234.57
print(format(x2,'0.2f')) # 30.10

3. 请描述format函数的主要用法

x2 = 30.1
print(format(x2,'*>15.4f')) # 右对齐, ********30.1000
print(format(x2,'*<15.4f')) # 左对齐, 30.1000********
print(format(x2,'*^15.4f')) # 中心对齐, ****30.1000****

print(format(1234567890,',')) # 千位号分割, 1,234,567,890
print(format(123456.86868686,',.2f')) # 123,456.87

print(format(123456.86868686,'e')) # 1.234569e+05
print(format(123456.86868686,'0.2E'))  # 1.23e+05

forma函数用于格式化数值,第二个参数指定格式化的格式

9. 你了解Python中的字符串吗

目标

  1. 转义符的使用
  2. 让转义符失效
  3. 保持字符串的原始格式

1. 如何同时显示单引号和双引号

print('"hello" \'world\'') # "hello" 'world'

2. 让转义符失效的方法

print(r"Let\'s go!")
print(rper("Let\'s go!"))
print("Let\\'s go!")
 
"""结果:
Let\'s go!
Let\'s go!
Let\'s go! 
"""

3. 如何保持字符串的原始格式

print("""
hello
     world
         Beijing
""")
 
"""结果:
hello
     world
         Beijing
                 """

10. 请详细描述print函数的用法

1. 使用print函数输出字符串时,用逗号分隔

print('aa','bb') # aa bb
print('aa','bb',sep=',') # aa,bb

2. 使用print函数输出字符串时,如何不换行

print('hello', end=' ')
print('world')

# hello world

3. 如何使用print函数格式化输出

s = 'roadster'
x = len(s)
print('The length of %s is %d' %(s,x)) # The length of roadster is 8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值