6小时完全入门
# ********非运行
#主语言java 业务需要使用python 理解一下python的用法
0.输入输出
print(i * '*') 将'*'输出i次
1.格式转换
first = 'John'
last = 'Smith'
message = first + ' [' + last +'] ' + 'is a good coder' 太麻烦,需要拼接
msg = f'{first} [{last}] is a good coder'
print(msg)
2.数组:list tuple +词典dict
2.1复制数组
a = 'python for beginners'
b=a[0:-1] python for beginner #从位置0开始到不包括最后一个位置
2.2二位数组 列表
(1)定义
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][1])
(2)输出
for row in matrix:
print(row) #输出行
for item in row:
print(item)#输出单个
(3)数组相关的函数
numbers = [5, 5, 5, 6, 3, 2, 1]
numbers.append(13) #1)append 添加末尾
numbers.insert(0, 10) #2)insert 添加指定index 添加10到索引为0的位置
numbers.remove(5) #3)remove 删除某value !!!经验证,只能删除第一个5
numbers.clear() #4)clear 清空数组
numbers.pop() #5) pop 删除末尾value 跟java栈一样用法
# 判断value是否存在 1-index 2-用in判断 3-count函数判断
print(numbers.index(5)) #6) index 返回value首次出现的位置 找不到会抛出错误
print(50 in numbers) #返回False 所以用in判断value是否存在会更安全
print(numbers.count(5)) #7) count 返回value的个数 不存在返回0
#8) 排序sort
numbers.sort()
print(numbers)
#9) reverse 反转 比java方便太多,但是一般笔试题要求时间空间复杂度,reverse应该不能直接用
numbers.reverse()
#10) copy 复制,此时对原数组numbers进行修改都不会影响numbers2的内容
numbers2 = numbers.copy()
2.3 tuple
#特点 元组不可改变 只有count和index两个函数
(1)定义
numbers = (1, 2, 3)
print(numbers[0])
2.4 #unpacking 解包 tuple list都适用
# 解包在英文里叫做 Unpacking,就是将容器里面的元素逐个取出来Python 中的解包是自动完成的
numbers = (1, 2, 3)
x = numbers[0]
y = numbers[1]
z = numbers[2]
x, y, z = numbers #以上两种表达式一样的效果
2.5 dict
customer = {
"name": "Brenda Zhang",
"age": 3,
"sex":"female"
}
print(customer["name"])
print(customer["birthdate"]) #直接抛出错误
#使用get方法获取key相对应的value值 !!!可以加默认值参数
print(customer.get("birthdate")) #返回None
print(customer.get("birthdate", "Oct 5 1900"))#返回后面给出的默认值
print(customer)#不会影响原dict
#电话号码小例子
phpne_nums = {
"1": "One",
"2": "Two",
"3": "Three",
"4": "Four"
}
nums = input("Phone :")
output = ""
for value in nums:
output += phpne_nums.get(value, "!") + " " #没有对应的给一个默认值
print(output)
#分解输入内容对应表情小例子
message = input(">")
words = message.split(' ')
emojis = {
":)": "😊", # win+; 可以调出表情
":(": "😊"
}
output = ""
for word in words:
output += emojis.get(word, word) + " " #!!!这个默认值可以保证 如果不在词典中,就原样输出
print(output)
3.字符串
3.1 查找替换
course = 'python for beginners'
print(course.find('beginners')) 找不到是-1
print(course.replace('beginners','zhangyang'))
print('python' in course) 判断是否存在某个子串 bool值 True False
3.2 分割 split 跟java相同用法
words = message.split(' ') #根据空格分隔
4.四则运算
print(10/3) 小数
print(10//3) 整数
print(10 ** 3) 1000=10^3
5.数学函数 https://www.runoob.com/python/python-numbers.html
x = -2.6
print(round(x)) -3 四舍五入
print(abs(x)) 2.6 绝对值
print(math.ceil(x)) -2 向上取整
print(math.floor(x)) -3 向下取整
6.条件语句的使用
(1)if else
like=True
dislike=False
if like:
print('Yes, he absolutely likes you!')
print('Congratulations!')
elif dislike:
print('Keep learning, keep fitting!')
else:
print('Come on, please try it again.')
(2)and or not 替代java中的&& || !
(3)while 循环
secret_num = 9
i = 0
while i < 3:
guess = input('guess bumber:')
i += 1
if secret_num == int(guess):
print('you wins!')
break
else: #while里面也可以对应else语句 amazing
print('you lost')
(4)for 循环
for item in "brenda":
print(item) #输出 b r e n d a 输出换行间隔
for item in ['brenda', 'naomi', 'lisa']:
print(item) #输出list内容
for item in [1, 2, 3, 4]:
print(item)
for item in range(10):
print(item) #range函数 输出0-9不包括10
for item in range(5, 10):
print(item) #输出5-9
for item in range(5, 10, 2):
print(item) #输出5 7 9 step=2
6.2 列表 字典
(1)
names = ['brenda', 'naomi', 'lisa']
print(names) #原样输出 包括中括号
print(names[1:]) #输出第二个名字到最后
7.一些函数
(1)大小写
name = 'bRenda'
name.upper() 大写
name.lower() 小写
name.capitalize() 首字母大写