2023.2.21 Python学习记录 |
您所在的位置:网站首页 › python怎样输出逗号 › 2023.2.21 Python学习记录 |
2023.2.21 部分数据类型以及转义布尔运算学习 python在线代码测试地址 : Python tester - Test code online 1、print()函数可接受多个字符串,用英文逗号隔开,(遇到逗号会输出空格)需要输入的字符串用''括起来; print('The quick brown fox','jumps over','the lazy dog')执行结果The quick brown fox jumps over the lazy dog 2、print()可以打印整数以及计算结果; print(300) 执行结果300 print(100+200) 执行结果300 print('100+200','=',100+300) 执行结果100+200 = 300 print('100 + 200 =',100+200) 执行结果100 + 200 = 300 说明:‘’字符串内的空格也直接打印 3、input()输入函数,用户输入字符串需要存放到一个变量中 name=input() 说明:将输入内容存入变量name中 print(name) 说明:打印变量直接在括号写变量名,不需要‘’ 实际操作结果:直接输入name变量名不可展示变量值; 4、同时字符串和变量用空格隔开: name=input() print('hello',name) 5、python变量命名规则 有字母、数字、英文下划线_ 不能以数字开头 不能包含空格 不能是python关键字,但可以包含python关键字 6、输入带提示 name=input('plese enter your name:') 说明:提示用‘’括起来 print('hello',name) 7、查询python关键字 import keyword print(keyword.kwlist) 执行结果['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] 8、简写 输入输出统称为Input/Output,或者简写为IO 9、批量注释ctrl+/ ,执行当前文件内容ctrl+shift+f10 数据类型 1、python可以直接处理的数据类型: 数值类型: 整数(包括负整数)、 浮点数(小数) 字符串类型string:(使用单引号''括起的)(如果字符串中有单引号',则需要用双引号""括起来) (如果既有'又包含",使用转义字符\来标识),在'或者"前用\表示直接直接输出'或者",示例:\'或者\" 布尔类型: 空值None类型: 此外还有其他类型: 数值类型:complex(复数) 列表类型:List 元组类型:Tuple 集合类型:Set 字典类型:Dictionary 自定义类型: 2、 转义字符:\n --换行 \t --制表符,代表四个空格,也就是一个tab;t标识table \\ --打印\ r'' --表示''中的内容不需要转义 print(r'I\'m \"OK\"','print','I\'m \"OK\"') 执行结果为I\'m \"OK\" print I'm "OK" --r''标识''中的字符串不转义 print(r'\\\t\\') 执行结果为\\\t\\ print('\\\n\\') 执行结果为\ \ 前两个\\表示转义字符\,打印结果为\,后面\n表示换行,最后\\表示转义字符\,打印结果为\ \n有多个时不易阅读,可以用'''...'''表示多行内容 print('''line1 I'm line2 line3''') 执行结果为 line1 I'm line2 line3 即三个单引号内的内容直接打印 多行字符串'''...'''前面加上r使用,表示r'''...'''中的内容直接打印 print(r'''hello,\nworld''') 打印结果为hello,\nworld 制表符打印 print('I\'m learning\tPython') 打印为I'm learning Python --\t表示4个空格,一个tab 3、布尔值 布尔值和布尔代数的表示完全一致,一个布尔值只有True和False两种值(注意首字母大写) 布尔值可以用and、or、not运算 and运算是与运算,只有所有都为True,and运算结果才是True print(True and True) 运算执行结果为True print(True and False) 运算执行结果为False print(False and False) 运算执行结果为False print(5>3 and 3>1) 运算结果为True or运算为或运算,只要其中一个为True,or运算结果就是True print(True or True) 运算执行结果为True print(True or False) 运算执行结果为True print(False or False) 运算执行结果为False print(5>3 or 1>3) 运算执行结果为True not运算为非运费,是一个单目运算符,把True变成False,把False变成True print(not True) 运算执行结果为False print(not False) 运算执行结果为True print(not 2>1) 运算执行结果为False 4、布尔值与条件判断 if else用法如下: if 条件: 条件成立执行的代码1 条件成立执行的代码2 ...... else: 条件不成立执行的代码1 条件不成立执行的代码2 ...... age =input('age:') if age >=18: print('adult') else: print('teenager') 运行提示TypeError: '>=' not supported between instances of 'str' and 'int' 意思是input中输入为字符串str,不能和18int比较 需要将input输入转换成int类型,如下 age =int(input('age:')) if age >=18: print('adult') else: print('teenager') 5、空值 空值用None表示(注意首字母大写),None不能理解为0,因为0是有意义的,而None是一个特殊的空值。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |