当行注视:# 被注释内容
多行注释:""" 被注释内容 """
1 # Author :Mr Hui 2 number = 5
上述代码声明了一个变量,变量名为: number,变量number的值为:5
1 name = ‘A‘ 2 name2 = name 3 name = ‘B‘ 4 print("name2 = " ,name2,"name = ",name ) //运行结果:name2 = A name = B
1 # Author :Mr Hui 2 import keyword 3 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‘]
abs()、divmod()、input()、open()、等等
python内置了一系列的常用函数,以便于我们使用,python英文官方文档详细说明:点击查看
内置函数、内置方法都不能作为变量名使用。
print(value,...., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False )
-1.sep:设置输出字符产之间的字符串。默认是空格
-2.end:设置输出文本末尾的字符串。默认是‘\n‘
-3.file:指定文本将要发送到的文件、标准流或其他类似文件的对象,默认是sys.stdout
-4.flush:用于控制输出缓存,该参数一般保持为False即可,这样可以获得较好的性能。
1 # Author :Mr Hui 2 name=‘Tom‘ 3 age=‘12‘ 4 job=‘student‘ 5 print(name,age,job,sep=‘-‘,end=" ",flush=False,file=open(‘d:/example.txt‘,‘w‘))
输出结果:D盘 example.txt文件 内容:Tom-12-student
整型数据就是整数,可以有正负号。
整型数据有4中表示方法:十进制,二进制,八进制和十六进制。
二进制:以0b或0B开头,后接数字0和1.
八进制:以0o或0O开头,后接数字0~7.
十六进制:以0x或0X开头,后接数字0~9,和字母A~F(或a~f).
为了提高数值可读性,允许为数值增加下画线作为分隔符。(浮点型)
#!/usr/bin/env python # -*- coding:utf-8 -*- #整形的使用 #将字符串转换为数字 a = "999" print(a,type(a)) b = int(a) print(b,type(b)) b += 1 print(b,type(b)) #进制间的转换 #bin() 十进制转二进制 a102 = bin(111) print(a102) #oct() 十进制转八进制 a108 = oct(111) print(a108) #hex() 十进制转十六进制 a1016 = hex(111) print(a1016) # int.bit_length() 用二进制表示这个数用多少位 1 10 11 100 101 (1 2 3 4 5) a01 = 4 b_length = a01.bit_length() print(b_length)
a = 1_000_000
print(a)实际值为1000000
结果:
999 <class ‘str‘>
999 <class ‘int‘>
1000 <class ‘int‘>
0b1101111
0o157
0x6f
3
a = None
# Author :Mr Hui #双引号包含单引号 str3 = "I‘m a coder" print(str3) #单引号包含双引号 str2 =‘"Spring is here, let us jam!",said woodchuck.‘ print(str2) #使用反斜杠(\)进行字符转义 str1 = ‘"we are scared,Let\‘s hide in the shade",says the bird‘ print(str1) 运行结果:
I‘m a coder
"Spring is here, let us jam!",said woodchuck.
"we are scared,Let‘s hide in the shade",says the bird
s2 = ‘Python ‘ s1 =‘is Funny‘ #使用+拼接字符串 s3 = s2+s1 print(s3)
运行结果:Python is Funny
数值与字符串拼接的方式:两种 str() 和 repr()
s = "我的萝卜 " p =55.6 # print(s+p) 程序报错 print(1,s+str(p)) print(2,s+repr(p)) print(3,repr(s))#以Python表达式的形式表示值 print(4,str(s))
运行结果:
1 我的萝卜 55.6
2 我的萝卜 55.6
3 ‘我的萝卜 ‘
4 我的萝卜
使用三个引号包括起来的长字符串完全可以赋值给变量。
ss = ‘‘‘ "Let‘s go fishing",said Mary. "OK,Let‘s go ",said her brother. ‘‘‘ print(ss)
kk = 20+4/4+ \
2*3
print(kk)
zz = "The quick brown fox \
jumps over the lazy dog"
print(zz)
、
运行结果:
"Let‘s go fishing",said Mary.
"OK,Let‘s go ",said her brother.
27.0
The quick brown fox jumps over the lazy dog
原始字符串以"r"开头,原始字符串不会把反斜线当成特殊字符
qq = ‘r D:\code\dest\c\demo.txt‘ print(qq)
用于常量或变量指定值,Python使用"="作为赋值运算符 a=b=c=d=22
+ | 加法运算、字符串拼接 |
- | 减法运算、负数运算 |
* | 乘法运算、字符串连接运算符 |
/ | 除法运算 |
% | 求余运算符 |
// | 整除运算符 |
** | 乘方运算符 |
# Author :Mr Hui # 单目运算符“+”,则不对操作数做任何改变 y = -0.5 y = +y print(y) # // 整除:只留整数,小数舍去 print("19//4 = ",19//4) # ** print("2**3次方 = ",2**3)
结果:
-0.5
19//4 = 4
2的3次方 = 8
#!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 #相同为0,不同为1 print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011 print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000 print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111 print "Line 6 - Value of c is ", c
*按位取反运算规则(按位取反再加1) 详解http://blog.csdn.net/wenxinwukui234/article/details/42119265
索引运算符 " [ ] " ,方括号中既可以使用单个索引值,也可以使用索引范围,并使用范围时,可指定步长。
ss = ‘abcdefghijklmnopq‘ #获取索引2到9的子串,步长为3 print(ss[2:9:3])
结果:cfi
原文:https://www.cnblogs.com/not-miss/p/10999655.html