Python 核心编程

1、程序输出:

	print(value,..., sep=' ', end='\n', file=sys.stdout, flush=Flase)
默认参数:sep=' ' 表示输出多个value时默认以‘空格’来分隔;

  end='\n' 表示输出value时会默认在每一行后面添加 换行符“\n”;

 file=sys.stdout 表示默认采用标准输出(即向屏幕输出内容);

 flush=Flase  作用....

2、程序输入:

	input(prompt=None, /)

3、注释:

单行注释:#

多行注释:'''    '''

4、操作符:

+ 、- 、*、/、//、%、** 分别表示:加、减、乘、地板除、求余、乘方;

<、<=、>、>=、==、!=、<> 

5、格式化字符串:

%,%s, %d,%f,......

6、列表:

[ ]

7、元组:

()

8、字典:

{ }

9、if语句:

if a<0:
    print("a小于0")
elif a>0:
    print("a大于0")
else:
    print("a等于0")

10、while循环:

count=0
while count<5:
    print("count is %d" % count)
    count+=1
11、for循环和range()内建函数:

for eachNum in range(5):
    print(eachNum)
range()内建函数用来生成列表
12、列表解析:

squared = [x ** 2 for x in range(8) if not x % 2 and x != 0]
for i in squared:
    print(i)
13、内建函数open():

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
14、错误和异常:

try:
    filename = input("Enter file name:")
    fobj = open(filename, 'r')
    for eachLine in fobj:
        print(eachLine)
    fobj.close()
except IOError as e:
    print("file open error:", e)
>>>
Enter file name:hhh.txt
file open error: [Errno 2] No such file or directory: 'hhh.txt'
15、函数:

用关键字 def 来定义函数:

def function_name([arguments]):
    "optional documentation string"
    function_sutie
[arguments]  : 表示可选参数。

16、类:

类 就是可以批量生产具有相似 功能、方法(函数)、属性 的 加工厂。

class FooClass(object):
    "my very first class:FooClass"
    version = 0.1
    def __init__(self, nm="Jack Cheng"):
        self.name = nm
        print("Created a class instance for", nm, self.name)

    def showname(self):
        print("Your name is", self.name)
        print("My name is", self.__class__.__name__)
当一个类实例被创建时,__init__() 方法会自动执行,类似构造器,它的目的是用来执行一些必要的初始化工作。

17、模块:

>>> import sys
>>> sys.stdout.write('Hello World!\n')
Hello World!
13
>>> sys.stdout.write('Hello World! \n')
Hello World! 
14
>>> sys.stdout.write('Hello World! \n')
Hello World! 
14
>>> sys.stdout.write('Hello World!  \n')
Hello World!  
15
>>>


18、实用函数:

dir([obj]) : 显示对象的属性,如果没有提供参数,则显示全局变量的名字;

help([obj]) :  显示对象的帮助文档,如果没有提供参数,则会进入交互式帮助;






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值