python basic grammar
1.Constant and Variable
1)Constant
eg.
import const
const.value=5
print const.value
const.value=6
2)Variable
eg.
a=1
2.Number and string
1)Single quotation mark
eg. a='string'
2)Double quotation marks
eg. b="string"
3)Three single quotation marks (output special characters)
eg. c='''string\nbbb'''
4)Three double quotation marks (the output of special characters)
eg. d="""string\nccc"""
eg. c1="jikexueyuan"
c2=c1[0]
c3=c1[7]
c4=c1[:2]
c5=c1[2:]
c6=c1[4:7]
print c6
3.data type
1)List
eg. students=["john","jack","lynn","susan"]
print students[3]
2)Tuple
eg. students=("john","jack","lynn","susan")
3)set
eg. a=set("abcaaaabbccddaasd")
b=set("cdfm")
#Intersection
x=a&b
#Union
y=a|b
#Difference set
z=a-b
#Remove duplicate elements
new=set(a)
4)Dictionary
eg. dic={"Name":"john","Age":18}
print dic["Age"]
dic["Age"]=38
print dic["Age"]
4.Line and indentation
1)Logical row and physical row
eg. #The following are 3 physical lines
print "abc"
print "789"
print "ccc"
#The following are 1 physical lines, 3 logical lines
print "abc";print "345";print "ccc"
#The following are 1 logical lines, 3 physical line
print ''' abc
cdef
1234'''
2)Use a semicolon rule
eg. #A logical line after all should use a semicolon, but except for the following conditions
print "123";print "cdg";
print "888";
#A semicolon can be omitted conditions:
At the end of each physical row can omit the semicolon, certainly can not omit the semicolon.
print "123"; print "456" #Here the semicolon can be omitted, also can not be omitted
print "777" #Here the semicolon can be omitted, also can not be omitted
3)line connect(\)
eg. print "abc\
123"
4)How to indentation
eg. #In general, the line should not leave blank
import sys
#Indentation method has two kinds, can press the space, also can press the tab key
#if Method for the indentation of a statement
a=7
if a>0:
print "hello"
#while Method for the indentation of a statement
a=0
while a<7:
print a
a+=1