林猫队伍+林猫+打卡python基础

目录

一、基础软件下载

二、基础学习(只记录了一些自己需要注意的)

1.缩进

2.注释  (写代码的好习惯)

3.变量

练习题

5 数据类型

练习

6 数字转化

7 字符串

1)声明方法

2)使用方法

练习题

8 布尔值

练习题

9 运算符

10 列表 list

创建方式

clear 清除

列表推导式

练习

11 元组

练习

12 集合

添加集合

 移除集合

集合连接

练习

13 字典

 字典的访问

返回键值

x = thisdict.keys()

获取值

返回元组

 更改字典的办法

遍历字典

嵌套字典

练习

14 if else

15  while

16 for 循环

17 函数

定义

参数数量

默认参数值

18 lambda 匿名函数


一、基础软件下载

pycharm下载+python 下载

(这个部分最大的问题就是python环境的运用了,在看教程之前因为自己先搜了搜。。。emmm。就自己下载了Anaconda,vscode,然后教程一出来,用的是pycharm,人傻了。结果就是导致python编译器貌似有好几个。pycham跟着教程设定的是用的python3.10(唉 也是个坑 安装到c盘了)然后用pip的时候发现用的3.8的python,目测是安anaconda时候安装的。不过运气好还能运行,但感觉,这几个坑可能已经埋好了。不知道啥时候会踩上0.0)    

二、基础学习(只记录了一些自己需要注意的)

1.缩进

与c、m语言不同,用缩进来表示循环的嵌套层数..... 用end啥的结束不好么0.0 ,才开始看的时候真的头大。

2.注释  (写代码的好习惯)

与自己常用的m语言不同 单行注释为 # 快捷键为ctrl+  多行注释为 ''' 三引号

3.变量

和c 、m语言差不多,字符串的拼接可以用+来实现。

例如

x = "川川真"
y = "帅"
z = x + y
print(z)

   全局变量的使用

x = "帅哥"

def myfunc():
  global x
  x = "菜鸟"

myfunc()

print("川川" + x)

在使用变量前,先提前用global声明一下下

练习题

#1
carname = '菜鸟'
#2
x = 60
#3
x = 5
y = 10
print(x+y)
#4
x = 8
y = 9
z =x+y
print(z)

5 数据类型

str 字符串  int 整形 float 浮点  complex 复数 bool 布尔(这几个是老盆友了)

list 列表 tuple 元组  range 范围型  dict 字典  set 集合  bytearry字节组(后面有具体讲解  这就只列出来了)

练习

1. int

2.字符串

3.浮点

4.x = ["apple", "banana", "cherry"]——————list(要点:注意是中括号

5.x = ("apple", "banana", "cherry")——————tuple(要点:注意是小括号

6 数字转化

int  float  complex  random

练习

1.转浮点  float

2.转int    int

3.转复数   complex

7 字符串

1)声明方法

常规使用方法     "a" 与 ’a‘一样

可以用3个引号将多行字符串进行分配

a = """从前有座山,
山里有座庙
庙里有个小和尚"""
print(a)

2)使用方法

首先需要注意的是:在python中,字符串其实是一个由字符组成的数组 。可以用索引对其中的字符进行调用。

如:a = "Hello, World!"
       print(a[1])

检查字符串

txt = "The best things in life are free!"
print("free" in txt)           #这里用到了 in   还有什么  for  x  in
                                      #相似的也有 not in  这个是一个判断返回的是真和假  为布尔型

切片字符串

....emmm这个就很像用索引去调用表示么  但神器的地方在于

b = "Hello, World!"
print(b[2:5])          #输出居然为llo  对应的索引为2,3,4....这个该如何去理解..感觉用这个脑子会糊                                 #应该就是2到5不包含5  数学区间这样写:[2,5)

:  在python的所用使用中相当于省略号吧  省略中间的索引编号。 例如 [ 2 : ] (2到底  滑稽.jpg) 

      [:,10]   从开始到10

修改字符串

小写转大写 uppr

 大写转小写 lower

strip  删除空格

replace  替换字符

format 进行传参

quantity = 20
itemno = 3000
price = 49.95
myorder = "川川今年 {}岁 买了个华为手机 {} 每个月花费 {} 元."
print(myorder.format(quantity, itemno, price))

练习题

#1
x = "Hello World"
print(len(x))
#2
txt = "Hello World"
x =txt[0]
print(x)
#3
txt = "Hello World"
x =txt[2:5]
print(x)
#4
txt = " Hello World "
print(txt.strip())
#5
txt = "Hello World"
x =txt.upper()
print(x)
#6
txt = "Hello World"
x =txt.lower()
print(x)
#7
txt = "Hello World"
x = txt.replace('H','J')
print(x)
#8
age = 36
txt = "My name is John, and I am {}  "
print(txt.format(age))

8 布尔值

任意非空集和非0元素转布尔型都为真。

练习题

1)true 2)false   3)false        4)true        5)false

9 运算符

加减乘除:+ - * /  

 /正常除法为浮点型   //取整数     %取余

a =17/3
b = 17//3
c = 17%3
print(a,b,c)
5.666666666666667 5 2

** 幂运算
a=4**2
b=2**3

10 列表 list

创建方式

1)thislist = list(("apple", "banana", "cherry")) 

2)mylist = ["川川一号", "川川二号", "川川三号"]    (重点  中括号)

重点

1. List列表是一个有序且可变的集合。允许重复成员。

2, turple元组是一个有序且不可更改的集合。允许重复成员。

3. Set集合是一个无序且无索引的集合。没有重复的成员。

4. dict字典是一个有序且可变的集合。没有重复的成员。

! ! !从Python 3.7版开始,字典才是有序的! ! ! 

用索引调用时记得  : 的特点  [2:5] = [2:5)  右侧为一个开区间

insert插入

mylist = ["川川一号", "川川二号", "川川三号","川川四号"]
mylist.insert(2,'帅哥呀')
print(mylist)
 

extend 合并

mylist = ["川川一号", "川川二号", "川川三号","川川四号"]
mylist2=("川川","菜鸟")
mylist.extend(mylist2)
print(mylist)

remove 与 pop

remove为按内容删除指定元素   pop为按索引删除

mylist = ["川川一号", "川川二号", "川川三号","川川四号"]
mylist.remove('川川二号')
等于
mylist.pop(2)

clear 清除
 

列表推导式

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x for x in fruits if "a" in x]

练习

#1
fruits = ["apple", "banana", "cherry"]
print(fruits[1] )
#2
fruits = ["apple", "banana", "cherry"]
fruits[0]  = 'kiwi'
print(fruits)
#3
fruits = ["apple", "banana", "cherry"]
fruits.append('orange')
print(fruits)
#4
fruits = ["apple", "banana", "cherry"]
fruits.insert(1,"lemon")
print(fruits)
#5
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits)
#6
fruits = ["apple", "banana", "cherry"]
print(fruits[-1])
#7
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(fruits[2:5])
#8
fruits = ["apple", "banana", "cherry"]
print(len(fruits))

11 元组

 matlab里的元胞数组?  元组一旦创建便无法更改。  但可以通过

1.将其转化为list进行改变。

2.将另一个元组向元组添加

元组的声明 需要注意:1.小括号  2.元组中只有单一项目时要加上逗号如:(’啦啦啦‘,)

元组解包

就是提取元组中的项目

当变量数少于值的数目时,可以使用  *+变量名   将剩下的值作为列表分配给变量

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)

元组可以通过+号合并
 

 count  函数可以用来记录某个元素的出现次数

index 可以找出元素的索引

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
x = thistuple.index(8)

练习

#1
fruits = ("apple", "banana", "cherry")
print (fruits[0])
#2
fruits = ("apple", "banana", "cherry")
print (len(fruits))
#3
fruits = ("apple", "banana", "cherry")
print (fruits[-1])
#4
fruits = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(fruits[2:5])

12 集合

基本使用方法相同  集合中的值不能重复

添加集合

update   一项的话可以用add

myset = {"川川一号", "川川二号", "川川三号"}
myset1 = {"川川一号", "川川二号", "川川三号",'川川菜鸟'}
myset.update(myset1)
print(myset)

 移除集合

remove、discade 根据移除某一项 

pop根据索引移除某一项  默认为最后一项

集合连接

update 与 union都可以做到  但不同点在于 

a.update(b)是将b插入到a中a 的值发生了改变,    而   c = a.union(b)是用一个新的变量来获得这个连接上的集合


a.intersection_update(b)  仅保留交集 改变了a   因为用上了update(更新)

c = a.intersection(b)    这个就能赋值了


a.symmetric_difference_update(b)  求不同的元素

a.symmetric_difference(b)

练习

#1
fruits = {"apple", "banana", "cherry"}
if "apple"  in fruits:
  print("Yes, apple is a fruit!")
#2
fruits = {"apple", "banana", "cherry"}
fruits.add('orange')
print(fruits)
#3
fruits = {"apple", "banana", "cherry"}
more_fruits = ["orange", "mango", "grapes"]
fruits.update(more_fruits)
print(fruits)
#4
fruits = {"apple", "banana", "cherry"}
fruits.remove('banana')
print(fruits)
#5
fruits = {"apple", "banana", "cherry"}
fruits.discard('banana')
print(fruits)

13 字典

 和  构成,用大括号写   有序 可变,但不可重复

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

 字典的访问

thisdict = {
  "name": "川川",
  "address": "上海",
  "year": 2000
}
x = thisdict["name"]
y=thisdict.get('name')
print(x)
print(y)

直接通过键调用  或者 用get

返回键值

x = thisdict.keys()
 

获取值

x = thisdict.values()

返回元组

在这里插入图片描述

 更改字典的办法

直接调用进行 值更改    或       update

删除

使用pop删除指定的项目(值 和 键)

popitem()删除最后插入的项目

del 进行关键字的删除

遍历字典

thisdict = {
  "name": "川川",
  "address": "上海",
  "year": 2000
}
for x in thisdict:    #键名
  print(x)

for x in thisdict:
  print(thisdict[x])  #值

嵌套字典

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}
print(myfamily)

或者 先单独建立 再合并

child1 = {
  "name" : "Emil",
  "year" : 2004
}
child2 = {
  "name" : "Tobias",
  "year" : 2007
}
child3 = {
  "name" : "Linus",
  "year" : 2011
}

myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}
print(myfamily)

练习

#1
from pyexpat import model
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print (car.get('model'))

#2
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car['year'] = 2020
print (car.get('year'))

#3
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car['color'] = 'red'
print (car)

#4
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.pop('model')
print (car)

#5
car =	{
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
car.clear()
print (car)

14 if else

常规用法

15  while

break 打破while大循环  continue打断本次循环

16 for 循环

break 打破while大循环  continue打断本次循环  pass 用于没有内容的for循环

17 函数

定义

def my_function():
  print("Hello from a function")

注意缩进

参数数量

未知的参数数量可以用 *args 代替   args会成为一个收到的参数元组

def my_function(*kids):
  print("川川帅哥 " + kids[2])
my_function("名字", "性别", "菜鸟")

默认参数值

def my_function(country = "Norway"):
  print("I am from " + country)
my_function()

此时输出的为默认值Norway

18 lambda 匿名函数

x = lambda a : a + 10   ##不用去define  但是只能有一个表达式。 可以用于一些简单的公式中
print(x(5))

x = lambda a, b, c : a + b + c   #x 公式名   abc传入变量  :后面表达式
print(x(5, 6, 2))


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林猫123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值