《python小白入门系列教程》专栏·第03篇|xc_718深度好文:1828字|4分钟阅读1.注意1)单行注释:##注释内容print('123')#123print('abc')#abcprint("abc")#abc 2)多行注释:'''or"""(1)第一种评论方式 ''' code code ...'''(2)第二种评论方式""" code code ... """2.Variable变量:是将一些操作的中间结果暂时存放在内存中,以供后续代码调用使用。(1)必须是任意组合由数字、字母、下划线组成,不能以数字开头。(2)python中不能是关键字['and','as','assert','break','class','continue','def','del','elif','else','except','exec','finally','for','from','global','if','import','in','is','lambda','not','or','pass','print','raise','return','try','while','with','yield'](3)变量是描述性的。(4)不能是中文。3.Constant常量:常量。例如:π常数一般用大写表示:BIR_OF_CHINA=19494。逻辑运算符(1)或或|(2)andand&(3)notnot^5。基本数据类型:(1)numbers int +,-,/,%(余数),*(幂),//(整除)(2)string str +(拼接),*(重复)(3)布尔值bool TrueFalse(4)列表 list 增删改查(5)字典 dic 增删改查(6)元祖 元组 增删改查(7)set set 加删查+可用于算术加法,也可用于列表的连接,元组和字符串(但不支持不同类型对象之间的加法或连接)*可用于列表、元组和字符串的算术乘法和复制递增操作。(字典和集合不支持和整数相乘,因为里面的元素不允许重复)/除法运算3/2=1.5//求整数商15//4=315.0//4=3.0-15//4=-4(向下取整)%求余数**运算符表示幂乘x_list=[1,2,3]#创建列表对象x_tuple=(1,2,3)#创建元组对象x_dict={'a':97,'b':98,'c':99}#创建字典对象x_set={1,2,3}#创建集合对象print(x_list[1])#使用下标访问指定位置的元素2print(x_tuple[1])#元组也支持使用序号作为下标2print(x_dict['a'])#下标字典对象的是x_set中的"key"973#MembertestTrue转换:字符到数字str--->int:int(str)mustbeanumbers2=2i2=int(s2)print(i2,type(i2))#2charactertoBooleanstr--->bool:bool(str)如果str为空,则转为False;如果str不为空,则转换为Trues7=''s8='non-empty'b7=bool(s7)b8=bool(s8)print(b7,type(b7))#str为空,则converttoFalseprint(b8,type(b8))#str不为空,然后转为Truenumbertostringint--->str:str(int)i1=1s1=str(i1)print(s1,type(s1))#1NumbertoBooleanint--->bool:bool(int)非零Number转换为True,0转换为Falsei3=3i4=0b3=bool(i3)b4=bool(i4)print(b3,type(b3))#Trueprint(b4,type(b4))#False布尔值到数字bool--->int:int(bool)True转换为1,False转换为0b5=Trueb6=Falsei5=int(b5)i6=int(b6)print(i5,type(i5))#1print(i6,type(i6))#0Booleantostringbool--->str:str(bool)True,Falseb1=Trueb2=Falses1=str(b1)s2=str(b2)print(s1,type(s1))#Trueprint(s2,type(s2))#False6.用户交互:input1)等待输入,2)将你输入的内容赋值给上一个变量3)输入数据类型均为str7。格式化输出:output_format格式化输出%s%d%%% 占位符s strstringd digitnumbern%% 表示数字n%name=input('请输入你的名字:')age=input('请输入您的年龄:')sex=input('请输入您的性别:')job=input('请输入您的工作:')#为了与占位符%区别,当number是5%,需要写成5%%info="我叫%s,今年%d岁,我是%s的学生,工作是:%s,我有已完成85%的大学学习%"%(name,int(age),sex,job)msg='''----------------%s的信息---------------姓名:%sAge:%dSex:%sJob:%s--------------------结束-------------------'''%(name,name,int(age),sex,job)print(msg)print(info) 运行结果如下:8.缩进冒号表示缩进的开始,即代码块的开始。缩进结束表示代码块的结束。同级代码块的缩进必须相同。一般使用4个空格作为基本缩进单位age=20ifage>=18:print('Adult')elifage>=6:print('Youth')else:print('Children')citys=["Zhengzhou","Shanghai","Beijing","Guangzhou"]forcincities:print(c)9.运行Python文件 python(空格)文件路径回车 hello.py文件需要在对应的文件夹下面是d盘的在??线练习:https://www.520mg.com/it