Python编程:从C语言到实践(适合有C语言基础的同学使用)【自学用,未完待续】

字符串

使用方法(函数)修改字符串的大小写
先定义一个字符串变量,这三个内置函数功能分别为

  1. 每个单词的首字母大写
  2. 全为大写
  3. 全为小写
name = "Ada Lovelace" 
print(name.title())
print(name.upper()) 
print(name.lower())
Ada Lovelace
ADA LOVELACE 
ada lovelace

合并(拼接)字符串

python用+来拼接字符串

first_name = "ada" 
last_name = "lovelace" 
full_name = first_name + " " + last_name 
print(full_name)

其结果如下:

ada lovelace

使用制表符或换行符来添加空白

制表符:\t,相当于TAB,可以添加空白
换行符:\n,另起一行,和c语言一致

>>> print("Python") 
Python
>>> print("\tPython") 
	Python
>>> print("Languages:\nPython\nC\nJavaScript")
Languages: 
Python 
C 
JavaScript

>>> print("Languages:\n\tPython\n\tC\n\tJavaScript") 
Languages: 
	Python 
	C 
	JavaScript

删除空白

一般删除的是字符串中的空白,有三个函数

  1. rstrip():剔除字符串末尾空白
  2. lstrip():剔除字符串开头的空白
  3. strip():同时剔除字符串两端的空白
>>> favorite_language = ' python '
>>> favorite_language.rstrip()
' python' 
 >>> favorite_language.lstrip()
'python ' 
 >>> favorite_language.strip() 
'python'

使用函数 str()避免类型错误

例如,假设你要祝人生日快乐,可能会编写类似于下面的代码:

age = '23' 
message = "Happy " + age + "rd Birthday!"
print(message)

但如果你运行这些代码,将发现它们会引发错误
因为23是int型变量,并不是字符串,为此,可调用函数str(),它让Python将非字符串值表示为字符串:

age = 23 
message = "Happy " + str(age) + "rd Birthday!" 
print(message)

#结果为Happy 23rd Birthday!在python中#为注释符

列表

列表当中可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表。
例如:一个自行车列表

bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 
print(bicycles)

结果

['trek', 'cannondale', 'redline', 'specialized']

访问列表元素

方式:采用下标的方式,索引还是从0开始,特殊的一点是当索引为-1的时候代表列表倒数第一个元素,-2代表倒数第二个,以此类推

bicycles = ['trek', 'cannondale', 'redline', 'specialized'] 
print(bicycles[1]) 
print(bicycles[3])

cannondale 
specialized
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])

specialized

修改列表元素

方式:将新的元素值赋值给列表中的元素(元素用下标表示)

 motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
 motorcycles[0] = 'ducati' 
print(motorcycles)

['honda', 'yamaha', 'suzuki'] 
['ducati', 'yamaha', 'suzuki']

在列表中添加元素

有两种添加元素的方法:

  1. 在列表末尾添加元素,要用到一个函数append()
  2. 在列表中插入元素,insert(a,b),a表示所插入的位置,b表示要插入的元素值

例一:

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
motorcycles.append('ducati') 
print(motorcycles) 

['honda', 'yamaha', 'suzuki'] 
['honda', 'yamaha', 'suzuki', 'ducati']

例二:

motorcycles = ['honda', 'yamaha', 'suzuki'] 
motorcycles.insert(0, 'ducati') 
print(motorcycles) 

['ducati', 'honda', 'yamaha', 'suzuki']

从列表中删除元素

有三种删除方法:

  1. 使用del语句删除元素,需要知道元素的位置(下标),使用次语句后,被删除的元素就再也无法访问了。
  2. 使用方法pop()删除元素,不指出pop的下标,便是删除末尾的元素;指出要pop的元素的下标,那表示删除此下标元素
  3. 根据值删除元素,要用到方法remove(),括号内是要删除的元素,元素要用引号引起来。

例一:

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
del motorcycles[0] 
print(motorcycles) 

['honda', 'yamaha', 'suzuki'] 
['yamaha', 'suzuki']

例二:

motorcycles = ['honda', 'yamaha', 'suzuki'] 
print(motorcycles) 
popped_motorcycle = motorcycles.pop() 
print(motorcycles) 
print(popped_motorcycle)

['honda', 'yamaha', 'suzuki'] 
['honda', 'yamaha'] 
suzuki
motorcycles = ['honda', 'yamaha', 'suzuki'] 
first_owned = motorcycles.pop(0) 
print('The first motorcycle I owned was a ' + first_owned.title() + '.') 

The first motorcycle I owned was a Honda.

例三:

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 
print(motorcycles) 
motorcycles.remove('ducati') 
print(motorcycles) 
 
['honda', 'yamaha', 'suzuki', 'ducati'] 
['honda', 'yamaha', 'suzuki']

因为我们知道要删除的值是什么,所以可以预先将其存放在一个变量当中,这样就算我们删除了列表中的值,我们变量中还备份了。
注意:方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

组织列表

通俗来讲,组织列表的意思是对列表中的元素进行整理组织
那么字符元素怎么整理呢?
就要用到字母排序了
我们通过介绍四个函数来掌握这节内容:

  • sort():对列表进行永久性的字母顺序排序,也可以使用sort(reverse=True)来按照字母顺序相反的顺序排列
  • sorted():对列表元素进行临时排序,同时不影响它们在列表中的原始排列顺序
  • reverse():反转列表元素的排列顺序;注意,reverse()不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序
  • len():确定列表的长度

例一:

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort()
print(cars) 

['audi', 'bmw', 'subaru', 'toyota']
cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort(reverse=True) 
print(cars) 

['toyota', 'subaru', 'bmw', 'audi']

例二:

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print("Here is the original list:") 
print(cars) 
print("\nHere is the sorted list:") 
print(sorted(cars)) 
print("\nHere is the original list again:") 
print(cars) 

Here is the original list: 
['bmw', 'audi', 'toyota', 'subaru'] 
Here is the sorted list: 
['audi', 'bmw', 'subaru', 'toyota'] 
 Here is the original list again: 
['bmw', 'audi', 'toyota', 'subaru']

例三:

cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print(cars) 
cars.reverse() 
print(cars) 

['bmw', 'audi', 'toyota', 'subaru'] 
['subaru', 'toyota', 'audi', 'bmw']

例四:

>>> cars = ['bmw', 'audi', 'toyota', 'subaru']
>>> len(cars)
4

操作列表

遍历整个列表

在Python中我们遍历整个列表用的还是for循环,跟c语言中是一致的,不过就是for语句的形式不一样,先感受一下这俩例子:
例一:

magicians = ['alice', 'david', 'carolina'] 
for magician in magicians: 
	print(magician)

alice 
david 
carolina

例二:

magicians = ['alice', 'david', 'carolina'] 
for magician in magicians:
print(magician.title() + ", that was a great trick!") 

Alice, that was a great trick! 
David, that was a great trick! 
Carolina, that was a great trick!

看例一,关键字for后面紧跟着的是一个变量magician,用来存放列表magicians中的第一个元素,冒号后面是要进行的操作,完成循环内的操作之后再取列表的第二个元素进行操作这样以此进行。
我们仔细一点可以发现for语句和下方的print语句并不是对齐的,而是有缩进的,原来在Python中要用缩进来表示语句与语句之间的从属或者并列关系。例如:

magicians = ['alice', 'david', 'carolina']
for magician in magicians:
	print(magician.title() + ", that was a great trick!")
	print("I can't wait to see your next trick, " + magician.title() + ".\n") 

Alice, that was a great trick! 
I can't wait to see your next trick, Alice. 

David, that was a great trick! 
I can't wait to see your next trick, David.

Carolina, that was a great trick! 
I can't wait to see your next trick, Carolina.

上面这个例子中两个print语句均缩进了,所以他们都需要在循环中执行

magicians = ['alice', 'david', 'carolina'] 
for magician in magicians: 
	print(magician.title() + ", that was a great trick!") 
print("I can't wait to see your next trick, " + magician.title() + ".\n")

Alice, that was a great trick! 
David, that was a great trick! 
Carolina, that was a great trick! 
I can't wait to see your next trick, Carolina.

而在这个例子中,第二个print语句并没有缩进,也就是不在循环操作中

切片

它是对什么的切片?
答:是对列表的,其方法是:列表名[a:b],其中a代表开始元素的索引,b代表的是结束元素的索引,但是有一点需要注意,切片当中不包含下标为b的元素,这跟range函数的性质一样

切片技巧:

  • 如果你要提取列表的第n~m个元素,可将起始索引指定为n-1,并将终止索引指定为m,可使用列表名[n-1:m]。
  • 如果你要提取列表的前n个元素,可使用列表名[:n]
  • 如果你要输出列表中的最后n名元素,可使用切片列表名[-n:]

现在回忆一下自己学过的知识:

  1. 如何高效地处理列表中的元素?
  2. 如何使用for循环遍历列表?
  3. Python如何根据缩进来确定程序的结构以及如何避免一些常见的缩进错误?
  4. 如何创建简单的数字列表?以及可对数字列表执行的一些操作?
  5. 如何通过切片来使用列表的一部分和复制列表
  6. 你还学习了元组(它对不应变化的值提供了一定程度的保护),以及在代码变得越来越复杂时如何设置格式

复制列表

复制列表有两种:

  1. 将一个列表赋值给另一个列表,此时这两个列表指向的是同一份数据,也就是同一份数据拥有两个名字,对其中的任意一个列表操作就是对这份数据的操作
  2. 利用对列表的切片操作[:],实现对副本列表和原生列表的各自操作,也就是这会儿有两份初始的相同的数据,他们有不同的名字,你可以分别操作不同的列表但是结果均保存在各自列表中

例一:

my_foods = ['pizza', 'falafel', 'carrot cake'] 
friend_foods = my_foods[:] 
print("My favorite foods are:") 
print(my_foods) 
print("\nMy friend's favorite foods are:") 
print(friend_foods) 

My favorite foods are: 
['pizza', 'falafel', 'carrot cake'] 
My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake']

例二:

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli') 
friend_foods.append('ice cream') 
print("My favorite foods are:")
print(my_foods) 
print("\nMy friend's favorite foods are:")
print(friend_foods)

My favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'cannoli'] 
My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'ice cream']

元组

元组创建一系列不可修改的元素,用圆括号标注

定义元组

元组名 = (a,b,c)     #其中a,b,c为元素

定义好元组,使用元组的元素时也是用索引跟列表一样
例一:

dimensions = (200, 50) 
print(dimensions[0]) 
print(dimensions[1]) 

200 
50

元组不允许单独修改其中的某个元素的值

遍历元组中的所有值

同列表的for循环
例一:

dimensions = (200, 50) 
for dimension in dimensions: 
	print(dimension) 

200 
50

修改元组变量

因为不能单独地修改某个元素的值,所以只能对元组进行重新定义,可以不修改元组名,然后将所要替换的元组内容直接赋值给当前元组名:

dimensions = (200, 50)
print("Original dimensions:") 
for dimension in dimensions: 
	print(dimension) 
dimensions = (400, 100) 
print("\nModified dimensions:") 
for dimension in dimensions: 
	print(dimension) 

Original dimensions: 
200 
50 
Modified dimensions: 
400 
100

元组和列表的区别

元组不可单独修改,列表随意动态修改
元组用圆括号来标识,列表用方括号来标识
元组的元素不可更改,列表的元素可多可少可更改
修改元组的元素只能重新定义元组

从作用来说:

  • 元组和c语言的数组是有相近的,但是有点区别。元组是只要定义了,元组的长度和c语言的数组都是不可更改的,但是数组的值是可以单独改变的,而元组不行
  • 列表 和c++标准库中的向量vector是相近的

if语句

if语句

if-else语句
if-elif-else
测试多个条件
就是创建多个if语句,这样就可以执行多个判断语句,如果判断成立则执行相应的代码块,而不至于像if-elif-else,如果其中一个判断条件成立则执行相应的代码块,其他判断语句都将跳过
多if条件语句和if-elif-else条件语句
如果你想执行多个代码块那就执行多if条件语句,如果想执行一个代码块就执行if-elif-else

字典

举一个不太恰当的的比喻:
字典里存的是成双成对的情侣(键—值)
元组和列表存的是单身狗
哈哈哈哈哈

访问字典中的值

添加键—值对

修改字典中的值

删除键—值对

遍历字典

四种遍历字典的情形:

  • 遍历所有的键—值对
  • 遍历字典中的所有键
  • 按顺序遍历字典中的所有键
  • 遍历字典中的所有值

嵌套

有三种嵌套方式:

  • 在列表中嵌套字典,将字典名作为列表的元素罗列出来放在中括号中
  • 在字典中嵌套列表 ,将字典中键对的值作为一个列表
  • 在字典中嵌套字典,将字典中键对的值作为另一个字典

用户输入和while循环

用户输入

有两种方式输入:

  • input()函数,面向用户文本的输入
  • int()函数,将文本输入转换为数据类型

while循环

for循环用于针对集合中的每个元素都一个代码块,而while循环不断地运行,直到指定的条件不满足为止。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Fruit_Caller

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

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

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

打赏作者

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

抵扣说明:

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

余额充值