python学习笔记1-基础语法
1 在3版本中print需要加上括号
2 多行语句:用\连接
1 item_one=1 2 item_two=2 3 item_three=3 4 total = item_one + \ 5 item_two + \ 6 item_three 7 print (total)
3 引号
字符串通常在引号中 不管是单引号 双引号还是三引号
必须保证前后一致
1 #引号 2 word = 'word' 3 sentence = "这是一个句子。" 4 paragraph = """这是一个段落。 5 包含了多个语句""" 6 print (word) 7 print (sentence) 8 print (paragraph)
4注释:
(1)#开头 也可以在结尾用#进行注释
(2)多行注释 用三引号括起来
1 #注释 2 # 第一个注释 3 print ("Hello, Python!"); # 第二个注释 4 name = "Madisetti" # 这是一个注释 5 6 ''' 7 这是多行注释,使用单引号。 8 这是多行注释,使用单引号。 9 这是多行注释,使用单引号。 10 ''' 11 12 """ 13 这是多行注释,使用双引号。 14 这是多行注释,使用双引号。 15 这是多行注释,使用双引号。 16 """
5码组:
1 #码组 2 ''' 3 if expression : 4 suite 5 elif expression : 6 suite 7 else : 8 suite 9 '''
6帮助
1 help(sys.stdout.write)
7变量赋值
1 #变量赋值 2 counter = 100 # 赋值整型变量 3 miles = 1000.0 # 浮点型 4 name = "John" # 字符串 5 6 print (counter) 7 print (miles) 8 print (name) 9 10 a = b = c = 1 11 print (a,b,c) 12 a, b, c = 1, 2, "john" 13 print (a,b,c) 14 15 #数字 16 var1 = 1 17 var2 = 10 18 19 #del var1[,var2[,var3[....,varN]]]] 20 var=5896419821 21 var_a=0.22 22 var_b=3e2 23 del var 24 del var_a, var_b
8数据类型
Numbers
(1)不可以改变的数据类型
(2)用于存储数值
(3)int long float complex
string
(1)str*2代表输出2次
(2)可以用+进行字符串的连接
1 #字符串 2 #s="a1a2•••an"(n>=0) 3 s = 'ilovepython' 4 print (s[1:5])#从0开始 5 print (s[5:-1])#去不到末尾的n 6 7 str = 'Hello World!' 8 print (str) # 输出完整字符串 9 print (str[0]) # 输出字符串中的第一个字符 10 print (str[2:5]) # 输出字符串中第三个至第五个之间的字符串 11 print (str[2:]) # 输出从第三个字符开始的字符串 12 print (str * 2) # 输出字符串两次 13 print (str + "TEST") # 输出连接的字符串
List
(1)用中括号[]创建 用,分开
(2)*和+的用法和string类似
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] tinylist = [123, 'john'] print (list) # 输出完整列表 print (list[0]) # 输出列表的第一个元素 print (list[1:3]) # 输出第二个至第三个的元素 print (list[2:]) # 输出从第三个开始至列表末尾的所有元素 print (tinylist * 2) # 输出列表两次 print (list + tinylist) # 打印组合的列表
Tuple
(1)用小括号()
(2)元素是固定的 相当于可读
(3)用法和上面的string list类似
(4)元祖中不能直接赋值给某个元素 tuple[2]=1000//wrong
1 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) 2 tinytuple = (123, 'john') 3 4 print (tuple) # 输出完整元组 5 print (tuple[0]) # 输出元组的第一个元素 6 print (tuple[1:3]) # 输出第二个至第三个的元素 7 print (tuple[2:]) # 输出从第三个开始至列表末尾的所有元素 8 print (tinytuple * 2) # 输出元组两次 9 print (tuple + tinytuple) # 打印组合的元组 10 11 tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) 12 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] 13 tuple[2] = 1000 # 元组中是非法应用 14 list[2] = 1000 # 列表中是合法应用
Dictionary
(1)用{},无序集合
(2)采用键值的方式 例如'name':'join'
(3)可以输出所有的键和值
1 #元字典 2 dict = {} 3 dict['one'] = "This is one" 4 dict[2] = "This is two" 5 6 tinydict = {'name': 'john','code':6734, 'dept': 'sales'} 7 8 9 print (dict['one']) # 输出键为'one' 的值 10 print (dict[2]) # 输出键为 2 的值 11 print (tinydict) # 输出完整的字典 12 print (tinydict.keys()) # 输出所有键 13 print (tinydict.values()) # 输出所有值
9运算符
9.1算数运算符
(1)幂 **
(2)取整除 //
1 a = 21 2 b = 10 3 c = 0 4 5 c = a + b 6 print ("Line 1 - Value of c is ", c) 7 8 c = a - b 9 print ("Line 2 - Value of c is ", c) 10 11 c = a * b 12 print ("Line 3 - Value of c is ", c) 13 14 c = a / b 15 print ("Line 4 - Value of c is ", c ) 16 17 c = a % b 18 print ("Line 5 - Value of c is ", c) 19 20 a = 2 21 b = 3 22 c = a**b 23 print ("Line 6 - Value of c is ", c) 24 25 a = 10 26 b = 5 27 c = a//b 28 print ("Line 7 - Value of c is ", c)
9.2比较运算符
(1)== 比较对象是否相等
9.3位运算符
(1)&:位与:两个相应位都为1 结果为1
(2)|:位或:其中一个为1则为1
(2)^:位异或:相异的时候为1
(4)~:取反:0-》1 1——》0
(5)<<:左移:
(6)>>:右移
1 #位运算符 2 a = 60 # 60 = 0011 1100 3 b = 13 # 13 = 0000 1101 4 c = 0 5 6 c = a & b; # 12 = 0000 1100 7 print "Line 1 - Value of c is ", c 8 9 c = a | b; # 61 = 0011 1101 10 print "Line 2 - Value of c is ", c 11 12 c = a ^ b; # 49 = 0011 0001 13 print "Line 3 - Value of c is ", c 14 15 c = ~a; # -61 = 1100 0011 16 print "Line 4 - Value of c is ", c 17 18 c = a << 2; # 240 = 1111 0000 19 print "Line 5 - Value of c is ", c 20 21 c = a >> 2; # 15 = 0000 1111 22 print "Line 6 - Value of c is ", c
9.4成员运算符
(1)in:在指定序列中找到值返回true
(2)not in
1 a = 10 2 b = 20 3 list = [1, 2, 3, 4, 5 ]; 4 5 if ( a in list ): 6 print ("Line 1 - a is available in the given list") 7 else: 8 print ("Line 1 - a is not available in the given list") 9 10 if ( b not in list ): 11 print ("Line 2 - b is not available in the given list") 12 else: 13 print ("Line 2 - b is available in the given list") 14 15 a = 2 16 if ( a in list ): 17 print ("Line 3 - a is available in the given list") 18 else: 19 print ("Line 3 - a is not available in the given lis)t"
9.5身份运算符
(1)is:判断是不是引用的同一个对象
(2)is not:判断两个标识符是不是引用不同对象
1 #身份运算符 2 a = 20 3 b = 20 4 5 if ( a is b ): 6 print ("Line 1 - a and b have same identity") 7 else: 8 print ("Line 1 - a and b do not have same identity") 9 10 if ( id(a) == id(b) ): 11 print ("Line 2 - a and b have same identity") 12 else: 13 print ("Line 2 - a and b do not have same identity") 14 15 b = 30 16 if ( a is b ): 17 print ("Line 3 - a and b have same identity") 18 else: 19 print ("Line 3 - a and b do not have same identity") 20 21 if ( a is not b ): 22 print ("Line 4 - a and b do not have same identity") 23 else: 24 print ("Line 4 - a and b have same identity")