python10_1day

x=input(“x:”)

y=input(“y:”)#input假设用户输入的是标准表达式,

print(int(x)*int(y))

print(pow(2, 3))
from math import sqrt
import cmath

print(sqrt(8))
print(cmath.sqrt(-1))
print(repr(“hello word”)) # 以合法的形式表达值
print(“hello \nword”) # \进行转义
greeting = ‘hello’ # 索引
print(greeting[0])
test_1 = [1, 2, 3] * 3
test_2 = [4, 5, 6]
print(test_1)
print(max(test_1))
print(len(test_1))
list(‘hello’)
test_1.extend(test_2) # 一次追加
x = test_2.count(4) # 计数
print(x)
print(test_2.index(6)) # index显示匹配项的索引值
y = tuple([89, 65, 32]) # 转换元组
print(y)

%格式化

print(“price of egg:%d” % 42)
fo = “hello,%s,%s \nenough for ya?”
value = (‘word’, ‘hot’)
print(fo % value)
title = “l love python,you?”
print(title.find(‘you’)) # find查找字符串返回字符串最左端索引
seq = [‘1’, ‘2’, ‘5’, ‘9’]
sep = ‘+’
sep.join(seq) # 连接
print(sep.join(seq))
print(title.upper())
print(title.lower())
print(title.replace(’,’, ’ what '))
items = [(‘name’, ‘first’), (‘age’, 42)]
d = dict(items)
print(d)
phonebook = {‘beth’: ‘9012’, ‘alice’: ‘2341’, ‘cecil’: [‘3258’, ‘89’, ‘98’]}
print(“cecil’s phone number is %(cecil)s.” % phonebook) # 格式化字典
return_value = d.clear()
print(return_value)
x_1 = {}
y_1 = x_1
x_1[‘key’] = ‘value’
print(y_1)
z = phonebook.copy()
z[‘beth’] = ‘1996’
z[‘cecil’].remove(‘98’) # copy潜复制,在副本中替换值时,原始字典不受影响。
print(phonebook)
print(z)
from copy import deepcopy

a = deepcopy(phonebook)
a[‘cecil’].append(‘99’) # deepcopy深复制,副本原本互不影响
print(a)
b = {}
print(b.get(‘name’)) # 访问的字典不存在时,不会报错,has_key不存在3.0中,检查字典中是否含有键
print(phonebook.items()) # items将字典以列表的形式返回
print(phonebook.keys())
print(phonebook.values())

print(b.popitem())#移除随机的项

print(b.setdefault(‘name’, ‘N/A’))
print(b.update(phonebook)) # update将用一个字典更新另一个字典

赋值

序列解包

e, f, g = 1, 2, 3
print(e, f, g)
values = 1, 2, 3

链式赋值

x = 1
while x <= 10:
print(x)
x += 1

并行迭代

names = [‘anne’, ‘beth’, ‘grorge’, ‘damon’]
ages = [12, 45, 32, 102]
for name, age in zip(names, ages): # zip内置函数,将两个序列压缩,返回元组的列表
print(name + ’ ’ + str(age) + " years old.")

编号迭代

index_b = 0
for name in names:
if ‘anne’ in name:
# index_b=names.index(name)
names[index_b] = ‘[censored]’
index_b += 1

翻转和排序迭代

exec(“print(“hello word”)”) # exec执行存储在字符串或文件中的语句。eval将字符串当成有效的表达式来求值并返回结果

猜你喜欢

转载自blog.csdn.net/qq_38501057/article/details/88426463
今日推荐