Python 学习笔记



#!/usr/bin/python
# -*- coding: UTF-8 -*-
#coding=utf-8


import sys,os          #import 导入模块


#import a as b             # 引入模块a,并将模块a重命名为b

#from a import function1   # 从模块a中引入function1对象。调用a中对象时,我们不用再说明模块,即直接使用function1,而不是a.function1。

#from a import *           # 从模块a中引入所有对象。调用a中对象时,我们不用再说明模块,即直接使用对象,而不是a.对象。 这些引用方式,可以方便后面的程序书写。


print "hello python"
print "it's cool"

print sys.argv[0]   # argv0 表示脚本本身,依次类推表示输入参数
#print sys.argv

#以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入;
#以双下划线开头的 __foo 代表类的私有成员;以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数。
#python 中多行注释使用三个单引号(''')或三个双引号(""")
'''
20171016 多行注释测试之三个单引号
'''
"""
20161016 多行注释测试之三个双引号
"""
#井号注释单行

age = 26                            #Numbers(数字)数据类型不能改变
name = 'Andy'                       #String(字符串)可以使用'或则"来创建字符串 sting[1:5] 1 5表示下角标,从零0,截取算头不算尾
skill_set=['sql','etl']             #List(列表) 列表的数据项不需要具有相同的类型 max() min() len() cmp() list() 元组转换成列表 list对象有一系列方法
workgroup=('BI','R&A')              #Tuple(元组)不能重新负值 Python的元组与列表类似,不同之处在于元组的元素不能修改
person = {'first_name':'zhao','second_name':'Andy'}                           #Dictionary(字典)

print "i am",name,", i am ",age
print name.strip()

print skill_set[0],skill_set[1]
print skill_set
print skill_set[1:14]

print person.keys()
print person.values()


print 10**2   #**幂运算
print 9//2    #//取整除  注意在python2.x整数除以整数只能是整数
print 6%5     #% 取模

# == != <> >= <= > < 比较运算

if (1>2):
 print "1>2"
elif (1<2):
 print "1<2"
else:
 print "1=2"

# and or not 逻辑运算符号  非零就为true ; in 成员运算 ,在则返回true不在则返回false

a = 10
b = 0
 
if ( a and b ):
   print "1 - variables both  a and  b are true "
else:
   print "1 - varianles eihter of a and  b is not  true"
  
if ( 'sql' in skill_set):
 print "YES"
else:
 print "NO"

""" 条件判断 无switch
if 判断条件1:
    执行语句1……
elif 判断条件2:
    执行语句2……
elif 判断条件3:
    执行语句3……
else:
    执行语句4……
"""


count = 0
while (count < 9):
   print 'The count is:', count
   count = count + 1
print "Good bye!"

var = 1
while var == 1 :  # 该条件永远为true,循环将无限执行下去
 num = raw_input("Enter a number  :(Enter 0 to exit)")
 if(int(num) == 0):
  break
 else:
  print "You entered: ", num
 
print "Good bye!"


#Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        #
   print 'current fruit :', fruit
   for letter in fruit:
 print letter

#数值函数 ceil() floor()

e='121212'
print "hello"+e


#异常

try:
 print os.getcwd()
 fo = open("testfile.txt", 'w')
 print fo.name
 fo.write('hello')
except:
 print "file open fialed"
else:
 print "file open successfully "
 fo.close()

 
   

'''
try:
<语句>        #运行别的代码
except <名字>:
<语句>        #如果在try部份引发了'name'异常
except <名字>,<数据>:
<语句>        #如果引发了'name'异常,获得附加的数据
else:
<语句>        #如果没有异常发生
finally:
<语句>        #总会执行
'''


##不可变类型:类似 c++ 的值传递,如 整数、字符串、元组。如fun(a),传递的只是a的值,没有影响a对象本身。比如在 fun(a)内部修改 a 的值,只是修改另一个复制的对象,不会影响 a 本身。

##可变类型:类似 c++ 的引用传递,如 列表,字典。如 fun(la),则是将 la 真正的传过去,修改后fun外部的la也会受影响


def printinfo( arg1,arg2=2,*vartuple ):
   "打印任何传入的参数"
   print "output "
   print arg1
   for var in vartuple:
  print var
   return

printinfo(arg1=2,arg2=2)

#文件IO
print os.getcwd()
f1 = open('ftest.txt','w+')
print f1.name,f1.mode,f1.closed
f1.write('hello\nthis is file object test')
position = f1.tell()
print position
f1.seek(0,0) #seek(offset [,from])
str = f1.read(2)
f1.close()
print str
os.rename('ftest.txt','filetest1.txt')
os.remove('testfile.txt')
os.mkdir('testdir')
os.chdir('testdir')
print os.getcwd()
os.chdir('C:\Users\zhaoy10')
print os.getcwd()
os.rmdir('testdir')
os.remove('filetest1.txt')


#Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句
#1、当前目录
#2、如果不在当前目录,Python 则搜索在 shell 变量 PYTHONPATH 下的每个目录。
#3、如果都找不到,Python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值