python 基础学习
(一)变量和简单数据结构
字符串方法:
title();//首字母大写显示每个单词
lower();//转换成小写
upper();//转换成大写
字符串合并
first_name="ada"
last_name="lovelace"
full_name=f"{first_name}{last_name}"
print(full_name)
或者 3.5一下版本
full_name="{}{}".format(first_name,last_name)
删除字符串中的空白
lanuage=" python "
lanuage=lanuage.rstrip()//删除末尾空格
lanuage=lanuage.lstrip()//删除左端空格
lanuage=lanuage.strip()//删除两端空格
数
浮点数
只要有操作数是浮点数,python默认得到的总是浮点数
>>> 3*0.1
0.30000000000000004
>>> 2*0.1
0.2
>>> 0.2*0.1
0.020000000000000004 //会出现小数位数不确定情况
//但有方法处理多余小数位
>>> universe_age=14_000_000_0000//大数可用_断开,只有3.6以上版本支持
>>> universe_age
140000000000
列表
由一系列特定顺序排列的元素组成
>>> bicycles=['trek','cannondale','redline','specialize']
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialize']
>>> print(bicycles[0])//从0开始索引
trek
>>> print(bicycles[0].title())
Trek
>>> print(bicycles[-1])//索引-1,返回表尾元素
specialize
>>> print(bicycles[-2])
redline
motorcycles=[]//创建空表
insert
>>> motorcycles.insert(0,'ducati')
>>> motorcycles
['ducati']
append(添加)
>>> bicycles.append('ducati')//在尾部添加元素
>>> print(bicycles)
['trek', 'cannondale', 'redline', 'specialize', 'ducati']
del
['trek', 'cannondale', 'redline', 'specialize', 'ducati']
>>> del bicycles[1]
>>> print(bicycles)
['trek', 'redline', 'specialize', 'ducati']
pop()
['trek', 'redline', 'specialize', 'ducati']
>>> bicy=bicycles.pop() //pop末尾元素
>>bicy
'ducati'
>>> bicycles
['trek', 'redline', 'specialize']
>>> bicy=bicycles.pop(1);bicycles;bicy //pop索引处的元素
['trek', 'specialize']
'redline'
remove()
['trek', 'specialize']
>>> bicycles.remove('specialize');bicycles//只删除第一个,删除其余要用循环
['trek']
sort()
>>> cars=['bmw','audi','toyota','subaru']
>>> cars.sort();cars //正序排列
['audi', 'bmw', 'subaru', 'toyota']
>>> cars.sort(reverse=True);cars //逆序排列
['toyota', 'subaru', 'bmw', 'audi']
sorted()//临时排序
>>> cars=['bmw','audi','toyota','subaru'];print(sorted(cars))
['audi', 'bmw', 'subaru', 'toyota']
>>> cars
['bmw', 'audi', 'toyota', 'subaru']
reverse()
['bmw', 'audi', 'toyota', 'subaru']
>>> cars.reverse();cars
['subaru', 'toyota', 'audi', 'bmw']
len()
>>> len(cars)
4
(二)循环
for item in list_items:
exp:
>>> list_items=['dog','cat','ck'];
>>> for item in list_items: //注意':'
print(item) //python里面注意缩进
dog
cat
ck
(三)创建数值列表
range()
>>> for value in range(1,5): //在5时停止
print(value)
1
2
3
4
>>> for value in range(5): //第二种创建
print(value)
0
1
2
3
4
>>> even_numbers=list(range(2,11,2));print(even_numbers)
//range(begin,end,step)三个参数
//list()函数将range()的结果直接转换为列表
[2, 4, 6, 8, 10]
min() max() sum()
>>> digits=[1,2,3,4,5,6,7,5,7,2]
>>> min(digits);max(digits);sum(digits)
1
7
42
列表解析
>>> squares=[value**2 for value in range(1,11)];squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
切片
squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
print(squares[0:3])
print(squares[:3])
print(squares[3:])//注意[begin:end:step]
print(squares[3::2])//end 是停止位置 不输出
result:
[1, 4, 9]
[1, 4, 9]
[16, 25, 36, 49, 64, 81, 100]
[16, 36, 64, 100]
复制列表
squares=[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
suqares_front3=squares[:3]
print(suqares_front3)
result:
[1, 4, 9]
//不可以 suqares_front3=squares 这样只相当于c++中的引用, suqares_front3也可以对squares操作
//要使用切片
(三)元组
不可改变的列表
dimensions=(200,50)
//支持[]取值 但不能修改
//可以对元组变量重新赋值
//元组实质是由逗号',' 标识,所以一个元组也要有 my_t(3,)
(四)if语句
连接关键字:and or in not in
列表是否空可返回 true false
if 判断 :
执行语句
elif 判断:
执行语句
else:
执行语句
age>11 and age<20
age>14 or age<6
>>> values=[1, 4, 9]
>>> 4 in values
True
>>> 5 not in values
True
if values: //这里 value 不为空 返回true ,为空时 返回false
执行语句
(五)字典
>>> alien={} //建立空字典
>>> alien['color']='green' //添加元素
>>> alien['point']=5
>>> print(alien) //输出字典
{'color': 'green', 'point': 5}
>>> print(alien['color']) //取字典中的元素
green
>>> del alien['color'];alien //删除字典元素
{'point': 5}
//建立多键值对字典,注意格式如下
language={
'i':'c',
'jo':'py',
'iv':'java',
}
get()//两个参数,不存在‘ch’,返回第二个参数
>>> language.get('ch','no ch')
'no ch'
循环遍历字典
>>> for key,value in language.items()://遍历键值对
print(f"{key} {value}")
i c
jo py
iv java
>>> for key in language.keys()://遍历key值
print(key.title())
I
Jo
Iv
keys() 可以将key值提取出 形成列表
values() 类似作用
支持 sorted(字典名)
转换成set
>>> language
{'i': 'c', 'jo': 'py', 'iv': 'java', 'ca': 'c++', 'I': 'c++'}
>>> for value in set(language.values()):
print(value)
c
c++
java
py
(六)嵌套
可以字典(列表),字典(字典),列表(字典),列表(列表)
py
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
(小声说Py里
#井号是(单行)注释
)是的没错 但在这里直接用的C++的注释 (笑) 习惯了
而且这#容易在这里变成标题吧
我只是碎碎念一句😝 有时
//
时复制题解代码Py的话会CE(Compile Error),#
或者""" """
就不会(老复制选手了) 不过y总的cpp写的是真熟练。Markdown语法用惯了#和\``` \```
点里面的内容区分还是easy(OK)哒