python 基础教程(第二版)

Magnus Lie Hetland 著

第一章 基础知识

1. 安装python 

下载地址:https://www.python.org/downloads/

2. Linux下输入python 进入python交互式解释器, 按ctrl+D 退出

root@cdndev:/share/test# python
Python 2.7.9 (default, Sep 14 2019, 20:00:08) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

1.9 模块

模块使用import 命令来导入模块, 然后按照“模块.函数”的格式来使用这个模块中的函数

>>> import math
>>> math.floor(32.9)
32.0

虚数: 1J 结尾

python 程序以.py 结尾

1.10.2 让脚本像普通程序一样运行

扫描二维码关注公众号,回复: 11626414 查看本文章

在test.py 代码的首行加入

#!/usr/bin/env python

然后直接执行./test.py 脚本就可以了

1.10.3 注释

以# 号开头

1.11 字符串

双引号或者单引号括起来的就是字符串

1.11.2 拼接字符串

用空格或者+ 将两个字符串拼起来

>>> "let's go" "hello world"
"let's gohello world"
>>> "hello" + "world"
'helloworld'

1.11.3 字符串表示, str 和repr

str 函数会把值转换为合理形式的字符串

repr 函数会创建一个字符串,它以合法的python表达式的形式来表示值

>>> print repr("hello, world")
'hello, world'
>>> print str("hello, world")
hello, world

1.11.4 input 和raw_input 的比较

input 会假设用户输入的是合法的python表达式

raw_input 会把所有的输入当作原始数据(raw data)

>>> input("enter a nuber:")
enter a nuber:1
1
>>> raw_input("enter a nuber: ")
enter a nuber: 2
'2'
>>> 

1.11.5 长字符串、原始字符串和Unicode

1. 长字符串

三个单引号''' ''''或者三个双引号""" """

2. 原始字符串

原始字符串以r 开头

3. Unicode

python中的普通字符串在内部是以8位的ASCII 码存储的, 而Unicode字符串则存储为16位Unicode字符。

1.12.1 本章的新函数

第二章 列表和元组

在python中,最基本的数据结构是序列(sequence)。序列中的每个元素被分配一个序号--即元素的位置,也被称为索引。第一个索引是0.

2.1 序列概览

列表和元组的区别在于:列表可以修改,元组则不能。

p45

猜你喜欢

转载自blog.csdn.net/ai2000ai/article/details/102834642