最好有点c++基础来看,,每天都更新一篇吧

这一篇是一些基础东西

1.运算符
2.变量
3.基本输入输出
4.字符串
5.列表
6.元组
7.字典
8.集合
9.简单的说下循环啥的

1.运算符

特别的

a / b:为浮点数,a // b 为整数,丢弃小数部分 a**b 为a的b次方

还有 c // = a,c ** = a;

python 中没有 &&, ||, !

分别为 如 if(a and b)   ,if(a or b)   if( not a),

and  ,or , not来代替

其中还有  in    和  not  in

可以在字符串,列表或元组序列中使用  直接查找是否存在

还有is   和  is not  顾名思义

2.变量

不必声明变量类型   直接  比如  x = 3即可

改变的时候也可以直接转换

x = 'a'
y = 'b'
print(x)
print(y)
x = 1
y = 2
print(x)
print(y)

3.基本输入输出

输入用input

x = input("x:")
y = input("y:")
#print(x * y) 则错误 ,因为输入的内容是以字符串的形式返回的,应该转换成int型
print(int(x) * int(y))

输入不区别单引号和双引号

print('hello\nrunoob') # 使用反斜杠(\)+n转义特殊字符
print(r'hello\nrunoob') # 在字符串前面添加一个 r,表示原始字符串,不会发生转义"""

或者 \n  写成  \\n  其中  \\ 表示\

# 换行输出
print( x )
print( y ) print('---------')
# 不换行输出
print( x, end=" " )#表示末尾为空格
print( y, end=" " )
print()'''

print用,连接字符串和变量

x = "aaaa"
print("x:" ,x)

4.字符串

str = "wangpeibing"
print(str)
print(str[0:-1])
print(str[0])
print(str[2:5])#截取
print(str[2:])#第二个字符到最后
print(str*2)#输出两次
print(str+"你好")#连接

可以有查找

if("wang" in str):
print(1)

字符串的格式化:就是类似于c语言的表达形式

print("My name is %s and weight is %d kg!" % ('peibing', 21) )

5.列表

类似c语言数组,有下标。特征是中括号,也有切片,,类似字符串

a = [1,2,3]
b = [4 ,5 ,6]
print(a + b)#连接
print(len(a))#长度
print(a*4)
del a[0]#删除第一个元素
print(a[-1])#倒数第一个
print(3 in a)
for i in a :#循环迭代输出
print(i)

还可以嵌套

c = [a,b]  #嵌套  类似于二维数组
print(c)
print(c[1][0])

一些常用方法

max(a)#列表最大值
min(a)#列表最小值
list.append(1)#a的末尾添加1
list.count(1)#a中统计出现1次数
list.index(1)#从列表中找出某个值第一个匹配项的索引位置
list.insert(下标,obj)#将对象插入列表,obj自己定义
list.pop([index=-1]])#移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
list.remove(obj)#移除列表中某个值的第一个匹配项
list.reverse()#反向列表中元素
list.clear()#清空列表
list.copy()#复制列表

sort,sort 方法不适合 int 和 str 类型的比较。

#list.sort(cmp=None, key=None, reverse=False)#对原列表进行排序
#reverse = True 降序, reverse = False 升序(默认)
# 获取列表的第二个元素
def takeSecond(elem):#函数
return elem[1]
# 列表
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 指定第二个元素排序
random.sort(key=takeSecond)
# 输出类别
print('排序列表:', random)
list=[ ["","c++","demo"],
["","c","test"],
["","java",""],
["","golang","google"],
["","python","gil"],
["","swift","apple"]
]
list.sort(key=lambda ele:ele[0])# 根据第1个元素排序
print(list)
list.sort(key=lambda ele:ele[1]) #先根据第2个元素排序
print(list)
list.sort(key=lambda ele:ele[1]+ele[0]) #先根据第2个元素排序,再根据第1个元素排序
print(list)

6.元组

元组和列表差不多 特点是不能修改,小括号啊

只有一个元素要加,

>>>tup1 = (50)
>>> type(tup1) # 不加逗号,类型为整型
<class 'int'> >>> tup1 = (50,)
>>> type(tup1) # 加上逗号,类型为元组
<class 'tuple'>

删除的话要么全部删除,要不就不能删

其他的方法都和列表 差不多

list(元组)  将元组转化成列表    tuple(列表)  将列表转化成元组

7.字典

理解为c++中的map。特点是大括号,每一个键都对应这键值

dict = {'Name': 'Runoob', 'Age': 7, 'Class': 'First'}

print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])
dict['Name'] = 'peibing' #修改相应的键值
del dict['Name'] # 删除键 'Name'
dict.clear() # 清空字典
del dict # 删除字典

注意  键必须不可变,所以可以用数字,字符串或元组充当,而用列表就不行,

一些方法

求长度 len(dict)   为3

显示 str(dict)

判断类型type(dict)

8.集合

和c++的set差不多,主要是去重

可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。

a = {'abracadabra'}#这时候set里面有一个字符串的值
print (type(a))
print(a)
a = set('abracadabra')#这时候给他规定为这个set里依次存放了这个字符串的字符
print (type(a))
print(a)
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b # 集合a中包含元素
{'r', 'd', 'b'}
>>> a | b # 集合a或b中包含的所有元素
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b # 集合a和b中都包含了的元素
{'a', 'c'}
>>> a ^ b # 不同时包含于a和b的元素
{'r', 'd', 'b', 'm', 'z', 'l'}
>>>thisset = set(("Google", "Runoob", "Taobao", "Facebook"))
>>> thisset.pop()
'Facebook'
>>> print(thisset)
{'Google', 'Taobao', 'Runoob'}

9.简单的说下循环啥的

有while 和for两个,基本用法都和c差不多,包括break和continue,先看while

i = 1
while i < 10:
i += 1
if i%2 > 0:
continue
print i i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break

有c++基础的应该不难看懂,注意这个没有大括号,是通过缩进和一些  :  来判别的

接下来是for循环

可以是

fruits = ['banana', 'apple',  'mango']
for fruit in fruits: # 第二个实例
print (当前水果 :', fruit)

也可以是

fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):
print ('当前水果 :', fruits[index])

range返回一个序列的数。

for num in range(10,20):  # 迭代 10 到 20 之间的数字
for i in range(2,num): # 根据因子迭代
if num%i == 0: # 确定第一个因子
j=num/i # 计算第二个因子
print ('%d 等于 %d * %d' % (num,i,j))
break # 跳出当前循环
else: # 循环的 else 部分
print (num, '是一个质数')

else if 在python中写成elif

python基础学习笔记(一)的更多相关文章

  1. 0003.5-20180422-自动化第四章-python基础学习笔记--脚本

    0003.5-20180422-自动化第四章-python基础学习笔记--脚本 1-shopping """ v = [ {"name": " ...

  2. python 基础学习笔记(1)

    声明:  本人是在校学生,自学python,也是刚刚开始学习,写博客纯属为了让自己整理知识点和关键内容,当然也希望可以通过我都博客来提醒一些零基础学习python的人们.若有什么不对,请大家及时指出, ...

  3. Python 基础学习笔记(超详细版)

    1.变量 python中变量很简单,不需要指定数据类型,直接使用等号定义就好.python变量里面存的是内存地址,也就是这个值存在内存里面的哪个地方,如果再把这个变量赋值给另一个变量,新的变量通过之前 ...

  4. Python基础学习笔记(十三)异常

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-exceptions.html Python用异常对象(excep ...

  5. Python基础学习笔记(十二)文件I/O

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-files-io.html ▶ 键盘输入 注意raw_input函 ...

  6. Python基础学习笔记(十一)函数、模块与包

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-functions.html 3. http://www.liao ...

  7. Python基础学习笔记(十)日期Calendar和时间Timer

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-date-time.html 3. http://www.liao ...

  8. Python基础学习笔记(九)常用数据类型转换函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...

  9. Python基础学习笔记(八)常用字典内置函数和方法

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-dictionary.html 3. http://www.lia ...

  10. Python基础学习笔记(七)常用元组内置函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-tuples.html 3. http://www.liaoxue ...

随机推荐

  1. 问题:win10防火墙不能自动启动

    问题:win10防火墙不能自动启动 描述:Windows防火墙不能自动启动,每次开机要手动启动,打开service.msc,里面防火墙的启动类型为手动,按钮为灰色,不能更改为自动,怎么办? 解决方法: ...

  2. Opencv——摄像头设置

    VideoCapture capture(0);/*设置摄像头参数 不要随意修改capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度 capture.set( ...

  3. 跳转到appstore下载app的链接 记录一下

    这是链接: https://itunes.apple.com/cn/app/da-dou-dou-lao-shi/id1395835036?mt=8 其中值得一提的是mt参数是啥意思 见下图:

  4. 以代码爱好者角度来看AMD与CMD(转)

    随着浏览器功能越来越完善,前端已经不仅仅是切图做网站,前端在某些方面已经媲美桌面应用.越来越庞大的前端项目,越来越复杂的代码,前端开发者们对于模块化的需求空前强烈.后来node出现了,跟随node出现 ...

  5. windows服务初识

    参考网址1:http://www.vchome.net/dotnet/dotnetdocs/dotnet38.htm 参考网址2:http://zhidao.baidu.com/link?url=7- ...

  6. .Net操作Excel公式实现

    //传入Excel公式,获取公式计算结果private string GetValue(string formula) { string result = ""; try { Ob ...

  7. jQuery选择器(上)

    一.基本选择器  1.ID选择器 $("#id")  2.类选择器 $(".class")  3.元素选择器 $("element")  4 ...

  8. Windows安装TensorFlow遇到错误

    1.先检查系统是64还是32位的,检查python版本是否相符合 2.windows系统上使用tensorflow需要python3.5版本

  9. spring cloud gateway之filter篇

    转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理,在路由 ...

  10. 我的react学习

    基础部分 创建一个react的项目 创建一个react的项目 全局安装 react 指令 // 全局安装react (根据需要安装,不是必须的) npm i -g react // 或者 yarn - ...