卧槽卧槽时不我待啊
一晃这么多天过去了太可怕了
=====================================
section: Basics
print('hello world') # string: '' or "", immutable # multi-line string: ''' or """ # escape sequences (\) print("what's your name") print("what\'s your name") print("what\\s your name") print('first line \nsecond line') print('abc \tdef') print("first sentence.\ second sentence.") print('\n') # index first position at 0 # string concatenation: + age = 20 name = 'Swaroop' print('{0} was {1} years old when he wrote this book'.format(name,age)) print('{} is {} years old'.format(name, age)) print(name + ' is ' + str(age) + ' years old') # decimal (.) precision of 3 for float '0.333' print('{0:0.3f}'.format(1.0/3)) print('{:0.3f}'.format(1.0/3)) # fill with underscore (_) with the text centered # (^) to 11 width '___hello___' print('{0:_^11}'.format('hello')) print('{:_^11}'.format('hello')) print('{0:-^11}'.format('hello')) print('{0:_^14}'.format('hello')) # keyword-based print('{name} wrote {book}'.format(name='swaroop', book="a byte of python")) # print ends with invisible new line character (\n) print('a',end='') print('b',end='') print('c',end=' ') print('d\ne') #raw string: prefix r or R print("newlines are indicated by \n") print(r"newlines are indicated by \n") print('\n') # identifier naming ''' 1. start with alphabet or underscore _ 2. the rest can consist of letters, _ or digits(0-9) 3. case-sensitive ''' # example: using variables and literal constants i=5 print(i) i=i+1 print(i) s='''this is a multiline string. this is second line''' print(s) # end of logical statement ; # However, it is recommended that you never use ;, and stick to writing a max of single logical line on each single physical line i=5 print(i) i=5;print(i) s = 'This is a string. \ This continues the string.' print(s) i=\ 6 print(i) #indentation: ''' using 4 spaces is recommended must use a consistent number of spaces for indentation '''
section: Operators and Expressions
# operators: ''' +: add or concatenation -: subtraction or negative *: multiply or repeat strings, 'la'*3 gives lalala **: power/exponentiation, 3**4 gives 3^4 /: divide //: divide and floor %: modulo, returns the remainder all comparison operators return True or False comparison can be chained: 3<6<8 gives True ==: equal !=: not equal not, and, or: boolean shortcut: a = 2; a = a*3 can be written as a = 2;a *= 3 ''' l = 5 w = 2 area = l * w print('area is', area) print('perimeter is', 2*(l+w))