python入门学习笔记

#####什么是变量
#变就是变化
#这个就是注释吗
#存储计算机程序中需要引用和操作的信息
#有自己的回收机制
#用大写字母表示的是常量    小写字母表示变量
#原始字符串   C = r'C:\now'  C = 'C:\now'
#长字符串 print("""长字符串放三个双引号中间""")
#####字符串的内置方法:
#str = 'abcdefg'
#str.casefold()    将字符串所有字符变成小写
#str.count(字串,开始位置,结束位置)   查找子字符串出现的次数,可选参数
#str.find(字串,开始位置,结束位置)   查找子字符串在该串中的位置,返回的是字串第一个位置的下标
#str.index(字串,开始位置,结束位置)同find 不同于find,找不到字串会报错
# + 也可以 连接字符串 join([字串,字串 ,字串,字串,字串,字串,])  str.join(字串) ' '.join(['i','love','you'])
#replace(old,new)  替换指定字符串 str.replace('you','fish')
#####字符串的格式化 按照统一的规格去输出字符串
#位置参数
#format(); "{0} love {1} {2}".format("i","you","yes")
#关键字参数
#print("{a} love {b} {c}".format(a="i",b="you",c="yes"))
#str1 = "{a} love {b} {c}"
#print(str1.format(a="i",b="you",c="yes"))
#位置参数与关键字参数综合使用,位置参数必须在前
#####格式化操作符
#%左右都是数字的时候为求余数,出现在字符中就是表示格式化操作符
#%d 格式化整数
#%f 格式化浮点数,可以指定小数点后面的精度
#%c
#print('%d'%91)
#####数据类型
#int 一个*是乘号,两个**是平方2**32  2的32次方
#float
#python 区分整形以及浮点型的唯一方式:就算看有没有小数点
#####python分支语句
#score = int(95)
#if score <= 100 :
#    print("优秀")
#if score <= 100 :
#    print()
#else:
#   if    :
#   else  :
#用if else 注意格式

#if score <= 100 :
#   print()
#elif:
#   print()

#assert 断言 后面的的条件为假时,会报错
#
#while循环

#import os
#import random
#temp = random.randint(0,100)
#n = 1
#try:
#    guess = int(input("请输入你猜的数字"))
#except NameError and ValueError:
#    print("输入的内容必须为整数")
#    guess = int(input("请输入你猜的数字"))
#    n=n+1
#while guess < temp or guess > temp:
#    n=n+1
#    if guess > temp:
#        print("数大了")
#    else:
#       print("数小了")
#    guess = int(input("请输入你猜的数字"))
#if guess == temp:
#    print("对了")
#print("预测{}次,你对了",format(n))
#os.system("pause")

##### for 和 range
#####
#f = "i"
#for each in f:
#    print(each,end="")
##### range()函数,内建函数
##### range([start].stop,[step = 1](表示默认值为一)

#for i in range(5):#包含0,不包含5
#    print(i)


#for j in range(5,10):#包含0,不包含5
#    print(j)


####break continue
#a = "好"
#b = input("我好不好:")
#while True:
#    if b == a:
#        break;
#    b = input("我好不好")
#print("说大实话")

#continue 不用想了吧

#列表 = []
#列表.append() 在列表末尾添加元素,不能同时添加两个参数
#列表.extend() 能同时添加两个以上 参数 使用一个列表去扩展另外一个列表

#想插队:insert(在哪个位置去插入,要插入的元素)队头从0开始 
# 

#从列表中获取元素:通过元素索引值从列表中获得单个元素,索引值也从0开始
#列表[索引值 ]
#从列表中删除元素:列表名.remove(元素值);
#del:是一个语句,不是列表方法,索引不必在后面加小括号 del 列表名(删除整个列表)
#del student[1元素的索引值,就是下标值],删掉第一个
#弹出元素 列表名.pop(弹出的元素的索引值,就是下标值),弹出的不会再出现在列表里 
#列表名.pop,默认弹出列表最后一个元素
#
#列表常用操作符
#列表1 = [1,2,3,4]
#列表2 = [5,6,7,8]
#列表1 > 列表2
#输出true  多个元素默认比较第一个
#
#lie1 = [1,2,3,4]
#lie2 = [5,6,7,8]
#lie3 = lie1 + lie2=[1,2,3,4,5,6,7,8]
#
#lie1 = [1,2,3,4]
#lie1*3 复制三次 
#
#2 in lie3
#true 判断2在不在列表lie1里
#
#

#列表分片  一次性获取多个元素
#student = ["cnm","dsb","gnm"]
#student[0:2] 输出列表下标0-2的元素,2不包含,实际上0,1
#得到的是原来列表的拷贝,原理列表并没有改变
#
#student[0:2:4] 4代表一个步长,从0开始
#
#

#dir(list)查询列表的的所有使用方法
#
#
#元组:只读的列表,限制了权限
#a = (1,2,3,4,5)
#访问和列表没什么区别
#a[1]
#a[:]
#a[0:2]
#
#元组标志性符号是逗号,不是()
#a = 1,2,3,4 == a = (1,2,3,4)
#a=() 空元组
#
#
#更新和删除元组
# 
#c = tuple((1,22,3)) (1,22,3)是其中一个元组
#
#求长度len() len(list)  
#求最大/小值 max(list) min(list) 字符串,数字数组都可以
#
#
#


#函数:如何创建一个函数:def +函数名()

#创建:def myfunction():
#       print("wsdsb")


#调用: myfunction()

#参数:函数小括号里面的东西

#
#def hshu(name):
#    print(name+"大帅比")

#hshu("我是")

#返回参数
#def my(a,b):
#    return a+b
#print(my(5,60))

# 
#
#默认参数:在定义时赋予了默认值的参数,相当于缺省函数
#
#
#def myf(name = "dsb ",word = "is dsb"):
#    print(name + word)

#myf()    
#myf(name = "zy",word = "csb")#也可以重新赋值
#
#函数变量的作用域
#
#内嵌函数
#golbal
#
#cout = 5
#def myfun():
#    global count
#    count = 10
#    print(count)
#myfun()
#print(count)
#
#python 函数定义是可以嵌套的,也就是允许在函数内部创建另一个函数
#def fun1():
#    print("dsb")
#    def fun2():
#        print("顿开教育")
#    fun2()
#fun1()
#
#闭包:函数式编程的一个重要的语法结构
# 
#def funx(x):
#   x = 5
#   def funy(y):
#       nonloacl x
#       x *= 5        可以修改x
#       return x*y
#   return fun(y)
#
#print(fun(8)(5))
#
#nonlocal 
#
#匿名函数
#lambda表达式(又叫匿名函数)参数:返回值
#
#def dis(x):
#    return x * 2 + 1
#print(dis(5))
#a = lambda x: x * 2 + 1#lambda返回的是函数对象,对它进行一个简单的赋值操作
#print(a(5))
#
#filter():理解为过滤器,有两个参数,第一个参数可以为函数,也可以为null,
#第二个参数里的每一个参数作为函数的参数进行计算,把返回值true的值的值筛选出来
#def odd(x):
#    return x%2
#a = filter(odd,range(10))
#print(list(a))
#
#map():映射意思 也有两个参数 参数(参数,可迭代序列)把序列的每一个数据传给函数进行运算
#def sum(x):
#    return x*2
#c = map(sum,range(10))
#print(list(c))
#
#递归
#
#def fun(n):
#   if n==1 :
#       return 1
#   else:
#       return n*fun(n-1)
#
#a = int(input("请输入一个数:"))
#b = fun(a)
#print(b)
#
#字典和集合
#字典:在python 中把字称为键,把其对应的含义称为值
#在python中字典是唯一的映射类型
#字典用大括号,冒号左边为键,冒号右边为值,每一个键和值组成一个项
#dict1 = {"adam":"老师","顿开":"教育"}
#print(dict1)
#
#dict["x"] = 88
#print(dict)
#
#dict["x"] = 120
#print(dict)#重新赋值
#
#dict2 = dict(f=70)
#print(dict2)
#
#fromkeys():用于创建并返回一个新的字典 有两个参数(字典的键,键对应的值),不写则默认为null
#dict7 = {}
#dict7.fromkeys((1,2,3),("cnm"))
#
#访问字典
#keys() 用于返回字典中的键
#dict7 = {}
#dict8 = dict7.fromkeys((1,2,3),("cnm"))
#dict8.keys()
#
#values() 返回字典中的所有值
#print(dict8.values())
#
#items():作用返回字典中的项
#print(dict8.items())
#print(dict8[5])
#
#清除与复制字典
#get():访问方法 如果访问的数不在字典里,则会返回一个none
#print(dict8.get(20),"没有找到") 返回值就是没有找到,而不是none
#
#clear() 清除字典
#dict8.clear()与dict8 = {}的区别
#dict9 = dict8
#使用dict8.clear() dict9也会被清空
#使用dict8 = {} dict9不会被清空,还是dict8的值
#
#copy() 复制这个字典
#c = dict8.copy()
#print(c)
#
#弹出与更新字典
#pop()  给出键就会弹出对应的值
#dict8.pop(2)
#
#popitem():直接弹出项
#dict8.popitem(3)
#
#setdefault()在字典中找不到相应的键就会自动添加
#dict8.setdefault(3)
#
#update() 更新字典
#dict8 = {"3":"two"}
#dict8.update(3 ="three")
#print(dict8)
#
#
#集合:
#a = {"a":"b"}
#print(type(a))
#b = {1,2,3,4,5}#单纯的把数字括起来  重复数字会删除,保证唯一,无序,不能通过索引值获取元素
#print(b)
#
#d = [1,2,3,4,5,6]
#c = set(d) 把列表变成集合
#print(c)
#
#add(要增加的元素) 集合元素添加
#remove(要删除的元素) 集合元素删除
#
#forzenset()
#a = forzenset({1,2,3,4,5,6,7})被冻住,修改不了的
#print(a)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值