Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1541044
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2017-10-09 10:56:10



  1. apple_fruit=10
  2. print(apple_fruit)

  3. condition=1
  4. while condition<3:
  5.     print(condition)
  6.     condition +=1

  7. example_list=[1,2,3,4,5,6,7]
  8. for i in example_list:
  9.     print(i)
  10. '''
  11. Windows里面用Control [来对齐代码
  12. MAC OS里面用Commnd [来对齐代码
  13. '''

  14. for i in range(1,4,2):
  15.     print(i)
  16.     
  17. x=1
  18. y=2
  19. z=3
  20. if x>y:
  21.     print("x is greater than y")
  22. '''
  23. Python 和C语言一样,if elif else分支语句只会执行第一个满足条件的分支
  24. '''
def fun(a,b):
    c=a/b
    print(c)

fun(1,2)
fun(b=2,a=1)
fun(b=1,a=2)
'''
输出,可以用参数名来明确指定参数的值,可以不按默认顺序赋值
0.5
0.5
2.0
'''

单行注释与多行注释
全局变量与局部变量
  1. APPLE=100
  2. def fun():
  3.     a=10
  4.     return a+100
  5. print(APPLE)
  6. #print(a) 错误 局部变量无法在这里访问


  7. b=None
  8. def foo():
  9.     global b #如果没有这句,那么后面的输出也应该是None
  10.     b=20
  11. print('before foo:',b)
  12. foo()
  13. print('after foo:',b)
  14. '''
  15. before foo: None
  16. after foo: 20
  17. '''

  1. """
  2. MAC OS
  3. sudo pip3 install numpy 安装
  4. sudo pip3 uninstall numpy 卸载
  5. sudo pip3 install numpy-1.9.2 指定版本
  6. sudo pip3 install -U numpy

  7. """


  1. text='This is my first test.\nThis is next line.\nThis is last line.'

  2. my_file=open('01myfile.txt','w')
  3. my_file.write(text)
  4. my_file.close()

  5. append_text='\nThis is appended file.'
  6. my_file=open('my_file.txt','a')
  7. my_file.write(append_text)
  8. my_file.close()

 file=open('01myfile.txt','r')
 content1=file.read()
 print(content1)
 content2=file.readlines()
 print(content2)

类,初始化,缺省参数
  1. class Calculator:
  2.     name='Good calculation'
  3.     price=18
  4.     def __init__(self,name,price,hight=18,width=29,weight=5):
  5.         self.name=name
  6.         self.price=price
  7.         self.h=hight
  8.         self.wi=width
  9.         self.we=weight
  10.     def add(self,x,y):
  11.         result=x+y
  12.         print(result)
  13.     def minus(self,x,y):
  14.         result=x-y
  15.         print(result)
  16.     def times(self,x,y):
  17.         result=x*y
  18.         print(result)
  19.     def divide(self,x,y):
  20.         result=x/y
  21.         print(result)

输入
  1. a=input('Please give a number:')#返回值是string,所以下面要用字符串形式
  2. #a=int(input('Please give a number:')) 或者用int函数转换为int,那么下面直接用int即可
  3. if a=='1':
  4.     print('This is a good one')
  5. elif a==str(2):
  6.     print('See you next time')
  7. else:
  8.     print('Good luck')


元组与列表
  1. #tuple list
  2. a_tuple=(12,3,5,15,6)
  3. another_tupl=2,4,6,8,9
  4. a_list=[12,3,67,7,82]
  5. for content in a_tuple:
  6.     print(content)
  7. for content in a_list:
  8.     print(content)

  9. for index in range(len(a_list)):#len 求list的元素个数 range范围函数
  10.     print('index=',index,'number in list=',a_list[index])
  11. """
  12. 用双引号的多行注释,;另外python下标从0开始
  13. index= 0 number in list= 12
  14. index= 1 number in list= 3
  15. index= 2 number in list= 67
  16. index= 3 number in list= 7
  17. index= 4 number in list= 82
  18. """

  19. ap=[1,2,3,4,5,6,7,2]
  20. ap.append(0)
  21. print(ap)
  22. ap.insert(1,0)#在1的位置插入0, 注意list从index 0开始
  23. print(ap)
  24. ap.remove(2)#移除第一个2
  25. print(ap)
  26. print(ap[0])#第0个元素
  27. print(ap[-1])#最后一个元素
  28. print(ap[1:3])#第0第1第2个元素
  29. print(ap[3:])#3到末尾的元素
  30. print(ap.index(5))#第一次出现5的索引
  31. print(ap.count(4))#共有多少个4出现
  32. ap.sort()
  33. print(ap)
  34. ap.sort(reverse=True)
  35. print(ap)
  36. """
  37. [1, 2, 3, 4, 5, 6, 7, 2, 0]
  38. [1, 0, 2, 3, 4, 5, 6, 7, 2, 0]
  39. [1, 0, 3, 4, 5, 6, 7, 2, 0]
  40. 1
  41. 0
  42. [0, 3]
  43. [4, 5, 6, 7, 2, 0]
  44. 4
  45. 1
  46. [0, 0, 1, 2, 3, 4, 5, 6, 7]
  47. [7, 6, 5, 4, 3, 2, 1, 0, 0]
  48. """


字典
  1. d1={'apple':1,'pear':{1:3,3:'a'},'orange':3}#字典可以嵌套
  2. print(d1)
  3. d1['melon']=4
  4. print(d1)
  5. print(d1['pear'][3])#引用嵌套的字典
  6. """
  7. {'apple': 1, 'pear': {1: 3, 3: 'a'}, 'orange': 3}
  8. {'apple': 1, 'pear': {1: 3, 3: 'a'}, 'orange': 3, 'melon': 4}
  9. a
  10. """

Module
numpy,pandas








阅读(1698) | 评论(0) | 转发(0) |
0

上一篇:[20170722 PRO]

下一篇:Git basic

给主人留下些什么吧!~~