楼主: up_up。
1322 1

Python10分钟入门快速教程(三) [推广有奖]

  • 0关注
  • 26粉丝

教授

16%

还不是VIP/贵宾

-

威望
1
论坛币
913 个
通用积分
20.7205
学术水平
63 点
热心指数
88 点
信用等级
44 点
经验
17730 点
帖子
408
精华
0
在线时间
594 小时
注册时间
2015-2-21
最后登录
2022-11-22

楼主
up_up。 发表于 2015-7-27 11:05:27 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
10、异常

Python中的异常由 try-except [exceptionname] 块处理,例如:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

def some_function():

try:

# Division by zero raises an exception

10 / 0

except ZeroDivisionError:

print "Oops, invalid."

else:

# Exception didn't occur, we're good.

pass

finally:

# This is executed after the code block is run

# and all exceptions have been handled, even

# if a new exception is raised while handling.

print "We're done with that."


>>> some_function()

Oops, invalid.

We're done with that.

11、导入

外部库可以使用 import [libname] 关键字来导入。同时,你还可以用 from [libname] import [funcname] 来导入所需要的函数。例如:

1

2

3

4

5

6

import random

from time import clock


randomint = random.randint(1, 100)

>>> print randomint

64

12、文件I / O

Python针对文件的处理有很多内建的函数库可以调用。例如,这里演示了如何序列化文件(使用pickle库将数据结构转换为字符串):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

import pickle

mylist = ["This", "is", 4, 13327]

# Open the file C:\\binary.dat for writing. The letter r before the

# filename string is used to prevent backslash escaping.

myfile = open(r"C:\\binary.dat", "w")

pickle.dump(mylist, myfile)

myfile.close()


myfile = open(r"C:\\text.txt", "w")

myfile.write("This is a sample string")

myfile.close()


myfile = open(r"C:\\text.txt")

>>> print myfile.read()

'This is a sample string'

myfile.close()


# Open the file for reading.

myfile = open(r"C:\\binary.dat")

loadedlist = pickle.load(myfile)

myfile.close()

>>> print loadedlist

['This', 'is', 4, 13327]

13、其它杂项
  • 数值判断可以链接使用,例如 1<a<3 能够判断变量 a 是否在1和3之间。

  • 可以使用 del 删除变量或删除数组中的元素。

  • 列表推导式(List Comprehension)提供了一个创建和操作列表的有力工具。列表推导式由一个表达式以及紧跟着这个表达式的for语句构成,for语句还可以跟0个或多个if或for语句,来看下面的例子:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

>>> lst1 = [1, 2, 3]

>>> lst2 = [3, 4, 5]

>>> print [x * y for x in lst1 for y in lst2]

[3, 4, 5, 6, 8, 10, 9, 12, 15]

>>> print [x for x in lst1 if 4 > x > 1]

[2, 3]

# Check if an item has a specific property.

# "any" returns true if any item in the list is true.

>>> any([i % 3 for i in [3, 3, 4, 4, 3]])

True

# This is because 4 % 3 = 1, and 1 is true, so any()

# returns True.


# Check how many items have this property.

>>> sum(1 for i in [3, 3, 4, 4, 3] if i == 4)

2

>>> del lst1[0]

>>> print lst1

[2, 3]

>>> del lst1

  • 全局变量在函数之外声明,并且可以不需要任何特殊的声明即能读取,但如果你想要修改全局变量的值,就必须在函数开始之处用global关键字进行声明,否则Python会将此变量按照新的局部变量处理(请注意,如果不注意很容易被坑)。例如:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    number = 5


    def myfunc():

    # This will print 5.

    print number


    def anotherfunc():

    # This raises an exception because the variable has not

    # been bound before printing. Python knows that it an

    # object will be bound to it later and creates a new, local

    # object instead of accessing the global one.

    print number

    number = 3


    def yetanotherfunc():

    global number

    # This will correctly change the global.

    number = 3


小结

本教程并未涵盖Python语言的全部内容(甚至连一小部分都称不上)。Python有非常多的库以及很多的功能特点需要学习,所以要想学好Python你必须在此教程之外通过其它方式,例如阅读Dive into Python。希望这个教程能给你一个很好的入门指导。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python exception Accessing correctly Division invalid raised

经世济民。

沙发
hsj0624 发表于 2015-7-27 17:27:29
首先最重要的是下载pycharm 然后下载各种计算模块才是最基础的
已有 1 人评分论坛币 收起 理由
admin_kefu + 10 热心帮助其他会员

总评分: 论坛币 + 10   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-30 01:04