Python编程基础(1)

本编讲解的知识点依据如下导图,当然不能涵盖全部的知识点。

1. Python语言的基本语法

使用缩进表示代码块,同一代码块的语句必须包含相同的缩进空格数。不像C,C#使用{ }

编写代码时尽量不要使用过长的语句,应保证一行代码不超过屏幕宽度。如果实在太长,允许在行尾使用"\" 续行符,表示下一行代码仍属于本条语句。

注释:#  单行注释; 3个单引号或3个双引号 多行注释

基础数据类型

6中标注数据类型:Number 数字,String 字符串, List 列表, Tuple 元组, Set 集合, 字典 Dictionary

不可变数据类型:Number,String,Tuple, 可变数据类型有:List, Dictionary, Set

变量和赋值

Python中变量不需要数据类型;同一变量可以反复赋值,而且可以是不同类型的变量;

x =‘Test’

x =100

x =11.22

x =2 +3j

可以多变量同时赋值。

x, y, z =1, 2, ‘Test’

print(x, y, z) #  输出:1 2 Test

运算符和表达式

算术运算: +, -, *, /, %, //, **

逻辑运算: and , or, not

关系运算: <, <=, >, >=, !=, ==

优先级:算术运算 >关系运算>逻辑运算

字符串

定义:引号直接的字符集合。可单引号 ‘, 双引号 ", 三引号’’’ 配对使用

\ 转义字符: \n 换行;\\ 反斜杠; \" 双引号;\t 制表符

r 前缀表示字符串不转义

字符串的运算

连接操作 +

s1 =“hello”

s2 =" World"

s = s1 + s2 #  hello World

重复操作 *

print(s *3 )

输出: hello Worldhello Worldhello World

索引 [ ]

print(s[1]) # e

索引规则:第一个字符的索引为0,后续字符依次递增

从右向左索引:最后一个字符索引为-1,前面字符依次减1

print(s[-1])  # d

切片 [:]

print(s[1:4])  # ell

print(s[:4]) # hell

print(s[1:]) # ello World

print(s[:]) #hello World

字符串常见属性方法:

capitalize( )

测试如下:

s =‘this is a test string’

print(s.capitalize())  # This is a test string

count[sub[,strat[,end]])

print(len(s)) # 21

print(s.count(‘t’)) # 4

print(s.count(‘t’, 4, 15))  # 2

endswith[sub[,start[,end]]),startswith[sub[,start[,end]])

print(s.startswith(‘T’)) # False

print(s.startswith(‘t’)) # True

find(sub[,start[,end]])

print(s.find(‘s’))  #  3

print(s.find(‘s’, 10, 20))  #  12

split(sep=None)

sp = s.split(’ ')

print(sp)  # [‘this’, ‘is’, ‘a’, ‘test’, ‘string’]

strip([chars]),默认删除空白字符

s1 = s.strip(‘thg’)

print(s1)  # is is a test strin

upper()/lower()

print(s.lower())  # this is a test string

print(s.upper())  #  THIS IS A TEST STRING

流程控制语句

条件语句

分支结构又称选择结构,依据判断条件,程序选择执行特定的代码。一个语句最多只能有一个else子句,且必须在整条语句的最后。

if  condition:

if-block

elif condition:

elif-block

else:

else-block

循环语句

循环结构是指满足一定条件的情况下,重复执行特定代码块的一种编码结构。

常见的循环语句while循环,for 循环

while condition:

while-block

测试我们熟悉的算法,从1加到100.

Total =0

i =0

while i <=100:

Total += i

i +=1

print(Total) # 5050

for 循环

for v in Seq:

for-block

使用for循环改写上测试代码,从1加到100.

Total =0

for iin range(1, 101):

Total += i

print(Total) # 5050

关于range() 函数: range(start, end, step)

start是数列开始值,默认0开始,end是终点值,不可缺省,step是步长,默认值是1. 产生的数不包含边界end

2. 内置数据类型

列表

列表是Python中最具灵活的有序集合对象类型。

具有可变长度,异构,任意嵌套列表的特点。

列表是可变对象,支持在原处修改。

列表常用方法:

append( )方法:在列表末尾追加元素

myList = [1, 2, 3]

myList.append(‘a’)

print(myList)  # [1, 2, 3, ‘a’]

insert(index,v):在指定位置插入元素

myList.insert(1, ‘b’)

print(myList) # [1, ‘b’, 2, 3, ‘a’]

index(v):返回列表中的第一个值为V的元素索引

print(myList.index(3))  # 3

remove(v):从列表中移除第一次找到的值v

MyList = [‘a’, ‘b’, ‘c’, ‘b’, ‘b’, ‘c’, ‘a’]

print(MyList)

MyList.remove(‘a’)

print(MyList)

输出:

[‘a’, ‘b’, ‘c’, ‘b’, ‘b’, ‘c’, ‘a’]

[‘b’, ‘c’, ‘b’, ‘b’, ‘c’, ‘a’]

pop( ),pop([i]):从列表指定位置删除元素,并将其返回,如果没有指定索引,则返回最后一个元素

p = MyList.pop()

print§  #a 最后一个元素

print(MyList) # [‘a’, ‘b’, ‘c’, ‘b’, ‘b’, ‘c’]

p = MyList.pop(2)

print§  #c  索引的第3个元素

print(MyList) #  [‘a’, ‘b’, ‘b’, ‘b’, ‘c’]

reverse( ):倒排列表中的元素

list2 = [3, 2, 9, 8, 6]

list2.reverse()

print(list2) #  [6, 8, 9, 2, 3]

count(x) : 返回x在列表中出现的次数

MyList = [‘a’, ‘b’, ‘c’, ‘b’, ‘b’, ‘c’, ‘a’]

print(MyList.count(‘a’)) # 3

print(MyList.count(‘c’))  # 2

sort(key=None, reverse=False ):对链表中的元素进行适当的排序

reverse = False 升序 --默认

reverse = True  降序

list2 = [3, 2, 9, 8, 6]

list2.sort(reverse=True)

print(list2)

list2.sort(reverse=False)

print(list2)

输出:

[9, 8, 6, 3, 2]

[2, 3, 6, 8, 9]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

flysh05

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

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

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

打赏作者

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

抵扣说明:

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

余额充值