pycharm是个不错的python IDE

 

单行注释:#,区域注释 '''注释部分 '''

一、输入输出

1 输出:

 

print "hello, python"
print  3+2 < 5-7
print " what is 3+2 ?",3+2
my_name = 'huangzq' # parameter
my_age = 35
print "let's talk about %s , he is %d years old" %(my_name,my_age)
str = "huangzq"
str2 = "good boy"
print str+str2
x = " there are %d types of people." % 10
print x
print "."*10
print "pigred" *10
formatter = "%r %r %r"
print formatter % (1,2,3)
print formatter % ("one","two","three")
print formatter % ("xiaoqiang",True,False)
red = "huangzq"
print " %s is a good boy" % red
sent = "yellow is a \"good \" boy %s " % red
print sent

输出多行

print """
Alright ,so you said %r about liking me
you live in %r. Not sure where tha is.
 and you have a %r computer. Nice.
 """ % (likes,lives,computer)

 

2 输入:

 

print 'how old are you?',
age = raw_input()
print "so, youare %r years old" % age
weight = raw_input('how much do you weigh')
print 'your weight is %r' % weight
 

 

二、调用python脚本:

 
 
a.py文件
 
from sys import argv
script, first, second = argv
print " the script is called:", script
print "your first variable is :", first
print "your second variable is: ",second
调用 python a.py param1 param2

 

三、读写文件:

1 读入文件:
 
txt = open(filename)
print "here's your file %r:" % filename
print txt.read()


2 写入文件:

 
print "Opening the file ..."
target = open(filename,'w')

print "truncateing the file . goodbye!"
target.truncate()

print "now i'm going to ask you for thee lines."

line1 = raw_input("line 1:")
line2 = raw_input("line 2:")
line3 = raw_input("line 3:")

print " i'm going to write these to the file."

target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "and finally, we close it."
target.close()
 
 
current_file = open(input_file)
 
current_file.seek(0) 文件操作指针转到文件开头
 
读写:
 
ofile = open('beijin.txt','r')
wfile = open("beijin2.txt",'w')
wfile.truncate()
#str_sql = "create table hzq_assist_tmp{realcode varchar};"
str_sql = "create table hzq_assist_tmp as select real_code from nav_realimage limit 0;\n"
wfile.write(str_sql)
str_sql = "insert into hzq_assist_tmp values{"

for eachline in ofile:
    aline = eachline.rstrip()
    str_sql = str_sql + "\"" + aline + "\","
wfile.write(str_sql)

wfile.close()
ofile.close()
 

四、函数

函数用 

def  函数名(参数):

    函数具体实现

方式定义,函数的实现前面是四个空格,如果没有四个空格说明函数结束

 

传参执行:(test.py)

 

# this one is like your scripts with argv
def print_two(*args):
    arg1, arg2 = args
    print "args: %r, %r" % (arg1,arg2)

def print_two_again(arg1, arg2):
    print "arg1: %r, arg2 %r" % (arg1,arg2)

def print_one(arg1):
    print " arg1 %r" % arg1

def print_none():
    print " i got nothing "

#call function
print_two("yellow","red")
print_two_again("daddy","mummy")
print_one("love")
print_none()

 

五、列表

 

 list 的使用:

 

eyes = ['brown', 'blue', 'green']
weights = [1, 2, 3, 4]
change = [1,'pennies',2,'dimes',3,'quartrs']

for number in  weights:
    print " this is count %d" % number
for color in eyes:
    print "this is %r eye color" % color
for i in change:
    print "this is change %r" % i

elements = []
for i in range(0,6):
    elements.append(i)
for i in elements:
    print "element was: %d" % i

 

 

 

类继承:

参考:http://www.cnblogs.com/mmix2009/p/3538663.html

 

class SchoolMember:
    '''Represents any school member.'''
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print '( initialized school member %s)'% self.name
    def tell(self):
        '''tell my details'''
        print 'name:"%s" age:"%s"' %(self.name, self.age)
class Teacher(SchoolMember):
    '''represent a school teacher'''
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print'(initialize teacher:%s)' % self.name
    def tell(self):
        SchoolMember.tell(self)
        print'Salary: "%d"' % self.salary
class Student(SchoolMember):
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print'(initialize student:%s)' % self.name
    def tell(self):
        SchoolMember.tell(self)
        print 'marks: %d' % self.marks
t = Teacher('Mr huang', 27, 14000)
s = Student('Mz su', 26, 98)
members = [t,s]
for member in members:
    member.tell()

 

获取系统时间、sleep

 

import time 
import datetime
while 1:
  time.sleep(0.5)
  now = datetime.datetime.now()
  otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
  print otherStyleTime

模块与包:(参考https://www.cnblogs.com/heyongqi/p/5565651.html

模块
1.创建模块:创建一个扩展名为.py的文件就相当于创建了一个模块,文件名即为模块名,文件中的内容即为模块所包含的内容。
2.导入模块:import 模块名
3.使用模块:模块名.变量名(可为普通变量或函数或类等)
4.导入模块中的某个元素:from 模块名 import  变量。(可多级导入)
5.导入模块中的所有元素:from 模块名 import *。
注:2和4、5的区别:使用2方式时,引用模块中的元素需要使用“模块名.变量名”的形式,而4、5可直接使用变量名。


1.创建包:
1)新建一个文件夹,文件夹名即为包名。
2)在文件夹(包)中创建一个__init__.py文件。(这一步是必须的,不然构不成一个包,注意:init前后各两个下划线)
3)在文件夹中添加想包含在包中的模块。
4)在__init__.py中导入这些模块或模块中的元素。
2.导入包:import 包名。
3.使用包:同模块使用方法一样。
4.from 包名 import *的使用:

调试:

如果有图形界面建议用pycharm调试,简直不能再爽。

如果没有图形界面,用pyb调试,如:python -m pdb mytest.py arg1 arg2

常用命令: 
l #查看运行到哪行代码 
n #单步运行,跳过函数 
s #单步运行,可进入函数 
p 变量 #查看变量值 
b 行号 #断点设置到第几行 
b #显示所有断点列表 
cl 断点号 #删除某个断点 
cl #删除所有断点 
c #跳到下一个断点 
r #return当前函数 
exit #退出 

参考:https://www.cnblogs.com/jingzhishen/p/3493991.html

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐