Python入门(一)之Python语法 本文主要来源于Python编程从入门到实践

  本文不做对于Python过多的介绍主要就是放入Python的源代码,方便读者进行阅读和使用,希望可以帮到读者们。

第一章
主要对Python的介绍和安装在这里就不多做介绍等
第二章 变量和简单数据类型
2.1运行hellowrd.py时发生的情况

a=1
b=1
#打印a,b
print(a,b)
print("helloword")

主要就是让我们知道Python的print()和对变量的操作等等,
2.2变量
本节跳过,很重要。从中可以知道变量的类型 比较常见的就是整型,浮点型和字符型等等。Python与c等不同的地方就是直接写就用,与MATLAB类似。
2.3使用方法修改字符串的大小写

"""字符串的操作"""
"""
#python注释的方法
'''
python的多行注释
'''
name="adc Love cda"
#字母的大小写调节
print(name.title())
print(name.upper())
print(name.lower())

#字符串的拼接“+”
FirstName="adc"
LastName="Love"
FullName=FirstName+" "+LastName
print(FullName)

print("Hello, "+FullName.title()+"!")
message=FullName
print(message)

"""

"""
    删除空白并不修改变量
    若需要修改则重新赋值

"""
#删除后面空白rstrip
FavoriteLanguage="python "
FavoriteLanguage.rstrip()  #删除但不修改原变量
FavoriteLanguage
#修改原变量
FavoriteLanguage=FavoriteLanguage.rstrip()


#删除前面空白 lstrip
FavoriteLanguage=" python  "
FavoriteLanguage.lstrip()

#删除双边空白 strip
FavoriteLanguage.strip()


在这里需要注意一下Python的多行注释为<开始>:""" +“”“”<结束>在上述代码中有例子,需要读者自己看一下

2.4 数字
也就是整数和浮点数
2.4.3使用函数str()避免类型错误

age=23
message="Happy"+str(age)+"Brithday" #错误的表达式message="Happy"+age+"Brithday"
print(message)

2.5注释
主要为#号,其中多行注释书中并未提到,我在上面也说过了

第三章 列表
3.1 列表是什么

#列表   下标由0-(n-1)
bicycles=['trek','cannondale','redline','specialized']
print(bicycles)
#访问列表元素
print(bicycles[0])
print(bicycles[1])
#首字母大写
print(bicycles[0].title())
#负数访问为倒序
print(bicycles[-1])
print(bicycles[-4])

message="My favorite bicycle is "+bicycles[2]+"."
print(message)

3.2修改、添加和删除元素

#改变元素中的变量
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)

#列表中添加元素
#1.元素后添加append
motorcycles.append('ducati')
print(motorcycles)
#空列表添加
motorcycles=[] #空白数组
motorcycles.append('ducati')
motorcycles.append('honda')
motorcycles.append('yamaha')
print(motorcycles)

#2索引号前插入新值insert(索引号,添加的内容)
motorcycles.insert(0,"suzuki")
print(motorcycles)

#3.1从列表中删除 del 删除的元素

del motorcycles[0]
print(motorcycles)

#3.2 从列表末进行删除,并存储 pop
people_motorcycles=motorcycles.pop()
print(motorcycles)
print(people_motorcycles)

motorcycles=['honda','yamaha','suzuki']
first_owned=motorcycles.pop(0) #添加索引号,表示下标
print('this is my first ' + first_owned +' motorcycle')

#4.1 根据值进行删除
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.remove("honda")
print(motorcycles)
#4.2删除并继续使用
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
too_expensive='honda'
motorcycles.remove(too_expensive)
print(motorcycles)
print(too_expensive+ " is so expensive motorcycles")



3.3组织列表

#列表排序 sort按照首字母永久性排序
cars=['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

#临时修改sorted()
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:")
print(cars)

#倒着打印

cars.reverse()
print(cars)


#确定数列的长度
print(len(cars))

3.4使用列表时避免索引的错误
希望读者仔细对照该书进行学习

第四章 操作列表
4.1遍历整个列表

 #for循环进行列表遍历,在进行需要循环的语句时注意缩进

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

本章主要注意的就是缩进的问题,记住当你需要执行在该循环的过程时必须缩进于该语句,与c语言等可以用{}扩起来,但是对于Python,它不吃这套。还有别忘了在for语句后添加:
4.3 创建数值列表

#用range产生一系列数 range(首数,n)  首数...n-1
for value in range(1,5):
    print(value)
print(value)   #单独打出为最后一个数

for value in range(-2,5):
    print(value)

#使用range创建列表系统默认间隔为1
number=list(range(-1,5))
print(number)
#使用range(首数,n,间隔)
even_number=list(range(-2,11,2))
print(even_number)
#使用append进行添加

squares=[]
for value in range(-2,11,2):
    square=value**2
    squares.append(square)
print(squares)
#改进的算法,无中间变量
squares=[]
for value in range(-2,11,2):
    squares.append(value**2)
print(squares)

#对数字进行简单的统计
digits=[1,2,3,4,5,6,7,8]
print(min(digits))
print(max(digits))
print(sum(digits))
#数列解析
squares=[value**2 for value in range(-2,11,2)]
print(squares)

4.4 使用列表的一部分

'''
#使用数列中的一部分
'''
#切片  列表名[起始位置,终止位置] 起点...(终点-1)
players=['charles','martina','florence','eli']
print(players[0:3])
#无索引号,默认从头开始
print(players[:4])
#无索引号,默认一直到结束
print(players[2:])
#倒序
print(players[-3:])

#遍历切片

print("This is a list about all players")
for play in players[:]:
    print(play)

4.4.3 复制列表

#复制列表
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods
print("My foods are:")
print(my_foods)
print('My friend foods are:')
print(friend_foods)

#分别添加内容改变
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods
my_foods.append("ice cream")
friend_foods.append('cannoli')
print("My foods are:")
print(my_foods)
print('My friend foods are:')
print(friend_foods)

#分别添加内容不改变
my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
my_foods.append("ice cream")
print("My foods are:")
print(my_foods)
friend_foods.append('cannoli')
print('My friend foods are:')
print(friend_foods)

for food in my_foods:
    print(food)
print("\n")    
for food in friend_foods:
    print(food)

4.5 元组

#定义元组
dimensions=[200,500]
print(dimensions[0])
print(dimensions[1])
#dimensions[0]=5  错误的操作,不能直接对列表中的值进行修改

#遍历元组中的所有值
dimensions=(200,50)
for dimensioon in dimensions:
    print(dimensioon)

#修改元组变量
dimensions=(200,50)
print('Original dimensions:')
for dimension in dimensions:
        print(dimension)

print('\nModified dimensions:')
dimension=(400,100)
for dimension in dimensions:
        print(dimension)

不知不觉就写了那么多行,大概花了1个多小时,希望能够帮助到读者,本文仅供读者们学习,请勿用于商业用途,违者必究。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值