发布时间:2019-09-08 09:10:20编辑:auto阅读(1662)
print ('hello python')
print ("hello python")
print('''hello python
python2
pytnon3''')
a = '123'
b = a
a = '456'
print a,b
运行结果实际上是456,123
- len()函数可以获取list的长度
friendlist = ['Alice','Bob','Clark']
print len(friendlist)
- list的索引依旧是从0开始,可以用负数
n 来取倒数第|n| 个元素
friendlist = ['Alice','Bob','Clark']
print friendlist[-1], friendlist[-2], friendlist[-3]
- list可以通过append将新元素加入末尾,也可以通过insert方法插入指定位置,还可以通过pop进行删除(可加索引),也可以直接赋值给指定的索引位置
#append
friendlist = ['Alice','Bob', 'Clark']
friendlist.append('David')
print friendlist
#insert
friendlist.insert(1,'Evil')
print friendlist
#pop
friendlist.pop(3)
print friendlist
- list中的元素可以是相同的数据类型也可以是不同的数据类型
my_list = ['Apple', 123, 3.2]
print my_list
- 另外一种有序列表叫做tuple(元组),但是tuple初始化后不能进行修改,tuple是用小括号。
my_tuple = ('apple', 4, 3.14)
print my_tuple
- tuple的不变是指每个元素的指向不变,但是tuple的每个元素可以发生变化,但是如果改变下面的整数或者浮点数将会报错
my_tuple = (['apple','banana'], 4, 3.14)
my_tuple[0][0] = 'orange'
my_tuple[0][1] = 'lemon'
print my_tuple
age = 12
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
else:
print 'kid'
sum = 0
for x in range(101):
sum += x
print sum
dictionary = {'Son':20, 'Father':50,'Mother':48}
print dictionary['Son']
#Use in
dictionary = {'Son':20, 'Father':50,'Mother':48}
print 'Sister' in dictionary
#Use get method
print dictionary.get('Sister')
前者会输出False,而后者会输出None.可以在get函数参数指定想要得到的value(如何找不到对应的value,则输出预设的值)
dictionary = {'Son':20, 'Father':50,'Mother':48}
print dictionary
dictionary.pop('Father')
print dictionary
my_set = set([1,2,2])
print my_set
set_1 = set([1,2,3])
set_2 = set([2,3,4])
print set_1&set_2
print set_1|set_2
###list
print cmp(2,1)
print cmp(1,1)
print cmp(1,2)
a = abs
print a(-1.5)
def judge_age(age):
if age >=18:
pass
elif age >= 12:
print 'teenager'
else:
print 'kid'
judge_age(18)
#calculate a circle's area and length
def calculate_circle_parameters(radius):
area = 3.14*radius*radius
length = 6.28*radius
return area,length
print calculate_circle_parameters(4)
#calculate a circle's area and length
def calculate_circle_parameters(radius,pi = 3.14):
area = pi*radius*radius
length = pi*radius
return area,length
print calculate_circle_parameters(4)
print calculate_circle_parameters(4, pi=3.1415926)
def add_list(L = []):
L.append('0')
return L
print add_list()
print add_list()
def calculate_sum(*num):
result = 0
for x in num:
result += x
return result
print calculate_sum()
print calculate_sum(1,2,3)
L = [2,3,4]
print calculate_sum(*L)
T = (4,5,6)
print calculate_sum(*T)
def person(name,age,**kw):
print 'name:',name, 'age:',age, 'others',kw
person('Bauer', 30)
person('Bauer', 30, city='Guangzhou')
kw = {'city':'Guangzhou','job':'teacher'}
person('Bauer',30,**kw)
def func(a,b,c=0,*args,**kw):
print 'a = ', a, 'b = ',b, 'c = ',c ,'args = ',args, 'kw = ',kw
func(1,2)
func(1,2,c=3)
func(1,2,3,'a','b')
func(1,2,3,'a','b',x=99)
我们下期见!~
上一篇: python中string和bool的转
下一篇: Python项目使用memcached缓
47857
46422
37309
34755
29327
25986
24937
19964
19558
18045
5804°
6429°
5944°
5973°
7077°
5924°
5959°
6452°
6415°
7796°