变量
1 2 3 4 5 6 7 8 9 | python不需要定义变量 变量是内存中的一块区域。对象赋值实际上是对象的引用。a=10 变量的命名: 变量名由字母,数字,下划线组成,不能以数字开头. (a,b, c) hello = 100, hello_world = 100, count2 =100, 1count=10(x) Python中,变量定义时不需要指定类型的,当用变量的时候,必须要给这个变量赋值; |
基本数字类型
进制
1 2 3 4 5 | 整数一般以十进制表示,但是 Python也支持八进制(0开始)或十六进制(0x开始)来表示整数。 十进制转换成二进制 bin(10) 十进制转换成八进制 oct(10) 十进制转换成十六进制 hex(10) 2). 整数的范围取决于机器是32位还是64位。但长整数不是, 取决于虚拟内存的大小. |
1 2 3 4 5 6 | >>> bin(10) '0b1010' >>> oct(10) '0o12' >>> hex(10) '0xa' |
整型
1 2 3 | int:1,2,3(无小数点) long:python2中有,python3中已经将整数和长整数整合在一起 python3中长整数用int表示 |
1 2 3 4 5 6 7 8 9 | C:\Users\LENOVO>python Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> age=100 >>> age 100 >>> type(age) < class 'int'> |
1 2 3 | >>> money=1000000000000000000000000000000000000000000000000 >>> type(money) < class 'int'> |
浮点数
1 | 3.1415926,2e |
1 2 3 4 5 6 7 8 9 | >>> float_a=3.1415 >>> type(float_a) < class 'float'> >>> float_b=2e+4 >>> type(float_b) < class 'float'> >>> float_c=2e-4 >>> type(float_c) < class 'float'> |
布尔值
1 | True,False |
1 2 3 4 5 6 7 | >>> 1< 3 True >>> 1>3 False >>> bool_a=1>3 >>> type(bool_a) < class 'bool'> |
复数
1 | 物理学 |
运算符
算术运算符
1 | +, -, *, /, **, %, // |
1 2 3 4 5 6 7 8 9 10 11 12 | >>> # 1. 算数运算符:+, -, *, /, **, %, // >>> 2**4 16 >>> ## ** 代表次方 >>> ## % 代表取余 >>> ## //代表整除 >>> 10%3 1 >>> 10/3 3.3333333333333335 >>> 10//3 3 |
赋值运算符
1 | =,+=,-=,*=,/=,%=,//= |
1 2 3 4 5 6 7 8 9 | >>> # 2.赋值运算符:=,+=,-=,*=,/=,%=,//= >>> num = 1 >>> num = num+1 >>> num 2 >>> num = 1 >>> num += 1 # num = num + 1 >>> num 2 |
关系运算符
关系运算符,返回波尔类型
要么关系成立,要么关系不成立
1 | > >= < <= != == |
1 2 3 4 5 6 7 8 | >>> # 3.关系运算符:> >= < <= != == >>> name = 'yqq' >>> name == "admin" # 判断name变量的值是否等于admin False >>> name == "yqq" # 判断name变量的值是否等于yqq True >>> name != "admin" True |
逻辑运算符
1 | and,or,not |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | >>> # 4.逻辑运算符:and,or,not >>> ## 逻辑与,全真为真,一假为假 >>> ## 逻辑或,全假为假,一真为真 >>> ## 逻辑非,真就是假,假就是真 >>> name 'yqq' >>> password = 'yqq' >>> name, password ('yqq', 'yqq') >>> name == 'yqq' and password == 'yqq' True >>> name == 'yqq' and password == 'admin' False >>> name == 'yqq' or password == 'admin' True >>> not name == 'yqq' False >>> not name == 'admin' True |
数据的输入与输出
输入
- 将输入的值存放到name的内存变量中
1 2 3 4 5 | >>> # 1.用户输入input >>> name = input() Outlook >>> name 'Outlook' |
- 显示提示输入
1 2 3 4 | >>> name = input("请输入你的用户名:") 请输入你的用户名:Outlook >>> name 'Outlook' |
- 转换数据类型
1 2 3 | int() #转换数据类型为整型 float() #转换数据类型为浮点型 bool() #转换数据类型为布尔型 |
如果不将数值转换成整型,则不能和整型数值进行比较
1 2 3 4 5 6 | >>> num = input("数值:") 数值:21 >>> num > 18 Traceback (most recent call last): File "< stdin >", line 1, in < module > TypeError: '>' not supported between instances of 'str' and 'int' |
转换为整型
1 2 3 4 5 6 | >>> num = int(input("数值:")) 数值:21 >>> type(num) < class 'int'> >>> num > 18 True |
- 0101的int型
1 2 3 4 5 6 7 8 9 10 | >>> num = 21 >>> type(num) < class 'int'> >>> binary_num = bin(num) #二进制 >>> type(binary_num) < class 'str'> >>> binary_num '0b10101' >>> num 21 |
输出
- 格式化打印字符串
1 2 3 4 5 | #占位符 %s #字符串 %d #整型数 %f #浮点数 %.2f #保留小数点后两位的浮点数 |
简单打印
1 2 3 4 | >>> name = "Outlook" >>> age = 21 >>> print(name,age) Outlook 21 |
使用占位符%s
%d
打印
1 2 | >>> print("姓名:%s,年龄:%d" %(name,age)) 姓名:Outlook,年龄:21 |
使用占位符%f
打印
1 2 3 | >>> score = 99.9999994 >>> print("姓名:%s,年龄:%d,分数:%f" %(name,age,score)) 姓名:Outlook,年龄:21,分数:99.999999 |
使用占位符%.2f
打印
1 2 3 | >>> score = 99.9999994 >>> print("姓名:%s,年龄:%d,分数:%.4f" %(name,age,score)) 姓名:Outlook,年龄:21,分数:100.0000 |
- format格式化打印字符串
1 2 | >>> print(f"姓名:{name},年龄:{age},分数:{score}") 姓名:Outlook,年龄:21,分数:99.9999994 |
测试
Windows搜索IDLE(在没有pycharm下,python自带的编译器)
Linux(直接vim)
代码如下:
1 2 3 4 5 6 | name = input("name:") Chinese = int(input("Chinese:")) Math = int(input("Math:")) English = int(input("English:")) print("all_score:%d"%(Chinese + Math + English)) print("average:%d"%((Chinese + Math + English)/3)) |
运行后的结果:
1 2 3 4 5 6 7 | ==================== RESTART: E:/python/python1-学生平均成绩统计.py ==================== name:Outlook Chinese:98 Math:99 English:97 all_score:294 average:98 |
常用内置函数
(了解)
1 2 3 | abs #求绝对值 >>> abs(-1) 1 |
1 2 3 | pow #求次方 >>> pow(2,2) 4 |
1 2 3 | round #保留小数点后几位 >>> round(3.1415926,3) 3.142 |
1 2 3 | divmod #返回商和余数 >>> divmod(10,3) (3, 1) |
标签: python
版权声明:本站所有图片/内容除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除,谢谢!