第1章 快速上手:基础知识

1.1 交互式解释器
这里写图片描述
这里写图片描述

Python在每行行末可加、可不加分号,推荐不加。

获得帮助:
1)输入help()

>>> help()

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

help> print
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

2)按F1
这里写图片描述

1.2 算法是什么

1.3 数和表达式

1.4 变量
Python变量没有默认值,使用前必须赋值。

>>> x = 3
>>> x * 2
6
>>> 

1.5 语句

1.6 获取用户输入

>>> x = input("x: ")
x: 34
>>> y = input("y: ")
y: 42
>>> print(int(x) * int(y))
1428
>>> 

1.7 函数

1.8 模块

>>> import math
>>> math.sqrt(9)
3.0
>>> from math import sqrt
>>> sqrt(9)
3.0
>>> 

1.8.2 回到未来
模块future,导入当前不支持,但未来将成为标准组成部门的功能。

1.9 保存并执行程序
Python程序扩展名为.py。

1.9.1 从命令提示符运行Python脚本
这里写图片描述

1.9.2 让脚本像普通程序一样
Linux下,在脚本第一行加上:

#!/usr/bin/env python

windows下在脚本后面加上下面一行,放置dos窗口立即关闭:

input("Press <enter>")

1.9.3 注释

#后面全是注释

1.10 字符串
1.10.1 单引号字符串以及对引号转义
1.10.2 拼接字符串
用 +
1.10.3 字符串表示str和repr

>>> print(repr("Hello,\nworld!"))
'Hello,\nworld!'
>>> print(str("Hello,\nworld!"))
Hello,
world!
>>> 

1.10.4 长字符串、原始字符串和字节
1 长字符串
使用反斜杠\,或者用三引号”’….”’

>>> print \
    ('Hello,world')
Hello,world
>>> print('''This is very
long string.''')
This is very
long string.
>>> 

2 原始字符串
原始字符串不以特殊方式处理反斜杠。

>>> print('c:\a\b\c')
c:\c
>>> print('c:\\a\\b\\c')
c:\a\b\c
>>> print(r'c:\a\b\c')
c:\a\b\c
>>> 

原始字符串用前缀r表示。

3 Unicode、byte和bytearray
Python使用Unicode编码来表示文本。
每个Unicode字符都使用一个码点表示,而码点是Unicode标准给每个字符指定的数字。
Unicode字符的通用机制:使用16或32的十六进制字面量(分别加上前缀\u或\U)或者使用字符的Unicode名称(\N{name})。

1.11 小结

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值