#hello.py
print ('hello %s %d' %('sss!',100))
#list用法
classmates = ['A','B','C']
print (len(classmates)) #3
print (classmates[2])
if (classmates[2]==classmates[-1]):
print ('可以反向查找')
#但是不可以classmates[-4],越界
classmates.append('D')
print (classmates) #['A',..'D']
classmates.pop(1)
classmates.insert(1,'E')
print (classmates) #['A','E','C','D']
L=['A',123,True]
print (L)
s=['a',L,'b']
print (s,(len(s))) #3
print ((s[1][2])) #True
#tuple介绍
classmate=('A','B','C')
print (classmate[-2]) #B
t1=() #空的tuple
t2=(1) #t2=1,只是声明了一个变量t2=10
t3=(1,) #t3是一个一个元素的tuple
print (t3) #(1,)
t4=('a','b',['c','d']) #可变的tuple
print (t4)
t4[2].pop(0)
print (t4) #('a','b',['d']) 因为虽然t4内容变了,
#但是t4[2]指向的内容没有变,都是指同一个list
x=10
if x>=9:
print ('x>=9\n x')
elif x>=6:
print ('x>=6')
else:
print ('x<6\n')
#if语句
#birth = input('birth: ')
birth='2010'
birth=int(birth)
if (birth<2000):
print ('00前')
else:
print ('00后')
#循环语句
names = ['a','b','c']
for name in names:
print(name)
print (list(range(5))) #[0,1,2,3,4]
sum=0
range(1,100)
for x in range(101): #0到100
sum=sum+x
print (sum)
sum=0
n=100
while n>0:
sum=sum+n
n=n-1
print (sum)
#dictionary
#下面的a,b,c称为key,key不可以是list,因为key必须为不可变对象
d = {'a':95, 'b':75, 'c':60}
print (d)
print (d['a'])
d['a']=100
print (d['a']) #100
print ('c' in d , 'e' in d) #True False
print (d.get('a'), d.get('a',-1),d.get('e'),d.get('e',-1))
#100 100 None -1r
#set
s = set([1,2,3,3,2,1]) #将一个list作为参数
print (s) #{1,2,3}
s.add('4') #可以插入不同类型
print (s) #{1,2,3,'4'}
print (s.remove(1)) #None
print (s) #{2,3,'4'}
s1 = set({1,2,3})
s2 = set({2,3,4})
print (s1 & s2) #交集
print (s1 | s2) #并集
a = (1,6,5)
#a.sort() 错误
#a为可变对象
a = [1,6,5]
a.sort() #a=[1,5,6]
#a为不可变对象
a='abc'
b=a.replace('a','A') #对于不可变对象,调用自身任何方法都没有改变自身
print (a,b) #abc Abc
print (abs(-1),max(1,-1,2))
#类型转换
int('123') #123
int(12.34) #12
float('12.34') #12.34
str(1.23) #'1.23'
str(100) #'100'
bool(1) #True
bool('') #False
a=abs
print (a(-1)) #给函数起别名
print (hex(15)) #0xf
#print (my_abs(-10)) 错误,没有定义my_abs()
from fun1 import my_abs #导入函数,fun1为文件名(fun1.py),后面是函数名
print (my_abs(-10))
#空函数
def nop():
pass
def my_abs1(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
import math
def move(x,y,step,angle=0): #默认angel为0
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx,ny #返回一个tuple
x,y = move (100,100,60,math.pi/6)
print (x,y)
r = move(100,100,60,math.pi/6)
print (r) #(x,y)
math.sqrt(2) #1.414
#可变参数
def calc(*number):
sum=0
for n in number:
sum=sum+n*n
return sum
print (calc(1,2),calc()) #5 0
nums = [1,2,3]
print (calc(*nums))
L = [0,1,2,3]
print (L[:3]) #[0,1,2]
print (L[1:3]) #[1,2]
print (L[-3:-1]) #[1,2]
L = list(range(100))
print (L[::10]) #[10,20,,,90]
L='abcd'
print (L[1:4:2]) #bd
d = {'a':1,'b':2,'c':3}
for key in d: #顺序不定
print(key) #a,b,c
for v in d.values():
print (v)
for k,v in d.items():
print (k,v)
print ([x*x for x in range(1,11) if x%2==0]) #4 16 36 64 100
print ([m+n for m in 'ABC' if m!='A'for n in '123' if n!='1'])
#['b2','b3','c2','c3']
L = ['Hello', 'World', 'IBM', 'Apple']
print ([s.lower() for s in L]) #大小写
print ([s.upper() for s in L])
print (isinstance(L[0],str)) #True
def fib(max):
n=0;a=0;b=1
while(n<max):
a,b=b,a+b
n=n+1
return a
print (fib(5))
#高阶函数,函数作为参数传入函数
def add(a,b,f):
return f(a)+f(b)
print (add(1,-2,abs)) #3