A Byte of Python3 学习笔记
第七章 控制流
1.if语句(带输入函数)
#if语句
number=23
#input() 数据输入函数,int()将字符串转换成int
guess=int(input('Enter an integer :'))
if guess==number:
# New block starts here
print('Congratulations,you guessec it.')
# New block end here
print('(but you do not win any prizes)')
elif guess<number:
print('No,it is a little higer than that')
else:
print('No,it is a little lower than that')
print('Done')
2.while语句
# encoding:utf-8
#while...else(else可选)
number=23
running=True
while running:
# input() 数据输入函数,int()将字符串转换成int
guess = int(input('Enter an integer :'))
if guess == number:
# New block starts here
print('Congratulations,you guessec it.')
running=False
# New block end here
print('(but you do not win any prizes)')
elif guess < number:
print('No,it is a little higer than that')
else:
print('No,it is a little lower than that')
else:
print("the while loop is over.")
print('Done')
3.for循环
encoding:utf-8
#for...in ...else(else可选)
for i in range(1,5):
print(i)
else:
print('The for loop is over')
4.break、continue、return的区别
break:跳出本层循环。
continue:跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
return:跳出当前函数。
第八章 函数
8.1简介
encoding:utf-8
def sayHello():
print(' Hello World')
sayHello();
sayHello();
8.2函数参数
# encoding:utf-8
def printMax(a,b):
if a>b:
print(a,'is maximun')
elif a==b:
print(a,'is equal to',b)
else:
print(b,'is maximum')
printMax(3,4)
x=5
y=7
printMax(x,y)
8.3局部变量
# encoding:utf-8
#局部变量
x=50
def func(x):
print('x is ',x)
x=2
print('Changed local x to ',x)
func(x)
print('x is still',x)
8.4全局变量——类似于C#中的静态变量
# encoding:utf-8
#全局变量
x=50
def func():
global x
print('x is ',x)
x=2
print('Changed local x to ',x)
func()
print('value of x is',x)
8.6默认参数
# encoding:utf-8
#默认参数
def say(message,times=1):
print(message*times)
say('Hello')
say('World',5)
注:只有在形参表末尾的那些参数可以有默认参数值,即你不能在申明函数形参的时候,先申明有默认值的形参而后申明没有默认值的形参。这是因为赋给形参的值是根据位置而赋值的。例如,def func(a,b=5): 是有效的,但是def func(a=5,b): 是无效的。
8.7 关键参数
使用名字(关键字)而不是位置来给函数指定实参。
优势:一、由于我们不必担心参数的顺序,使用函数变得更加简单了。二、假设其他参数都有默认值,我们可以只给我们想要的那些参数赋值。
# encoding:utf-8
#关键参数
def func(a,b=5,c=10):
print('a is',a,'and b is',b,'and c is',c)
func(3,7)
func(25,c=24)
func(c=50,a=100)
8.8VarArgs参数
定义一个能获取任意个数参数的函数,这可通过使用*号来实现。
# encoding:utf-8
#VarArgs参数
def total(initial=5,*numbers,**keywords):
count=initial
for number in numbers:
count+=number
for key in keywords:
count+=keywords[key]
return count
print(total( 10, 1, 2, 3, vegetables=50, fruitus=100))
当我们定义一个带一个星的参数,像*param时,从那一点后所有的参数被收集为一个叫做’param‘的列表。
当我们定义一个带两个星的参数,像**param ,从那一点开始的所有的关键字参数会被收集为一个叫做’param‘的字典。
8.9Keyworld-only参数
8.10 return语句
return语句用来从一个函数返回即跳出函数,我们也可选是否从一个函数返回一个值。
# encoding:utf-8
#return语句
def maximum(x,y):
if x>y:
return x
else:
return y
print(maximum(2,3))
注:没有返回值的return语句等价于return none.
9.4模块的__name__
假如我们只想在程序本身被使用的时候运行主块,而它在被别的模块输入调用的时候不运行主块,这个时候就用模块的__name__属性
# encoding:utf-8
#模块的__name__
if __name__=='__main__':
print('This program is being run by itself')
else:
print('I am being imported from another module')
第10章 数据结构
在Python中有四种内建的数据结构——列表、元祖、字典和集合。
10.2列表
列表中的项目应该包括在方括号中,每个项目用逗号分隔。
# encoding:utf-8
#列表
# This is my shopping list
#创建list
shoplist=['apple','mango','carrot','banana']
#len 计算list长度
print('I have ',len(shoplist),'items to purchase')
#一个一个打印list
print('These items are :',end=' ')
for item in shoplist:
print(item,end=' ')
print('\nI also have to buy rice.')
#在list末尾添加‘rice’
shoplist.append('rice')
#一次性打印出list中的所有内容
print('My shopping list is now',shoplist)
print('I will sort my list now')
#给list用sort排序
shoplist.sort()
#一次性打印出list中的所有内容
print('Sorted shopping list is',shoplist)
#打印指定元素
print('The first item I will buy is',shoplist[0])
olditem=shoplist[0]
#删掉元素
del shoplist[0]
print('I bought the', olditem)
print('My shopping list is now',shoplist)
10.4元组
元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义
# encoding:utf-8
#元组
#remember the parentheses are optional
zoo=('python','elephant','penguin')
print('Number of animals in the zoo is',len(zoo))
new_zoo=('mokey','camel',zoo)
print('Number of cages in the new zoo is',len(new_zoo))
print('All animals in new zoo are ',new_zoo)
print('Animals brought from old zoo are', new_zoo[2])
print('Last animal brought from old zoo is',new_zoo[2][2])
print('Number of animal in the new zoo is',len(new_zoo)-1+len(new_zoo[2]))
注:元组之内的元组不会丢失它的特性
10.5字典
字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细地址簿)联系在一起。注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息。
注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以把不可变或可变的对象作为字典的值。
键/值对用冒号分隔,而各个对用逗号分隔。
字典中的键/值对是没有顺序的。
# encoding:utf-8
#字典
#创建字典
ab={
'Swaroop':'swaroop@swaroopch,com',
'Larry':'larry@wall.org',
'Matsumoto':'matz@ruby-lang.org',
'Spammer':'spammer@hotmail.com'
}
print("Swaroop's address is ",ab['Swaroop'])
#Deleting a key-value pair
del ab['Spammer']
print('\nThere are {0} contacts in the address-book\n '.format(len(ab)))
#使用字典的items方法,来使用字典中的每个键值对,这会返回一个元组的列表,
# 其中每个元组都包含一对项目
#字典的键值对是没有顺序的
for name,address in ab.items():
print('Contact {0} at {1}'.format(name,address))
#字典添加元素
ab['Guido']='guido@python.org'
#用in 操作符检验一个键值对是否存在,或使用dict 类的has_key方法
if 'Guido' in ab: #OR ab.has_key('Guido')
print("\nGuido's address is",ab['Guido'])
pycharm 小技巧
1.批量注释快捷键
代码选中的条件下,同时按住 Ctrl+/,被选中行被注释,再次按下Ctrl+/,注释被取消
python常见错误
1.Non-ASCII character’\xe6’ in file”错误
错误原因:程序爆出这个错误一般是程序中带有中文。
解决方法:在程序的开头加上 encoding:utf-8 即可。
来源:CSDN
作者:Ybossceo
链接:https://blog.csdn.net/weixin_43951995/article/details/99745893