python入门之初体验-----A Byte of Python3(观后感)

1.Python语言是开源,简单主义思想,既支持面向过程编程也支持面向对象的编程,并且丰富的库。


2.常量:

           数:整数(int)、浮点数(float)和复数:如(-5+4j)和(2.3-4.6j)

           字符串:注意python预言中双引号和单引号用法含义完全一样


3.数据结构:

            列表:例子shoplist = ['apple', 'mango', 'carrot', 'banana'],常用方法len(),append(),sort()等

            元组:例子zoo = ('python', 'elephant', 'penguin'),元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组,但和列表都能通过检索获取内容shoplist[0],zoo[0],new_zoo = ('monkey', 'camel', zoo),new[2][2]

            字典:键值对,记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。常用方法:item()

 ab = { 'Swaroop' : 'swaroop@swaroopch.com',
            'Larry' : 'larry@wall.org',
            'Matsumoto' : 'matz@ruby-lang.org',
            'Spammer' : 'spammer@hotmail.com'
         }

for name, address in ab.items():
      print('Contact {0} at {1}'.format(name, address))

#添加键值对

ab['Guido'] = 'guido@python.org'

#删除键值对

del ab['Spammer']

            集合:bri = set(['brazil', 'russia', 'india']),集合是没有顺序的简单对象的聚集。当在聚集中一个对象的存在比其顺序或者出现的次数重要时使用集合。使用集合,可以检查是否是成员,是否是另一个集合的子集,得到两个集合的交集等等


4.控制流:在Python 中有三种控制流语句——if、for 和while


5.面向对象编程:

             self方法:MyObject.method(arg1, arg2) 的时候,这会由Python 自动转为MyClass.method(MyObject, arg1, arg2)—— 这就是self 的原理了。

             __init__方法:__init__ 方法相当于C++,Java,C# 中的构造函数,例子

class Person:
   def __init__(self, name):
      self.name = name
   def sayHi(self):
     print('Hello, my name is', self.name)

p = Person('Swaroop')
p.sayHi()

             子类、父类:如果它不能在导出类中找到对应的方法,它才开始到基本类中逐个查找。
基本类是在类定义的时候

             例子:

1 #!/usr/bin/python
2 # Filename: inherit.py
3
4 class SchoolMember:
5 '''Represent any school member.'''
6 def __init__(self,name,age):
7 self.name = name
8 self.age = age
9 print('(Initialize SchoolMember:{0})'.format(self.name))
10
11 def tell(self):
12 '''Tell my details.'''
13 print('Name:"{0}" Age:"{1}"'.format(self.name,self.age),end
='')
14
15 class Teacher(SchoolMember):
16 '''Repressent a teacher.'''
17 def __init__(self,name,age,salary):
18 SchoolMember.__init__(self,name,age)
19 self.salary = salary
20 print('(Initialized Teacher:{0})'.format(self.name))
21
22 def tell(self):
23 SchoolMember.tell(self)
24 print('Salary:"{0:d}"'.format(self.salary))
25
26 class Student(SchoolMember):
27 '''Represents a student'''
28 def __init__(self,name,age,marks):
29 SchoolMember.__init__(self,name,age)
30 self.marks = marks
31 print('(Initialized Student:{0})'.format(self.name))
32
33 def tell(self):
34 SchoolMember.tell(self)
35 print('Marks:"{0:d}"'.format(self.marks))
36
37 t = Teacher('Mrs.Shrividya',30,30000)
38 s = Student('Swaroop',25,75)

42 members = [t,s]
43 for member in members:
44 member.tell() # work for both Teacher and Students

输出:

1 $ python inherit.py
2 (Initialize SchoolMember:Mrs.Shrividya)
3 (Initialized Teacher:Mrs.Shrividya)
4 (Initialize SchoolMember:Swaroop)
5 (Initialized Student:Swaroop)
6
7 Name:"Mrs.Shrividya" Age:"30"Salary:"30000"
8 Name:"Swaroop" Age:"25"Marks:"75"


6.异常

1 class ShortInputException(Exception):
2 '''A user-defined exception class'''
3 def __init__(self, length,atleast):

4 Exception.__init__(self)
5 self.length = length
6 self.atleast = atleast
7
8 try:
9 text = input('Enter something-->')
10 if len(text) < 3:
11 raise ShortInputException(len(text),3)
12 #other work can continue as usual here
13 except EOFError:
14 print('Why did you do an EOF on me')
15 except ShortInputException as ex:
16 print('ShortInputException The input was {0} long, excepted
\
17 atleast {1}'.format(ex.length, ex.atleast))
18 else:
19 print('No exception was raised.')


输出:

1 $ python raising.py
2 Enter something --> a
3 ShortInputException: The input was 1 long, expected at least 3
4
5 $ python raising.py
6 Enter something --> abc
7 No exception was raised.


   


this is a book about python. it was written by Swaroop C H.its name is "a byte of python". Table of Contents Preface Who This Book Is For History Lesson Status of the book Official Website License Terms Using the interpreter prompt Choosing an Editor Using a Source File Output How It Works Executable Python programs Getting Help Summary 4. The Basics Literal Constants Numbers Strings Variables Identifier Naming Data Types Objects Output How It Works Logical and Physical Lines Indentation Summary 5. Operators and Expressions Introduction Operators Operator Precedence Order of Evaluation Associativity Expressions Using Expressions Summary 6. Control Flow Introduction The if statement ivUsing the if statement How It Works The while statement Using the while statement The for loop Using the for statement Using the break statement The continue statement Using the continue statement Summary 7. Functions Introduction Defining a Function Function Parameters Using Function Parameters Local Variables Using Local Variables Using the global statement Default Argument Values Using Default Argument Values Keyword Arguments Using Keyword Arguments The return statement Using the literal statement DocStrings Using DocStrings Summary 8. Modules Introduction Using the sys module Byte-compiled .pyc files The from..import statement A module's __name__ Using a module's __name__ Making your own Modules Creating your own Modules from..import The dir() function Using the dir function Summary 9. Data Structures Introduction List Quick introduction to Objects and Classes Using Lists Tuple Using Tuples Tuples and the print statement Dictionary Using Dictionaries Sequences Using Sequences References Objects and References More about Strings String Methods Summary 10. Problem Solving - Writing a Python Script The Problem The Solution First Version Second Version Third Version Fourth Version More Refinements The Software Development Process Summary 11. Object-Oriented Programming Introduction The self Classes Creating a Class object Methods Using Object Methds The __init__ method Using the __init__ method Class and Object Variables Using Class and Object Variables Inheritance Using Inheritance Summary 12. Input/Output Files Using file Pickle Pickling and Unpickling Summary 13. Exceptions Errors Try..Except Handling Exceptions Raising Exceptions How To Raise Exceptions Try..Finally Using Finally Summary 14. The Python Standard Library Introduction The sys module Command Line Arguments More sys The os module Summary 15. More Python Special Methods Single Statement Blocks List Comprehension Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary 16. What Next? Graphical Software Summary of GUI Tools Explore More Summary A. Free/Libré and Open Source Software (FLOSS) B. About Colophon About the Author C. Revision History Timestamp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值