-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumeric.py
More file actions
109 lines (80 loc) · 2.71 KB
/
Copy pathNumeric.py
File metadata and controls
109 lines (80 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
数值类型
'''
__author__ = 'tzl'
# python 基本数据类型提供了整数,浮点数,字符串
# 注:1.整数精确,浮点数会有误差
# 注:2.r''或者r""表示后面字符串中的转义字符全部失效
# 注:4. %为 % 的转义字符,字符串'%%' 为 %
# 注:int() str() unicode() 坐字符转换
# None 和 '' 类似于 null 和空字符串
# 引号中的引号解决
# 用单引号包裹
# 转移字符
# python 提供了两种 内置集合类型 list 和 tuple
# 注:1.list 用 [] 表示,且可以添加和删除元素
# 2.tuple 用 () 表示,不可以添加或删除元素,或者说每个元素的内存地址不可改变
# 3.索引正序从 0 开始,逆序从 -1 开始
# python 中内置了对字典 dict 和集合 set 的支持,其中dict 类似于 java 中的Map
# 注:1.增 2.删 pop(key) 对应的键值也消失 3.改 键值不可改变 4.查 1>dict[key] 2>get(key) 3> in
# ex1 :打印 I'm "OK"!
print("I\'m \"OK\" ")
print(r'I'"m '"'OK" ')
# print("I'm ... "OK")
print(r'''hello,\n world''')
# 格式化
print('%2d-%02d' % (3, 1))
print('%.2f' % 3.1415926)
# ex2:计算成绩提升百分点
r = (85 - 72) / 72
print('%.1f %%' % r)
##########
# str:字符串为不可变
##########
a = 'abc'
b = a.replace("a", "A")
print(a, b)
# ex3:list和tuple索引
L = [
['Apple', 'Google', 'Microsoft'],
['Java', 'Python', 'Ruby', 'PHP'],
['Adam', 'Bart', 'Lisa']
]
# 打印Apple:
print(L[0][0])
# 打印Python:
print(L[1][1])
# 打印Lisa:
print(L[-1][-1])
import math
def quadratic(a, b, c):
# if not isinstance(a,(int,float)):
# raise TypeError("bad type")
delta = b * b - 4 * a * c
if delta >= 0:
return ((-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)), ((-b - math.sqrt(b * b - 4 * a * c)) / (2 * a))
else:
return -1, -1
print('hahah' + str(1 + 2))
headlines = ["Local Bear Eaten by Man",
"Legislature Announces New Laws",
"Peasant Discovers Violence Inherent in System",
"Cat Rescues Fireman Stuck in Tree",
"Brave Knight Runs Away",
"Papperbok Review: Totally Triffic"]
# TODO: set news_ticker to a string that contains no more than 140 characters long.
# HINT: modify the headlines list to verify your loop works with different inputs
def getStr(headlines):
count = 0
result = ''
for i in range(len(headlines)):
if count + len(headlines[i]) > 140:
# 截取字符串到 140
result = result + headlines[i][:140 - count]
return result
result = result + headlines[i] + ' '
count += len(headlines[i]) + 1
return result
news_ticker = getStr(headlines)