博主主修java,目前工作之余学习下python,此文章将从Java开发的角度记录学习python过程中的一些不同的、重要的语法点。
一、基础语法
1. **表示平方
a = 10
b = 2
print(a ** b)
consol: 10^2=100
2.str()把其他类型转String
3.列表 ≈ List
a.最后一个元素 :下标-1,以此类推-2,-3倒数
b.末尾添加元素:.append()
c.删除元素:del list[index]
list.pop(index) 删除元素并返回该元素,默认是末尾元素
d.按值删除 list.remove(value)
e.反向永久性排序:list.sort(reverse = True)
f.临时排序:sorted(list)
4.循环
a.for temp in lists:
print(temp)
注意!for循环冒号下面的缩进的行都属于循环块内
b.for i in range(1,5)
print(i) #=1,2,3,4 >=1 && <5
c.列表解析
result = [value**2 for value in list]
d.列表切片:list[index1 : index2] (index1:起始位置index值;index2:结束位置index值)
e.元组 react = (3,4)
元组内的值不可变,但是可以重新定义整个元组使其覆盖,其他便利操作同list
5.判断
a. python里面 if 语句不需要小括号
b. and 、or 连接多个条件
c. 判断值是否在列表中 in 、not in
list = list(range(1,10))
if 5 not in list:
print('test')
d.python三目运算:x if(...) else y
e. python中的else if 是elif
f. 判断列表是否为空 :if list:
6.字典 {} ≈ Map
a. 循环遍历字典的key map键值对:
for key,value in alien.items():
print(key + "," + str(value))
b.循环遍历字典的keys、values
for key in aliean.keys()/values():
7.用户输入
a.字符串输入:input('提示语,但是只返回用户的输入内容不包括提示语')
b.数值输入:int()