分为上下两篇,上篇输出,列表及其操作和if语句。下篇包括字典,输入,while循环与函数。

字典

知识点:

  1. 概念:在Python中,字典 是一系列键—值对。每个键 都与一个值相关联,你可以使用键来访问与之相关联的值。与键相关联的值可以是数字、字符串、列表乃至字典。事实上,可将 任何Python对象用作字典中的值。 在Python中,字典用放在花括号{} 中的一系列键—值对表示。格式:card={'name':'jack','sex':''male}(card 为字典名,name,sex为键名,‘jack’和‘male’为与键对应的值,中间用“,”连接)。
  2. 基本操作:访问(字典名[‘键名’]),添加(直接字典名[‘键名’]=值),修改(字典名[‘键名’]=…),删除键值对(del 字典名[‘键名’])。
  3. 遍历字典:遍历所有的键值对(for a,b(自定义变量名) 字典名.items():),遍历所有的键(for a(变量名) in 字典名.keys():),按顺序遍历所有的键(for a in sorted(字典名)),遍历所有的值(for a(变量名) in 字典名.values(): )。
  4. 嵌套:要将一系列字典存储在列表中,或将列表作为值存储在字典中,这称为嵌套。基本操作有:字典列表,在字典中存储列表,在字典中存储字典。

实践代码如下:

family={'name':'morty','age':10}#字典表示
print(family['name'])#访问键-值对
print(family['age'])
family['sex']='male'#添加键-值对
print(family)
people={}#创建空字典
people['name']='beth'
print(people)
people['name']=people['name']+' and jerry'#修改值
print(people)del family['sex']#删除键-值对
print(family)card={'name1':'jack','sex1':'male','name2':'rose','sex2':'female'
}for key,value in card.items():#遍历字典中所有键值对print("\nkey: "+key)print("\nvalue: "+value)for name in card.keys():#遍历字典中所有的键print(name.title())for value in card.values():#遍历字典中所有的值print(value.title())for key in sorted(card):#按顺序遍历所有键print(key.title())card={'name1':'rick','name2':'rick','name3':'jerry'}#遍历字典中所有的值,set()函数表示去掉重复的值
for value in set(card.values()):print(value.title())card1={'name':'rick','sex':'male','relationship':'grandpa'}
card2={'name':'morty','sex':'male','relationship':'grandson'}
cardmax=[card1,card2]#嵌套,列表中存储字典
for card in cardmax:print(card)card12={'name':['jack','rose'],'sex':['male','female']}#字典中存储列表
for name,sex in card12.items():#遍历print(name.title())for value in sex:print("\t"+value.title())card13={'card01':{'name':'jack','sex':'male' },'card02':{'name':'rose','sex':'female'}}#字典中嵌套字典
for key,value in card13.items():#遍历print(key)for key1,value1 in value.items():print(key1.title()+":")print("\t"+value1.title())

实践结果:

morty
10
{'name': 'morty', 'age': 10, 'sex': 'male'}
{'name': 'beth'}
{'name': 'beth and jerry'}
{'name': 'morty', 'age': 10}key: name1value: jackkey: sex1value: malekey: name2value: rosekey: sex2value: female
Name1
Sex1
Name2
Sex2
Jack
Male
Rose
Female
Name1
Name2
Sex1
Sex2
Rick
Jerry
{'name': 'rick', 'sex': 'male', 'relationship': 'grandpa'}
{'name': 'morty', 'sex': 'male', 'relationship': 'grandson'}
NameJackRose
SexMaleFemale
card01
Name:Jack
Sex:Male
card02
Name:Rose
Sex:Female

用户输入和while循环

知识点:

  1. 用户输入:字符串类输入(input()函数),数值类输入(input()函数加上int()函数),多行输入(+="’…")
  2. while循环可实现功能:选择退出,使用标志(True 与False),continue与break的使用,无限循环(while True:)
  3. while在列表与字典中常用用法:列表间移动元素,删除特定元素,用户输入填充字典

实践代码如下:

big=input("just chose a number which is you favourite from 0 to 9?:")
big=int(big)if big==0:message1=input("Are you going to join us?(yes or no):")#输入if message1=='yes':print("It's pretty neat!")else:print("You wish!")message21=input("tell me your first name,and i wil tell you something back:")message22=input("Now it's turn to your last name:")message2=message21.title()+" "+message22.title()print("hahaha!"+message2+" Welcome to our Club in the world ofyes Python!")message3="And i will tell you more."#多行输入message3="Now tell me are you in Rick Sanchez of Earth dimension c-137?:(yes or no)"chose=input(message3)if chose=="yes":print("you are under arrest for crimes against alternate "+message2+" by the authority of the Trans-Dimensional Council of "+message2+"s.")else:print("oh!i just want to waste some time missing with you!")
if big==1:age=input("Please tell me your age:")age=int(age)#int()获取数值输入#print("Really?"+int(age)+" ?")can only concatenate str (not "int") to strif age>=16:print("Great!")else:print("Oh!Man!")
if big==2:num=0name=['r','i','c','k']while num<=3:#while循环print(name[num])num+=1
if big==3:#选择退出too="Now you have a chance to back off."too+="If you really think so.Just enter 'quit' to end the program.:"message4=""while message4 !='quit':message4=input(too)if message4 !='quit':#退出时不打印print(message4)
if big==4:active=True#标志too = "Now you have a chance to back off."too += "If you really think so.Just enter 'quit' to end the program.:"while active:message4=input(too)if message4 =='quit':active=Falseelse:print(message4)
if big==5:too = "Now you have a chance to back off."too += "If you really think so.Just enter 'quit' to end the program.:"while True:#用break退出while循环message4=input(too)if message4 == 'quit':breakelse:print(message4)
if big==6:message4=0while message4<=9:message4 += 1if message4 %2==0:#%求模运算符print(str(message4)+": :)")else:print(message4)continue
if big==7:#检验无限循环active=Truewhile active:print("morty!")
if big==8:#列表间移动元素team1=['sher','paul','helen']team2=[]while team1:member=team1.pop()print(member.title())team2.append(member)print("team2:")for member in team2:print(member.title())
if big==9:#删除特定值world=['rick','morty','summer','beth','rick','rick','morty','rick','jerry','sheep-rick']print(world)while 'rick' in world:world.remove("rick")print(world)tool=list(range(0,10))
print(tool)
book={'number':tool}
active=True
while active:#用户输入填充字典key=input("tell me a new key:")value=input("a new value:")book[key]=valuethen=input("Would you like to add more?(yes or no):")if then=="no":active=False
print(book)

函数

知识点:

  1. 定义函数(def 函数名:)
  2. 传递实参:有默认值得函数调用,等效函数调用
  3. 返回值
  4. 传递列表:在函数中修改列表,禁止函数修改列表(函数名(列表名[:]))
  5. 传递任意数量的实参(*/**)
  6. 将函数存储在模块中:导入整个模块(import 模块名),导入特定函数(from 模块名 import 函数名),用as给模块/函数指定别名(import 模块名 as 别名/from 模块名 import 函数名 as 别名),导入模块中所有函数(import 模块名*),

实践代码(不含模块)如下:

def name():#定义函数getname=input("tell me your name:")print("hello! "+getname+" !")def name1(firstname,lastname):#传递实参wholename=firstname.title()+" "+lastname.title()print("hi, "+wholename+" !")
def name2(firstname,lastname="world"):#设置默认值print(firstname+" :)")def age(myage,yourage):agenow=yourage-myage;return agenow#返回值def effects(firstname1,lastname1):firstname1.append('liu')lastname1.append('lan')ooldname={'firstname':firstname1,'lastname':lastname1}return ooldname#返回字典def name3(*value):#传递任何数量的实参newname.append(value)def name4(firstname2,lastname2,**inpuut):profile['firstname']=firstname2profile['lastname']=lastname2for key,value in inpuut.items():profile[key]=valueprofile={}
firstname1=['li','he','wang']
lastname1=['bai','qing','hong']
newname=[]
name()
first=input("tell me your first name:")
last=input("tell me your last name:")
name1(first,last)#传递位置实参
name1(firstname="hello",lastname="world")#传递关键字实参,多次调用函数
name1(lastname="world",firstname="hello")#等效函数调用
name1("hello","world")#等效函数调用
name2(firstname=first)#有默认值得函数调用
name2("blue")#与上式等效函数调用
age1=input("tell me your age:")
age1=int(age1)
age2=input("then your best frend's age:")
age2=int(age2)
now=age(age1,age2)
print(now)
oldname=effects(firstname1[:],lastname1[:])#返回字典,[:]禁止函数修改列表
print(oldname)
print(firstname1)
print(lastname1)oldname=effects(firstname1,lastname1)#返回字典
print(oldname)
print(firstname1)#函数中可修改列表print(lastname1)
name4('helen','white',sex='female',age='18')
print("profile: ")
print(profile)
active=True
while active:zhi=input("if you want to add one name,please don't enter'quit'to finsh the program,and tell me:")if(zhi=='quit'):active=Falseelse:name3(zhi)numm=3putto=list(range(1,numm))name3(putto)print(newname)numm+=1

实践结果:

tell me your name:xiaoming
hello! xiaoming !
tell me your first name:xiao
tell me your last name:ming
hi, Xiao Ming !
hi, Hello World !
hi, Hello World !
hi, Hello World !
ming :)
blue :)
tell me your age:16
then your best frend's age:19
3
{'firstname': ['li', 'he', 'wang', 'liu'], 'lastname': ['bai', 'qing', 'hong', 'lan']}
['li', 'he', 'wang']
['bai', 'qing', 'hong']
{'firstname': ['li', 'he', 'wang', 'liu'], 'lastname': ['bai', 'qing', 'hong', 'lan']}
['li', 'he', 'wang', 'liu']
['bai', 'qing', 'hong', 'lan']
profile:
{'firstname': 'helen', 'lastname': 'white', 'sex': 'female', 'age': '18'}
if you want to add one name,please don't enter'quit'to finsh the program,and tell me:

python入门部分基础知识(下)相关推荐

  1. Python入门之基础知识(三)

    文章目录 前言 一.序列是什么? 二.列表的概念 三.列表的创建 1.基本语法[]创建 2.list()创建 3.range()创建 4.推导式创建 四.列表元素的增加或删除 1.通过Append() ...

  2. 初识 Python 必看基础知识~ 熬夜爆肝

    本文主要梳理一些 Python 入门的基础知识,分享给每一位走在IT路上的侠客~ 全文知识梳理来源于<Python编程 从入门到实践(第2版)> 声明:未打广告,豆瓣评分9.3,刷博客时见 ...

  3. python基础一入门必备知识-Python数据分析入门必备基础知识

    今天,老师要带大家解数据分析的定义.核心思路.应用领域以及开发流程,向大家全方位展示数据分析入门必备基础知识,全都是干货哦!虽然看完本文,不能让大家立马变身为一名数据分析师,但是能让大家对数据分析有一 ...

  4. 学python需要什么基础知识-没学过Python先要学习哪些基础知识?

    零基础学Python应该学习哪些入门知识 关于零基础怎么样能快速学好Python的问题,百度提问和解答的都很多,你可以百度下看看.我觉得从个人自学的角度出发,应从以下几个方面来理解: 1 为什么选择学 ...

  5. python基础知识资料-学习Python列表的基础知识汇总

    千里之行,始于足下.要练成一双洞悉一切的眼睛,还是得先把基本功扎扎实实地学好.今天,本喵带大家仔细温习一下Python的列表.温故而知新,不亦说乎. 当然,温习的同时也要发散思考,因为有些看似无关紧要 ...

  6. python十大必备知识_学Python必备的基础知识

    学Python必备的基础知识 1.基本概念 表达式:就是一个类似于数学公式的东西,一般仅仅用了计算一些结果 ,不会对程序产生实质性的影响,如9+3; 语句:在程序中语句一般需要完成某种功能,比如打印信 ...

  7. python语言的基础知识_pythone语言基础知识汇总

    python语法的基础知识 相关推荐:<python视频> 数据类型 常用的数据类型:数字(number),字符串(string),list(数组),tuple(元组),dict(字典) ...

  8. Python入门——语言基础

    Python入门--语言基础 文章目录 Python入门--语言基础 一.标准输入和输出 二.变量和常量 三.运算符 四.选择结构 六.注释 七.逻辑行 一.标准输入和输出 1.标准输入(注意:inp ...

  9. python二级公共基础知识

    python二级公共基础知识 一.算法和数据结构 算法及其基本特征: 算法是对解题方法的准确而完整的描述. 算法的四个基本特征:可行性,确定性,有穷性,拥有足够的情报.  算法的复杂度: 算法的时间复 ...

  10. GPS 入门 1 —— 基础知识[转]

    GPS 入门 1 -- 基础知识 [转] (2008-10-11 18:14:57) <script> var $tag='gps,杂谈'; var $tag_code='b7179ced ...

最新文章

  1. Struts2 学习笔记 — 第一个struts2项目
  2. python getattr_python __getattr__
  3. python 测试用例的无输入_如何为无参数方法自动生成测试用例?
  4. 为什么全天坐在电脑前会让你精疲力竭
  5. 论文浅尝 - AAAI2020 | 基于知识图谱进行对话目标规划的开放域对话生成技术
  6. 161011、oracle批量插入数据
  7. Kong 1.0 GA 版本正式发布,微服务 API 网关
  8. 我所理解的JVM(三):字节码的执行
  9. kettle的变量空间接口VariableSpace实现与委托模式
  10. vue key重复_12道vue高频原理面试题,你能答出几道?
  11. OpenCV人工智能图像处理学习笔记 第6章 计算机视觉加强之机器学习上
  12. 谷歌chrome xp_什么是Google Chrome?
  13. VC中GetLastError() 错误代码的含义
  14. 简单的java华氏转摄氏温度
  15. 商户/服务商微信支付开发文档【 直连模式/服务商模式】如何在公众号、小程序中接入微信支付?
  16. 【人工智能】Google I/O 2023:让 AI 对每个人都更有帮助 Making AI more helpful for everyone
  17. 微分方程数值解法(实际应用)
  18. 质因数知识以及相关代码(C语言)
  19. 西蒙的管理决策理论—《可以量化的…
  20. java根据出生日期自动计算年龄(工具类)

热门文章

  1. 谈一谈凑单页的那些优雅设计
  2. 1000+常用Python库大全,太实用了!
  3. 排序算法7——图解快速排序(两种主元选择方法)以及CUTOFF时间测试
  4. 64K方法数限制原理与解决方案总结
  5. 查看HDS VSP高端存储的映射信息
  6. 注册表(安全 活动桌面)
  7. 神经网络拟合函数表达式,神经网络拟合函数matlab
  8. 数据预处理Part5——样本分布不均衡
  9. 深空摄影系列教程(昴星团摄星队)笔记
  10. Linux增加一块scsi硬盘,Linux下添加第二块scsi硬盘