sum = 0
for x in range(101):
sum =sum + x
print(sum)
sum = 0
n = 99
while n >0 :
sum = sum + n
print(sum)
n = n -2
print(sum)
99
196
291
384
475
564
651
736
819
900
979
1056
1131
1204
1275
1344
1411
1476
1539
1600
1659
1716
1771
1824
1875
1924
1971
2016
2059
2100
2139
2176
2211
2244
2275
2304
2331
2356
2379
2400
2419
2436
2451
2464
2475
2484
2491
2496
2499
2500
2500
d = {"Micheal":95,"Bob":75,"Tracy":85}
d["Adam"] = 67
d.pop("Bob")
print("Thomas" in d)
print(d.get("Thomas"))
print(d["Micheal"])
print(d)
False
None
95
{'Micheal': 95, 'Tracy': 85, 'Adam': 67}
# 进行开平方运算
import math
math.sqrt(18)
s=10
print(10//3)
thesum = thesum + 1
thesum
thesum = True
thesum
mylist= [1,3,True,6.5]
mylist[1:3]
mylist=[1,2,3,4]
A= [mylist]*3
print(A)
mylist[2] = 45
print(A)
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 45, 4], [1, 2, 45, 4], [1, 2, 45, 4]]
mylist = [1024,3,True,6.5]
mylist.append(True)# 插入在list的末尾
mylist.insert(2,4.5)# 根据索引位置来插入
mylist.sort() # 布尔类型在前.数字按从小到大的顺序排列.
print(mylist.index(6.5))# 返回6.5所在的索引
del mylist[0] # 删除首个索引的值
print(mylist)
4
[True, 3, 4.5, 6.5, 1024]
print(range(10))
print(list(range(10)))
print(list(range(5,10)))
print(list(range(5,10,2)))# 每两个数之间相差 2(不包含 10)
print(list(range(10,1,-1)))# 倒着数数,不包含1
range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9]
[5, 7, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2]
myName = "David"
myName[3] # 打印出第三个字符
myName * 2 # 双拼
len(myName) # 打印字符串的长度
myName.upper() # 大写表示
myName.center(1) # 返回一个字符串,w 长度,原字符串居中
myName.split('v') # 以 v 为分隔符,将原字符串分割,返回一个列表
mylist[0] = 2 ** 10
mylist
myName
myName[0]='x'# TypeError: 'str' object does not support item assignment
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-93-78f7cd226660> in <module>()
2 mylist
3 myName
----> 4 myName[0]='x'
TypeError: 'str' object does not support item assignment
mytuple=(1,True,4.96)
mytuple[1] = "A"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-108-c691abdb7a10> in <module>()
1 mytuple=(1,True,4.96)
2
----> 3 mytuple[1] = "A"
4
5
TypeError: 'tuple' object does not support item assignment
mytuple=(2,True,4.96)
mytuple
len(mytuple)
myset = {3.6,"cat",4.5,"False"}
myset
{3.6, 4.5, 'False', 'cat'}
A = {1,2,3,4}
B = {3,4,5,6}
A.union(B) # 并集
A.intersection(B) # 交集
A.difference(B) # 抛去A,B共有的,取A剩下的部分.
A.issubset(B) # 判断集合 A 中的所有元素是否都在集合 B 中.
A.add(7)#把 item 这个元素添加到集合 A 中
A.remove(3)
A
B
myset = {False,4.5,3,6,"cat"}
yourset= {99,3,100}
myset.union(yourset)
myset|yourset # 并集
myset.intersection(yourset) # 交集
myset.difference(yourset) # 抛去myset,yourset共有的,取myset剩下的部分.
myset-yourset # {4.5, 6, False, 'cat'} 同上
{3,100}<=yourset # 包含的关系
myset.add("house")
myset
yourset
{3.100}.issubset(yourset) # False,这些元素全在yourset中,但是缺少一个.
yourset.pop()
yourset
phoneext={'david':1410,'brad':1137}
phoneext.keys()
# dict_keys(['david', 'brad'])
list(phoneext.keys())
# ['david', 'brad']
list(phoneext.values())
# [1410, 1137]
phoneext.items() # 键值对组合
# dict_items([('david', 1410), ('brad', 1137)])
list(phoneext.items())
# [('david', 1410), ('brad', 1137)]
phoneext.get("kent",111)# kent不存在,返回111
sradius=input("please enter the radius of the cirle: ")
radius = float(sradius)
diameter = 2 * radius
diameter # 输入1,显示2.0
please enter the radius of the cirle: 1
2.0
print("Hello","World",sep="****") # Hello****World,sep代表着在中间拼接
print("Hello","Word",end="***")# Hello Word***,end代表着在尾部拼接
print(aName, "is", age, "years old.")
Hello****World
Hello Word***
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-55582d8d6dbc> in <module>()
1 print("Hello","World",sep="****") # Hello****World,sep代表着在中间拼接
2 print("Hello","Word",end="***")# Hello Word***,end代表着在尾部拼接
----> 3 print(aName, "is", age, "years old.")
4 print("%s is %d years old."%(aName,age))
NameError: name 'aName' is not defined