A Byte of Python3 学习笔记

  • 第七章 控制流
    • 1.if语句(带输入函数)
    • 2.while语句
    • 3.for循环
    • 4.break、continue、return的区别
  • 第八章 函数
    • 8.1简介
    • 8.2函数参数
    • 8.3局部变量
    • 8.4全局变量——类似于C#中的静态变量
    • 8.6默认参数
    • 8.7 关键参数
    • 8.8VarArgs参数
    • 8.9Keyworld-only参数
    • 8.10 return语句
    • 9.4模块的__name__
  • 第10章 数据结构
    • 10.2列表
    • 10.4元组
    • 10.5字典
  • pycharm 小技巧
    • 1.批量注释快捷键
  • python常见错误
    • 1.Non-ASCII character'\xe6' in file”错误

第七章 控制流

1.if语句(带输入函数)

#if语句
number=23
#input() 数据输入函数,int()将字符串转换成int
guess=int(input('Enter an integer :'))if guess==number:# New block starts hereprint('Congratulations,you guessec it.')# New block end hereprint('(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()将字符串转换成intguess = int(input('Enter an integer :'))if guess == number:# New block starts hereprint('Congratulations,you guessec it.')running=False# New block end hereprint('(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')

注:range()函数用法

4.break、continue、return的区别

break:跳出本层循环。
continue:跳过当前循环块中的剩余语句,然后继续进行下一轮循环。
return:跳出当前函数。

第八章 函数

8.1简介

encoding:utf-8
def sayHello():print(' Hello World')
sayHello();
sayHello();

8.2函数参数

# encoding:utf-8def 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=2print('Changed local x to ',x)
func(x)
print('x is still',x)

8.4全局变量——类似于C#中的静态变量

# encoding:utf-8
#全局变量
x=50
def func():global xprint('x is ',x)x=2print('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=initialfor number in numbers:count+=numberfor 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  xelse: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 即可。

A Byte of Python3 学习笔记相关推荐

  1. python基础第三章选择结构答案-python3 学习笔记(二)选择结构、循环结构

    python3 学习笔记 python 优雅 明确 简单 1.选择结构 (1)简单判断 if else 使用格式: if  条件: 表达式1 else: 表达式2 (2)多条件判断 elif 使用格式 ...

  2. Python3学习笔记之-学习基础(第三篇)

    Python3学习笔记之-学习基础(第三篇) 文章目录 目录 Python3学习笔记之-学习基础(第三篇) 文章目录 一.循环 1.for循环 2.while循环 3.break,continue 二 ...

  3. Python3 学习笔记

    Python3 学习笔记 1.基础语法 1.1 字符串操作 title() 将单词首字母改为大写 upper() 所有字母改为大写 lower() 所有字母改为小写 str1+str2 字符串通过'+ ...

  4. Python3学习笔记01-环境安装和运行环境

    最近在学习Python3,想写一些自己的学习笔记.方便自己以后看,主要学习的资料来自菜鸟教程的Python3教程和廖雪峰官方网站的Python教程. 1.下载 1)打开https://www.pyth ...

  5. python3学习笔记(9)_closure

    1 #python 学习笔记 2017/07/13 2 # !/usr/bin/env python3 3 # -*- conding:utf-8 -*- 4 5 #从高阶函数的定义,我们可以知道,把 ...

  6. python3学习笔记:3.其他部分

    整理文档发现两年前学习paython3的笔记.当时工作有需要结果也没用上,现在忘的差不多了,在这里整理下. 按 菜鸟编程python3教程编写的demo.链接 这里主要是以前记录的一些资料链接 Pyt ...

  7. Python3学习笔记-数据类型和变量

    有C++基础,一直对"万能"的Python语言感兴趣,目前正在学习廖雪峰老师的Python3教程和其他资料用来入门,这里记录一些没接触过或与C++有差异的知识,方便自己查阅吧~ 字 ...

  8. Python3学习笔记----环境安装及文本编辑器的选择

    在线学习网站:廖雪峰的Python3教程网站 如果你正在使用Mac,系统是OS X 10.8~10.10,那么系统自带的Python版本是2.7.如要安装最新的Python 3,有两个方法: 方法一: ...

  9. Mooc的Python3学习笔记

    文章目录 一些优秀的博主 仅供自己查阅!!! 首先是掌握基本语法! 内置的运算符函数 函数模块补充知识点 pass 函数返回多个值 关于默认参数使用的注意事项 可变参数的使用方法 天天向上代码 单元测 ...

最新文章

  1. Spring Boot 监听 Redis Key 失效事件实现定时任务
  2. 如何学好单片机编程?学好单片机的基础是什么?
  3. R语言ggplot2可视化气泡图:无填充色的气泡图、自定义填充色的气泡图
  4. Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax
  5. Redis的Hash操作
  6. JavaScript 基础知识 - BOM篇
  7. 微软系统修复工具(试用版)
  8. PHP付费资源下载交易平台网站源码
  9. c# 操作word中在右下角插入图片
  10. CSS雪碧,即CSS Sprite 简单的例子
  11. 每日一句20191105
  12. Asp.net页面生命周期详解任我行(2)-WebForm页面生命周期WEBFORM_ASPNET控件树的生成和作用...
  13. QQ文件中转站 发送给好友的功能 哪去了?
  14. 图的深度优先遍历java代码详解
  15. Java毕设项目-学生档案管理系统
  16. “我没干过华为OD的岗位,但它是外包,我就要怼”,什么心态?
  17. Goby内测版1.8.292|后台扫描、导出截图等功能上线(文末福利等你~)
  18. mysql实体监听器_使用remote_listener实现数据库与监听器分离 | HelloDML
  19. 微信小程序中的iPhone X适配问题
  20. 主机调优20141226

热门文章

  1. vue中 给元素添加鼠标移入,鼠标移出的效果的事件
  2. 【愚公系列】2022年04月 微信小程序-项目篇(公交查询)-01周边站点
  3. xp远程登录linux,Linux操作系统下如何远程登录XP桌面
  4. 自动驾驶与python_Python对自动驾驶技术的重要作用
  5. 如何管理计算机中文件,如何管理文件 -电脑资料
  6. c语言 字符串转换中文乱码,怎么将unicode转中文字符编码存在文本中
  7. 使用python计算一年有多少秒_python获取一年所有的日期
  8. 写代码之前要做什么?
  9. Mysql数据库的引擎介绍
  10. [转载] 苹果 AppStore 应用商店生存之道