python 教程笔记day1

2017-11-13  本文已影响16人  HenryTien

day1

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
17 % 2
1
5 83 + 2
  File "<input>", line 1
    5 83 + 2
       ^
SyntaxError: invalid syntax
5 +3 + 2
10
5*3 + 2
17
5**2
25
b
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'b' is not defined
4 * 3.75 -1
14.0
tax = 12.5/100
price = 100.50
prince*tax
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'prince' is not defined
price*tax
12.5625
prince + _
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'prince' is not defined
price + _
113.0625
round(_,2)
113.06
'spam eggs' #single quotes
'spam eggs'
'doenst'
'doenst'
'doest\'t' #use \' escape the single quote...
"doest't"
'"yes," he said.'
'"yes," he said.'
"\"Yes,\"he said."
'"Yes,"he said.'
'"Isn\'t," she said.'
'"Isn\'t," she said.'
'"Isn\'t," she said.'
'"Isn\'t," she said.'
print('"Isn\'t," she said.')
"Isn't," she said.
s = 'First Line.\nSecond line.' # \n means newline
s # without print() ,\n is included in the output
'First Line.\nSecond line.'
print(s) # with print(), \n produces a new line
First Line.
Second line.

https://docs.python.org/3/tutorial/introduction.html

休息一会啦

print('C:\some\name') # here \n means newline!
C:\some
ame
print(r'C:\some\name') #note the r before the quote
C:\some\name
print("""\
""")
print("""\
Usage: thingy[OPTIONS]
-h Display this usage message
_H hostname Hostname to connect to
""")
Usage: thingy[OPTIONS]
-h Display this usage message
_H hostname Hostname to connect to
# 3 times 'un, followed by 'ium'
3*'un' + 'ium'
'unununium'
'Py' 'thon'
'Python'
prefix ='Py'
prefix 'thon' # can't concatenate a variable and a string literal
  File "<input>", line 1
    prefix 'thon' # can't concatenate a variable and a string literal
                ^
SyntaxError: invalid syntax
prefix + 'thon'
'Python'
text = ('Put several strings with parenthesses')
text = ('Put several strings with parenthesses' """""")
text
'Put several strings with parenthesses'
text = ('Put several strings with parenthesses'                  ) 
word = 'python'
word[0] # character in position
'p'
word[5] # character in position 5
'n'
word[-1]
'n'
word[-2]
'o'
word[0:2]
'py'
word[:4] + word[4:]
'python'

3.1.3. Lists

square + [ 36, 49 ,64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
cubes = [1, 8,27, 65, 125] # something's wrong here
4 **3
64
cubes[3] = 64 # replace the wrong value
cubes
[1, 8, 27, 64, 125]
cubes.append(218)
cubes.append(7**3) # and the cube of 7
cubes
[1, 8, 27, 64, 125, 218, 343]
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
# replace some values
letters[2:5] = ['C', 'D', 'E']
letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
# now remove then
letters[2:5] = []
letters
['a', 'b', 'f', 'g']
# clear the list by replacing all the elements with an empty list
letters[:] =[]
letters
[]

a = ['a', 'v', 'c']
n = ['1', '2', '3']
x = [a,n]
x
[['a', 'v', 'c'], ['1', '2', '3']]
x[0]
['a', 'v', 'c']
x[1]
['1', '2', '3']
len(x)
2
x[0][1]
'v'

3.2. First Steps Towards Programming

上一篇 下一篇

猜你喜欢

热点阅读